Skip to content

Commit

Permalink
feat: allow cascade assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
rgdevment committed Apr 24, 2020
1 parent 289cd6b commit fe9f351
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/Node/BaseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,64 +61,86 @@ class BaseNode
*/
protected $allowNull = false;

public function setConstraints(array $constraints): void
public function setConstraints(array $constraints): self
{
$this->constraints = $constraints;

return $this;
}

public function addConstraint(ConstraintInterface $constraint): void
public function addConstraint(ConstraintInterface $constraint): self
{
$this->constraints[] = $constraint;

return $this;
}

public function addConstraints(array $constraints): void
public function addConstraints(array $constraints): self
{
$this->constraints = array_merge($this->constraints, $constraints);

return $this;
}

public function setTransformer(TransformerInterface $transformer): void
public function setTransformer(TransformerInterface $transformer): self
{
$this->transformer = $transformer;

return $this;
}

public function setInstantiator(InstantiatorInterface $instantiator): void
public function setInstantiator(InstantiatorInterface $instantiator): self
{
$this->instantiator = $instantiator;

return $this;
}

public function setTypeHandler(TypeHandler $typeHandler): void
public function setTypeHandler(TypeHandler $typeHandler): self
{
$this->typeHandler = $typeHandler;

return $this;
}

public function setType(string $type): void
public function setType(string $type): self
{
$this->type = $type;

return $this;
}

public function setTypeAlias(string $typeAlias): void
public function setTypeAlias(string $typeAlias): self
{
$this->typeAlias = $typeAlias;

return $this;
}

public function getTypeAlias(): string
{
return $this->typeAlias;
}

public function setRequired(bool $required): void
public function setRequired(bool $required): self
{
$this->required = $required;

return $this;
}

public function setDefault($default): void
public function setDefault($default): self
{
$this->default = $default;

return $this;
}

public function setAllowNull(bool $allowNull): void
public function setAllowNull(bool $allowNull): self
{
$this->allowNull = $allowNull;

return $this;
}

public function getDefault()
Expand Down
43 changes: 43 additions & 0 deletions tests/InputHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace Linio\Component\Input;

use Linio\Component\Input\Constraint\Email;
use Linio\Component\Input\Constraint\Enum;
use Linio\Component\Input\Constraint\Range;
use Linio\Component\Input\Constraint\StringSize;
use Linio\Component\Input\Instantiator\InstantiatorInterface;
use Linio\Component\Input\Instantiator\PropertyInstantiator;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -121,6 +124,31 @@ public function define(): void
}
}

class TestInputHandlerCascade extends InputHandler
{
public function define(): void
{
$this->add('name', 'string')
->setRequired(true)
->addConstraint(new StringSize(1, 80));

$this->add('age', 'int')
->setRequired(true)
->addConstraint(new Range(1, 99));

$this->add('gender', 'string')
->setRequired(true)
->addConstraint(new Enum(['male', 'female', 'other']));

$this->add('birthday', 'datetime')
->setRequired(false);

$this->add('email', 'string')
->setRequired(false)
->addConstraint(new Email());
}
}

class InputHandlerTest extends TestCase
{
public function testIsHandlingBasicInput(): void
Expand Down Expand Up @@ -539,6 +567,21 @@ public function testIsHandlingInputWithNullValues(): void

$this->assertNull($data);
}

public function testInputHandlerOnCascade(): void
{
$input = [
'name' => 'A',
'age' => 18,
'gender' => 'male',
'birthday' => '2000-01-01',
];

$inputHandler = new TestInputHandlerCascade();
$inputHandler->bind($input);

$this->assertTrue($inputHandler->isValid());
}
}

class TestConstraintOverrideType extends InputHandler
Expand Down

0 comments on commit fe9f351

Please sign in to comment.