Skip to content

Commit

Permalink
Merge pull request #10 from portphp/validate-from-metadata
Browse files Browse the repository at this point in the history
Add support for validation from metadata (e.g., annotations)
  • Loading branch information
Baachi authored Dec 11, 2017
2 parents c79e6a4 + 7dac2e6 commit 05965b9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
17 changes: 17 additions & 0 deletions spec/Step/ValidatorStepSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,21 @@ function($item) use ($step, $next) {
}
);
}

function it_validates_an_item_from_metadata(
ValidatorInterface $validator,
ConstraintViolationListInterface $list
) {
$next = function() {};
$list->count()->willReturn(1);
$item = new \stdClass();
$validator->validate($item)->willReturn($list);

$this->process(
$item,
$next
);

$this->getViolations()->shouldReturn([1 => $list]);
}
}
8 changes: 6 additions & 2 deletions src/Step/ValidatorStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ public function getViolations()
*/
public function process($item, callable $next)
{
$constraints = new Constraints\Collection($this->constraints);
$list = $this->validator->validate($item, $constraints);
if (count($this->constraints) > 0) {
$constraints = new Constraints\Collection($this->constraints);
$list = $this->validator->validate($item, $constraints);
} else {
$list = $this->validator->validate($item);
}

if (count($list) > 0) {
$this->violations[$this->line] = $list;
Expand Down

0 comments on commit 05965b9

Please sign in to comment.