1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\AnalyticsBundle\Validator; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\Collection; |
6
|
|
|
use Doctrine\Common\Collections\Criteria; |
7
|
|
|
use Doctrine\ORM\PersistentCollection; |
8
|
|
|
|
9
|
|
|
use Symfony\Component\Validator\Constraint; |
10
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
11
|
|
|
|
12
|
|
|
use OroCRM\Bundle\AnalyticsBundle\Entity\RFMMetricCategory; |
13
|
|
|
|
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) |
27
|
|
|
{ |
28
|
|
|
if (!$value instanceof PersistentCollection) { |
29
|
|
|
return; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ($this->validateCount($value, $constraint) && $this->validateBlank($value, $constraint)) { |
|
|
|
|
33
|
|
|
$this->validateOrder($value, $constraint); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
} |
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) |
90
|
|
|
{ |
91
|
|
|
return $value === '' || $value === null; |
92
|
|
|
} |
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) |
102
|
|
|
{ |
103
|
|
|
if ($value->count() >= self::MIN_CATEGORIES_COUNT) { |
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->context->addViolationAt( |
|
|
|
|
108
|
|
|
$constraint->getType(), |
109
|
|
|
$constraint->countMessage, |
110
|
|
|
['%count%' => self::MIN_CATEGORIES_COUNT] |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Check that collection is in right order. |
118
|
|
|
* |
119
|
|
|
* For increasing collection values must be in ascending order. |
120
|
|
|
* For decreasing collection value must be in descending order. |
121
|
|
|
* |
122
|
|
|
* @param PersistentCollection $value |
123
|
|
|
* @param CategoriesConstraint $constraint |
124
|
|
|
*/ |
125
|
|
|
protected function validateOrder(PersistentCollection $value, CategoriesConstraint $constraint) |
126
|
|
|
{ |
127
|
|
|
if ($value->isEmpty()) { |
128
|
|
|
return; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$orderedByIndex = $value->matching(new Criteria(null, ['categoryIndex' => Criteria::ASC])); |
132
|
|
|
$isIncreasing = $this->isIncreasing($orderedByIndex); |
133
|
|
|
|
134
|
|
|
if ($isIncreasing) { |
135
|
|
|
$criteria = Criteria::ASC; |
136
|
|
|
} else { |
137
|
|
|
$criteria = Criteria::DESC; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$orderedByValue = $value->matching( |
141
|
|
|
new Criteria(null, ['minValue' => $criteria]) |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
if ($orderedByValue->toArray() !== $orderedByIndex->toArray()) { |
145
|
|
|
$this->context->addViolationAt($constraint->getType(), $constraint->message, ['%order%' => $criteria]); |
|
|
|
|
146
|
|
|
|
147
|
|
|
return; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if (!$isIncreasing) { |
151
|
|
|
return; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$invalidItems = $orderedByValue->filter( |
155
|
|
|
function (RFMMetricCategory $category) { |
156
|
|
|
$maxValue = $category->getMaxValue(); |
157
|
|
|
if (!$maxValue) { |
158
|
|
|
return false; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $category->getMinValue() >= $maxValue; |
162
|
|
|
} |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
if (!$invalidItems->isEmpty()) { |
166
|
|
|
$this->context->addViolationAt($constraint->getType(), $constraint->message, ['%order%' => $criteria]); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
|
|
public function validatedBy() |
174
|
|
|
{ |
175
|
|
|
return 'orocrm_analytics.validator.categories'; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param Collection $orderedByIndex |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
|
|
protected function isIncreasing(Collection $orderedByIndex) |
183
|
|
|
{ |
184
|
|
|
return is_null($orderedByIndex->first()->getMinValue()) |
185
|
|
|
&& is_null($orderedByIndex->last()->getMaxValue()); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
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.