Skip to content

Commit

Permalink
feat: to allow recursive handler as parameter
Browse files Browse the repository at this point in the history
this new feature allows you to assign recursive handlers by parameter, managing to control infections and mutations by parameters.
  • Loading branch information
rgdevment committed Apr 24, 2020
1 parent fe9f351 commit 94fb0be
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/InputHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public function __construct(TypeHandler $typeHandler = null)
$this->root->setTypeHandler($this->typeHandler);
}

public function add(string $key, string $type, array $options = []): BaseNode
public function add(string $key, string $type, array $options = [], InputHandler $handler = null): BaseNode
{
return $this->root->add($key, $type, $options);
return $this->root->add($key, $type, $options, $handler);
}

public function remove(string $key): void
Expand Down
21 changes: 14 additions & 7 deletions src/Node/BaseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,16 @@ public function hasDefault(): bool
return (bool) $this->default;
}

public function add(string $key, string $type, array $options = []): BaseNode
public function add(string $key, string $type, array $options = [], InputHandler $handler = null): BaseNode
{
$child = $this->typeHandler->getType($type);

if (isset($options['handler'])) {
/** @var InputHandler $handler */
$handler = $options['handler'];
$handler->setRootType($type);
$handler->define();
if (isset($handler)) {
$child = $child->setHandler($handler, $type);
}

$child = $handler->getRoot();
if (isset($options['handler']) && !isset($handler)) {
$child = $child->setHandler($options['handler'], $type);
}

if (isset($options['required'])) {
Expand Down Expand Up @@ -281,4 +280,12 @@ protected function checkConstraints(string $field, $value): void
}
}
}

private function setHandler(InputHandler $handler, string $type): self
{
$handler->setRootType($type);
$handler->define();

return $handler->getRoot();
}
}
106 changes: 106 additions & 0 deletions tests/InputHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ public function define(): void
}
}

class TestRecursiveInputHandlerExplicit extends InputHandler
{
public function define(): void
{
$this->add('title', 'string');
$this->add('size', 'int');
$this->add('child', \stdClass::class, ['instantiator' => new PropertyInstantiator()], new TestInputHandler());
}
}

class TestNullableInputHandler extends InputHandler
{
public function define(): void
Expand Down Expand Up @@ -492,6 +502,102 @@ public function testIsHandlingInputWithRecursiveHandler(): void
$this->assertEquals([$fanA, $fanB, $fanC], $child->fans);
}

public function testIsHandlingInputWithRecursiveHandlerExplicit(): void
{
$input = [
'title' => 'Barfoo',
'size' => 20,
'child' => [
'title' => 'Foobar',
'size' => 35,
'dimensions' => [11, 22, 33],
'date' => '2015-01-01 22:50',
'metadata' => [
'foo' => 'bar',
],
'simple' => [
'date' => '2015-01-01 22:50',
],
'user' => [
'name' => false,
'age' => '28',
],
'author' => [
'name' => 'Barfoo',
'age' => 28,
'related' => [
'name' => 'Barfoo',
'age' => 28,
],
],
'fans' => [
[
'name' => 'A',
'age' => 18,
'birthday' => '2000-01-01',
],
[
'name' => 'B',
'age' => 28,
'birthday' => '2000-01-02',
],
[
'name' => 'C',
'age' => 38,
'birthday' => '2000-01-03',
],
],
],
];

$inputHandler = new TestRecursiveInputHandlerExplicit();
$inputHandler->bind($input);
$this->assertTrue($inputHandler->isValid());

// Basic fields
$this->assertEquals('Barfoo', $inputHandler->getData('title'));
$this->assertEquals(20, $inputHandler->getData('size'));
/** @var \stdClass $child */
$child = $inputHandler->getData('child');

// Scalar collection
$this->assertEquals([11, 22, 33], $child->dimensions);

// Transformer
$this->assertEquals(new \DateTime('2015-01-01 22:50'), $child->date);

// Mixed array
$this->assertEquals(['foo' => 'bar'], $child->metadata);

// Typed array
$this->assertEquals(['title' => 'Barfoo', 'size' => 15, 'date' => new \DateTime('2015-01-01 22:50')], $child->simple);

// Object and nested object
$related = new TestUser();
$related->setName('Barfoo');
$related->setAge(28);
$author = new TestUser();
$author->setName('Barfoo');
$author->setAge(28);
$author->setRelated($related);
$this->assertEquals($author, $child->author);

// Object collection
$fanA = new TestUser();
$fanA->setName('A');
$fanA->setAge(18);
$fanA->setBirthday(new \DateTime('2000-01-01'));
$fanB = new TestUser();
$fanB->setName('B');
$fanB->setAge(28);
$fanB->setBirthday(new \DateTime('2000-01-02'));
$fanC = new TestUser();
$fanC->setName('C');
$fanC->setAge(38);
$fanC->setBirthday(new \DateTime('2000-01-03'));
$this->assertEquals([$fanA, $fanB, $fanC], $child->fans);
}

public function testOverride(): void
{
$input = [
Expand Down

0 comments on commit 94fb0be

Please sign in to comment.