We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 18 |
Total Lines | 114 |
Duplicated Lines | 0 % |
Coverage | 97.56% |
Changes | 0 |
1 | <?php |
||
28 | abstract class AbstractComposite extends AbstractRule |
||
29 | { |
||
30 | /** |
||
31 | * @var Validatable[] |
||
32 | */ |
||
33 | private $rules = []; |
||
34 | |||
35 | /** |
||
36 | * Initializes the rule adding other rules to the stack. |
||
37 | * |
||
38 | * @param Validatable ...$rules |
||
39 | */ |
||
40 | 224 | public function __construct(Validatable ...$rules) |
|
41 | { |
||
42 | 224 | $this->rules = $rules; |
|
43 | 224 | } |
|
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | */ |
||
48 | 34 | public function setName(string $name): Validatable |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * Append a rule into the stack of rules. |
||
65 | * |
||
66 | * @param Validatable $rule |
||
67 | * |
||
68 | * @return AbstractComposite |
||
69 | */ |
||
70 | 223 | public function addRule(Validatable $rule): self |
|
71 | { |
||
72 | 223 | if ($this->shouldHaveNameOverwritten($rule)) { |
|
73 | 2 | $rule->setName($this->getName()); |
|
74 | } |
||
75 | |||
76 | 223 | $this->rules[] = $rule; |
|
77 | |||
78 | 223 | return $this; |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * Returns all the rules in the stack. |
||
83 | * |
||
84 | * @return Validatable[] |
||
85 | */ |
||
86 | 218 | public function getRules(): array |
|
87 | { |
||
88 | 218 | return $this->rules; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Returns all the exceptions throw when asserting all rules. |
||
93 | * |
||
94 | * @param mixed $input |
||
95 | * |
||
96 | * @return ValidationException[] |
||
97 | */ |
||
98 | 163 | protected function getAllThrownExceptions($input): array |
|
99 | { |
||
100 | 163 | return array_filter( |
|
101 | 163 | array_map( |
|
102 | 163 | function (Validatable $rule) use ($input): ?ValidationException { |
|
103 | try { |
||
104 | 163 | $rule->assert($input); |
|
105 | 140 | } catch (ValidationException $exception) { |
|
106 | 140 | $this->updateExceptionTemplate($exception); |
|
107 | |||
108 | 140 | return $exception; |
|
109 | } |
||
110 | |||
111 | 31 | return null; |
|
112 | 163 | }, |
|
113 | 163 | $this->getRules() |
|
114 | ) |
||
115 | ); |
||
116 | } |
||
117 | |||
118 | 223 | private function shouldHaveNameOverwritten(Validatable $rule): bool |
|
121 | } |
||
122 | |||
123 | 223 | private function hasName(Validatable $rule): bool |
|
124 | { |
||
125 | 223 | return null !== $rule->getName(); |
|
126 | } |
||
127 | |||
128 | 140 | private function updateExceptionTemplate(ValidationException $exception): void |
|
142 | } |
||
143 | 3 | } |
|
144 | } |
||
145 |