Skip to content

Commit

Permalink
Merge pull request #152 from gsteel/iterable-validator-chain
Browse files Browse the repository at this point in the history
Mark the ValidatorChain as iterable and final
  • Loading branch information
Ocramius committed Sep 20, 2022
2 parents 7f15f81 + bb7f4c4 commit 42de39b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"require": {
"php": "^7.4 || ~8.0.0 || ~8.1.0",
"laminas/laminas-servicemanager": "^3.12.0",
"laminas/laminas-stdlib": "^3.10"
"laminas/laminas-stdlib": "^3.13"
},
"require-dev": {
"laminas/laminas-coding-standard": "^2.4.0",
Expand Down
20 changes: 10 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions src/ValidatorChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Laminas\Validator;

use Countable;
use IteratorAggregate;
use Laminas\ServiceManager\ServiceManager;
use Laminas\Stdlib\PriorityQueue;
use ReturnTypeWillChange;
use Traversable;

use function array_replace;
use function assert;
Expand All @@ -16,10 +18,10 @@

/**
* @psalm-type QueueElement = array{instance: ValidatorInterface, breakChainOnFailure: bool}
* @implements IteratorAggregate<array-key, QueueElement>
* @final
*/
class ValidatorChain implements
Countable,
ValidatorInterface
class ValidatorChain implements Countable, IteratorAggregate, ValidatorInterface
{
/**
* Default priority at which validators are added
Expand Down Expand Up @@ -248,7 +250,7 @@ public function isValid($value, $context = null)
{
$this->messages = [];
$result = true;
foreach ($this->validators as $element) {
foreach ($this as $element) {
$validator = $element['instance'];
assert($validator instanceof ValidatorInterface);
if ($validator->isValid($value, $context)) {
Expand Down Expand Up @@ -331,4 +333,10 @@ public function __sleep()
{
return ['validators', 'messages'];
}

/** @return Traversable<array-key, QueueElement> */
public function getIterator(): Traversable
{
return clone $this->validators;
}
}

0 comments on commit 42de39b

Please sign in to comment.