| @@ 28-83 (lines=56) @@ | ||
| 25 | /** |
|
| 26 | * @package Limoncello\Validation |
|
| 27 | */ |
|
| 28 | class ArrayValidator extends BaseValidator |
|
| 29 | { |
|
| 30 | use ArrayValidation; |
|
| 31 | ||
| 32 | /** |
|
| 33 | * @param RuleInterface[] $rules |
|
| 34 | */ |
|
| 35 | public function __construct(array $rules) |
|
| 36 | { |
|
| 37 | parent::__construct(); |
|
| 38 | ||
| 39 | $this->setRules($rules); |
|
| 40 | } |
|
| 41 | ||
| 42 | /** |
|
| 43 | * @param RuleInterface[] $rules |
|
| 44 | * |
|
| 45 | * @return self |
|
| 46 | */ |
|
| 47 | public static function validator(array $rules): self |
|
| 48 | { |
|
| 49 | $validator = new static ($rules); |
|
| 50 | ||
| 51 | return $validator; |
|
| 52 | } |
|
| 53 | ||
| 54 | /** |
|
| 55 | * @inheritdoc |
|
| 56 | */ |
|
| 57 | public function validate($input): bool |
|
| 58 | { |
|
| 59 | if ($this->areAggregatorsDirty() === true) { |
|
| 60 | $this->resetAggregators(); |
|
| 61 | } |
|
| 62 | ||
| 63 | $this->validateArrayImplementation($input, $this->getCaptures(), $this->getErrors()); |
|
| 64 | $this->markAggregatorsAsDirty(); |
|
| 65 | ||
| 66 | $isOk = $this->getErrors()->count() <= 0; |
|
| 67 | ||
| 68 | return $isOk; |
|
| 69 | } |
|
| 70 | ||
| 71 | /** |
|
| 72 | * During validation you can pass to rules your custom context which might have any additional |
|
| 73 | * resources needed by your rules (extra properties, database connection settings, container, and etc). |
|
| 74 | * |
|
| 75 | * @param array $blocks |
|
| 76 | * |
|
| 77 | * @return ContextStorageInterface |
|
| 78 | */ |
|
| 79 | protected function createContextStorageFromBlocks(array $blocks): ContextStorageInterface |
|
| 80 | { |
|
| 81 | return new ContextStorage($blocks); |
|
| 82 | } |
|
| 83 | } |
|
| 84 | ||
| @@ 29-86 (lines=58) @@ | ||
| 26 | /** |
|
| 27 | * @package Limoncello\Validation |
|
| 28 | */ |
|
| 29 | class SingleValidator extends BaseValidator |
|
| 30 | { |
|
| 31 | use SingleValidation; |
|
| 32 | ||
| 33 | /** |
|
| 34 | * @param RuleInterface $rule |
|
| 35 | */ |
|
| 36 | public function __construct(RuleInterface $rule) |
|
| 37 | { |
|
| 38 | parent::__construct(); |
|
| 39 | ||
| 40 | $this->setRule($rule); |
|
| 41 | } |
|
| 42 | ||
| 43 | /** |
|
| 44 | * @param RuleInterface $rule |
|
| 45 | * |
|
| 46 | * @return ValidatorInterface |
|
| 47 | */ |
|
| 48 | public static function validator(RuleInterface $rule): ValidatorInterface |
|
| 49 | { |
|
| 50 | $validator = new static ($rule); |
|
| 51 | ||
| 52 | return $validator; |
|
| 53 | } |
|
| 54 | ||
| 55 | /** |
|
| 56 | * @inheritdoc |
|
| 57 | * |
|
| 58 | * @SuppressWarnings(PHPMD.StaticAccess) |
|
| 59 | */ |
|
| 60 | public function validate($input): bool |
|
| 61 | { |
|
| 62 | if ($this->areAggregatorsDirty() === true) { |
|
| 63 | $this->resetAggregators(); |
|
| 64 | } |
|
| 65 | ||
| 66 | $this->validateSingleImplementation($input, $this->getCaptures(), $this->getErrors()); |
|
| 67 | $this->markAggregatorsAsDirty(); |
|
| 68 | ||
| 69 | $noErrors = $this->getErrors()->count() <= 0; |
|
| 70 | ||
| 71 | return $noErrors; |
|
| 72 | } |
|
| 73 | ||
| 74 | /** |
|
| 75 | * During validation you can pass to rules your custom context which might have any additional |
|
| 76 | * resources needed by your rules (extra properties, database connection settings, container, and etc). |
|
| 77 | * |
|
| 78 | * @param array $blocks |
|
| 79 | * |
|
| 80 | * @return ContextStorageInterface |
|
| 81 | */ |
|
| 82 | protected function createContextStorageFromBlocks(array $blocks): ContextStorageInterface |
|
| 83 | { |
|
| 84 | return new ContextStorage($blocks); |
|
| 85 | } |
|
| 86 | } |
|
| 87 | ||