1 | <?php |
||
14 | class CategoriesValidator extends ConstraintValidator |
||
15 | { |
||
16 | const MIN_CATEGORIES_COUNT = 2; |
||
17 | |||
18 | /** |
||
19 | * Validate collection. |
||
20 | * |
||
21 | * @param PersistentCollection|RFMMetricCategory[] $value |
||
22 | * @param CategoriesConstraint $constraint |
||
23 | * |
||
24 | * {@inheritdoc} |
||
25 | */ |
||
26 | public function validate($value, Constraint $constraint) |
||
36 | |||
37 | /** |
||
38 | * Check collection for empty values. |
||
39 | * |
||
40 | * @param PersistentCollection $value |
||
41 | * @param CategoriesConstraint $constraint |
||
42 | * @return bool |
||
43 | */ |
||
44 | protected function validateBlank(PersistentCollection $value, CategoriesConstraint $constraint) |
||
84 | |||
85 | /** |
||
86 | * @param mixed $value |
||
87 | * @return bool |
||
88 | */ |
||
89 | protected function isEmpty($value) |
||
93 | |||
94 | /** |
||
95 | * Check that number of categories not less than minimum defined number. |
||
96 | * |
||
97 | * @param PersistentCollection $value |
||
98 | * @param CategoriesConstraint $constraint |
||
99 | * @return bool |
||
100 | */ |
||
101 | protected function validateCount(PersistentCollection $value, CategoriesConstraint $constraint) |
||
115 | |||
116 | /** |
||
117 | * Check that collection is in right order in next way: |
||
118 | * 1) Compare elements that ordered by index with elements that ordered by min value |
||
119 | * 2) Check if equality doesn't exist between different categories in min values |
||
120 | * 3) Check if max value ( null value exclude from checking ) always greater than min value |
||
121 | * |
||
122 | * For increasing collection values must be in ascending order. |
||
123 | * For decreasing collection value must be in descending order. |
||
124 | * |
||
125 | * @param PersistentCollection $value |
||
126 | * @param CategoriesConstraint $constraint |
||
127 | */ |
||
128 | protected function validateOrder(PersistentCollection $value, CategoriesConstraint $constraint) |
||
129 | { |
||
130 | if ($value->isEmpty()) { |
||
131 | return; |
||
132 | } |
||
133 | |||
134 | $orderedByIndex = $value->matching(new Criteria(null, ['categoryIndex' => Criteria::ASC])); |
||
135 | $isIncreasing = $this->isIncreasing($orderedByIndex); |
||
136 | $orderedByValueArray = $value->toArray(); |
||
137 | |||
138 | if ($isIncreasing) { |
||
139 | $inversion = 1; |
||
140 | $criteria = Criteria::ASC; |
||
141 | } else { |
||
142 | $inversion = -1; |
||
143 | $criteria = Criteria::DESC; |
||
144 | } |
||
145 | |||
146 | $isValid = $this->orderByValue($orderedByValueArray, $inversion); |
||
147 | if (!$isValid || $orderedByValueArray !== $orderedByIndex->toArray()) { |
||
148 | $this->context->addViolationAt($constraint->getType(), $constraint->message, ['%order%' => $criteria]); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | public function validatedBy() |
||
159 | |||
160 | /** |
||
161 | * @param Collection $orderedByIndex |
||
162 | * @return bool |
||
163 | */ |
||
164 | protected function isIncreasing(Collection $orderedByIndex) |
||
169 | |||
170 | /** |
||
171 | * @param array $value |
||
172 | * @param int $inversion |
||
173 | * |
||
174 | * @return boolean |
||
175 | */ |
||
176 | protected function orderByValue(array &$value, $inversion) |
||
196 | } |
||
197 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.