|
1 | | -<?php |
2 | | - |
3 | | -declare(strict_types=1); |
4 | | - |
5 | | -namespace PhpDevCommunity\Validator\Assert; |
6 | | - |
7 | | -use PhpDevCommunity\Validator\Validation; |
8 | | -use PhpDevCommunity\Validator\ValidationProcessor; |
9 | | -use function ctype_alpha; |
10 | | - |
11 | | -final class Collection extends AbstractValidator |
12 | | -{ |
13 | | - private string $message = 'This value should be of type {{ type }}.'; |
14 | | - |
15 | | - /** |
16 | | - * @param array<ValidatorInterface> $validators |
17 | | - */ |
18 | | - private array $validators; |
19 | | - private array $errors = []; |
20 | | - |
21 | | - /** |
22 | | - * @param array<ValidatorInterface> $validators |
23 | | - */ |
24 | | - public function __construct(array $validators) |
25 | | - { |
26 | | - foreach ($validators as $validator) { |
27 | | - if ($validator instanceof ValidatorInterface === false) { |
28 | | - throw new \InvalidArgumentException(sprintf('The validator must be an instance of %s', ValidatorInterface::class)); |
29 | | - } |
30 | | - } |
31 | | - $this->validators = $validators; |
32 | | - } |
33 | | - public function validate($value): bool |
34 | | - { |
35 | | - if ($value === null) { |
36 | | - return true; |
37 | | - } |
38 | | - |
39 | | - if (is_array($value) === false) { |
40 | | - $this->error($this->message, ['value' => $value, 'type' => 'collection']); |
41 | | - return false; |
42 | | - } |
43 | | - |
44 | | - $validationProcessor = new ValidationProcessor(); |
45 | | - $errors = []; |
46 | | - foreach ($value as $key => $element) { |
47 | | - $errors = array_merge($errors, $validationProcessor->process($this->validators, $key, $element)); |
48 | | - } |
49 | | - |
50 | | - if ($errors !== []) { |
51 | | - $this->errors = $errors; |
52 | | - return false; |
53 | | - } |
54 | | - |
55 | | - return true; |
56 | | - } |
57 | | - |
58 | | - public function message(string $message): self |
59 | | - { |
60 | | - $this->message = $message; |
61 | | - return $this; |
62 | | - } |
63 | | - |
64 | | - public function getErrors(): array |
65 | | - { |
66 | | - return $this->errors; |
67 | | - } |
68 | | -} |
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpDevCommunity\Validator\Assert; |
| 6 | + |
| 7 | +use PhpDevCommunity\Validator\Validation; |
| 8 | +use PhpDevCommunity\Validator\ValidationProcessor; |
| 9 | +use function ctype_alpha; |
| 10 | + |
| 11 | +final class Collection extends AbstractValidator |
| 12 | +{ |
| 13 | + private string $message = 'This value should be of type {{ type }}.'; |
| 14 | + |
| 15 | + /** |
| 16 | + * @param array<ValidatorInterface> $validators |
| 17 | + */ |
| 18 | + private array $validators; |
| 19 | + |
| 20 | + private ValidationProcessor $validationProcessor; |
| 21 | + private array $errors = []; |
| 22 | + |
| 23 | + /** |
| 24 | + * @param array<ValidatorInterface> $validators |
| 25 | + */ |
| 26 | + public function __construct(array $validators) |
| 27 | + { |
| 28 | + foreach ($validators as $validator) { |
| 29 | + if ($validator instanceof ValidatorInterface === false) { |
| 30 | + throw new \InvalidArgumentException(sprintf('The validator must be an instance of %s', ValidatorInterface::class)); |
| 31 | + } |
| 32 | + } |
| 33 | + $this->validators = $validators; |
| 34 | + $this->validationProcessor = new ValidationProcessor(); |
| 35 | + } |
| 36 | + public function convertEmptyToNull(): self |
| 37 | + { |
| 38 | + $this->validationProcessor->convertEmptyToNull(); |
| 39 | + return $this; |
| 40 | + } |
| 41 | + |
| 42 | + public function noConvertEmptyToNull(): self |
| 43 | + { |
| 44 | + $this->validationProcessor->noConvertEmptyToNull(); |
| 45 | + return $this; |
| 46 | + } |
| 47 | + |
| 48 | + public function validate($value): bool |
| 49 | + { |
| 50 | + if ($value === null) { |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + if (is_array($value) === false) { |
| 55 | + $this->error($this->message, ['value' => $value, 'type' => 'collection']); |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + $errors = []; |
| 60 | + foreach ($value as $key => $element) { |
| 61 | + $errors = array_merge($errors, $this->validationProcessor->process($this->validators, $key, $element)); |
| 62 | + } |
| 63 | + |
| 64 | + if ($errors !== []) { |
| 65 | + $this->errors = $errors; |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + public function message(string $message): self |
| 73 | + { |
| 74 | + $this->message = $message; |
| 75 | + return $this; |
| 76 | + } |
| 77 | + |
| 78 | + public function getErrors(): array |
| 79 | + { |
| 80 | + return $this->errors; |
| 81 | + } |
| 82 | +} |
0 commit comments