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) |
||
45 | { |
||
46 | $orderedByIndex = $value->matching(new Criteria(null, ['categoryIndex' => Criteria::ASC])); |
||
47 | $isIncreasing = $this->isIncreasing($orderedByIndex); |
||
48 | |||
49 | if ($isIncreasing) { |
||
50 | $firstMax = $orderedByIndex->first()->getMaxValue(); |
||
51 | $lastMin = $orderedByIndex->last()->getMinValue(); |
||
52 | $hasEmpty = $this->isEmpty($firstMax) || $this->isEmpty($lastMin); |
||
53 | } else { |
||
54 | $firstMin = $orderedByIndex->first()->getMinValue(); |
||
55 | $lastMax = $orderedByIndex->last()->getMaxValue(); |
||
56 | $hasEmpty = $this->isEmpty($firstMin) || $this->isEmpty($lastMax); |
||
57 | } |
||
58 | |||
59 | if (!$hasEmpty) { |
||
60 | $orderedByIndexWithoutEmpty = $orderedByIndex->filter( |
||
61 | function (RFMMetricCategory $category) use ($orderedByIndex) { |
||
62 | return !in_array($category, [$orderedByIndex->first(), $orderedByIndex->last()], true); |
||
63 | } |
||
64 | ); |
||
65 | |||
66 | /** @var RFMMetricCategory $category */ |
||
67 | foreach ($orderedByIndexWithoutEmpty->toArray() as $category) { |
||
68 | $min = $category->getMinValue(); |
||
69 | $max = $category->getMaxValue(); |
||
70 | |||
71 | if ($this->isEmpty($min) || $this->isEmpty($max)) { |
||
72 | $hasEmpty = true; |
||
73 | break; |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | |||
78 | if ($hasEmpty) { |
||
79 | $this->context->addViolationAt($constraint->getType(), $constraint->blankMessage); |
||
80 | } |
||
81 | |||
82 | return !$hasEmpty; |
||
83 | } |
||
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) |
||
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | */ |
||
171 | public function validatedBy() |
||
175 | |||
176 | /** |
||
177 | * @param Collection $orderedByIndex |
||
178 | * @return bool |
||
179 | */ |
||
180 | protected function isIncreasing(Collection $orderedByIndex) |
||
185 | } |
||
186 |
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.