Skip to content

Commit

Permalink
Added addOption() method to ValidatorStep to allow the use of Collect…
Browse files Browse the repository at this point in the history
…ion constraint options.
  • Loading branch information
cm committed Mar 16, 2018
1 parent 05965b9 commit b1e3269
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
17 changes: 17 additions & 0 deletions spec/Step/ValidatorStepSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ function($item) use ($step, $next) {
$this->getViolations()->shouldReturn([1 => $list]);
}

function it_throws_an_exception_when_option_is_not_supported(Step $step, ValidatorInterface $validator, Constraint $constraint, ConstraintViolation $violation)
{
$next = function() {};
$item = ['foo' => true];
$step->process($item, $next)->shouldNotBeCalled();

$this->add('foo', $constraint)->shouldReturn($this);
$this->addOption('bar', 'baz')->shouldReturn($this);

$this->shouldThrow('Symfony\Component\Validator\Exception\InvalidOptionsException')->duringProcess(
$item,
function($item) use ($step, $next) {
return $step->process($item, $next);
}
);
}

function it_throws_an_exception_during_process_when_validation_fails(
Step $step,
ValidatorInterface $validator,
Expand Down
21 changes: 18 additions & 3 deletions src/Step/ValidatorStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public function __construct(ValidatorInterface $validator)
*/
public function add($field, Constraint $constraint)
{
if (!isset($this->constraints[$field])) {
$this->constraints[$field] = [];
if (!isset($this->constraints['fields'][$field])) {
$this->constraints['fields'][$field] = [];
}

$this->constraints[$field][] = $constraint;
$this->constraints['fields'][$field][] = $constraint;

return $this;
}
Expand All @@ -78,6 +78,21 @@ public function getViolations()
return $this->violations;
}

/**
* Add additional options to the Collection constraint.
*
* @param string $option
* @param mixed $optionValue
*
* @return $this
*/
public function addOption($option, $optionValue)
{
$this->constraints[$option] = $optionValue;

return $this;
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit b1e3269

Please sign in to comment.