1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\JsonRpcParamsSymfonyConstraintDoc\App\Helper; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Validator\Constraint; |
5
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
6
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\CollectionDoc; |
7
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\NumberDoc; |
8
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\StringDoc; |
9
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\TypeDoc; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class MinMaxHelper |
13
|
|
|
*/ |
14
|
|
|
class MinMaxHelper |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param TypeDoc $doc |
18
|
|
|
* @param Constraint $constraint |
19
|
|
|
*/ |
20
|
23 |
|
public function append(TypeDoc $doc, Constraint $constraint) : void |
21
|
|
|
{ |
22
|
23 |
|
if ($doc instanceof StringDoc) { |
23
|
5 |
|
$this->appendStringDoc($doc, $constraint); |
24
|
18 |
|
} elseif ($doc instanceof NumberDoc) { |
25
|
7 |
|
$this->appendNumberDoc($doc, $constraint); |
26
|
11 |
|
} elseif ($doc instanceof CollectionDoc) { |
27
|
11 |
|
$this->appendCollectionDoc($doc, $constraint); |
28
|
|
|
} |
29
|
23 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param StringDoc $doc |
33
|
|
|
* @param Constraint $constraint |
34
|
|
|
*/ |
35
|
5 |
|
private function appendStringDoc(StringDoc $doc, Constraint $constraint) : void |
36
|
|
|
{ |
37
|
5 |
|
$min = $max = null; |
38
|
5 |
|
if ($constraint instanceof Assert\Length) { |
39
|
3 |
|
$min = $constraint->min; |
40
|
3 |
|
$max = $constraint->max; |
41
|
2 |
|
} elseif ($constraint instanceof Assert\NotBlank && null === $doc->getMinLength()) { |
42
|
|
|
// Not blank so minimum 1 character |
43
|
1 |
|
$min = 1; |
44
|
1 |
|
} elseif ($constraint instanceof Assert\Blank && null === $doc->getMaxLength()) { |
45
|
|
|
// Blank so maximum 0 character |
46
|
1 |
|
$max = 0; |
47
|
|
|
} |
48
|
|
|
|
49
|
5 |
|
$this->setMinMaxLengthIfNotNull($doc, $min, $max); |
|
|
|
|
50
|
5 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param NumberDoc $doc |
54
|
|
|
* @param Constraint $constraint |
55
|
|
|
*/ |
56
|
7 |
|
private function appendNumberDoc(NumberDoc $doc, Constraint $constraint) : void |
57
|
|
|
{ |
58
|
7 |
|
$this->appendNumberMinMax($doc, $constraint); |
59
|
|
|
|
60
|
7 |
|
if ($constraint instanceof Assert\LessThan) { |
61
|
1 |
|
$doc->setInclusiveMax(false); |
62
|
6 |
|
} elseif ($constraint instanceof Assert\GreaterThan) { |
63
|
1 |
|
$doc->setInclusiveMin(false); |
64
|
|
|
} |
65
|
7 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param CollectionDoc $doc |
69
|
|
|
* @param Constraint $constraint |
70
|
|
|
*/ |
71
|
11 |
|
private function appendCollectionDoc(CollectionDoc $doc, Constraint $constraint) : void |
72
|
|
|
{ |
73
|
11 |
|
$min = $max = null; |
74
|
11 |
|
if ($constraint instanceof Assert\Choice || $constraint instanceof Assert\Count) { |
75
|
6 |
|
$min = $constraint->min; |
76
|
6 |
|
$max = $constraint->max; |
77
|
5 |
|
} elseif ($constraint instanceof Assert\NotBlank && null === $doc->getMinItem()) { |
78
|
|
|
// Not blank so minimum 1 item |
79
|
1 |
|
$min = 1; |
80
|
|
|
} /* Documentation does not mention array, counter to NotBlank constraint |
81
|
|
|
elseif ($constraint instanceof Assert\Blank && null === $doc->getMaxItem()) { |
82
|
|
|
// Blank so maximum 0 item |
83
|
|
|
$max = 0; |
84
|
|
|
}*/ |
85
|
11 |
|
$this->setMinMaxItemIfNotNull($doc, $min, $max); |
|
|
|
|
86
|
11 |
|
$this->appendLessGreaterThanMinMaxItem($doc, $constraint); |
87
|
11 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param NumberDoc $doc |
91
|
|
|
* @param Constraint $constraint |
92
|
|
|
*/ |
93
|
7 |
|
private function appendNumberMinMax(NumberDoc $doc, Constraint $constraint) : void |
94
|
|
|
{ |
95
|
7 |
|
$min = $max = null; |
96
|
7 |
|
if ($constraint instanceof Assert\Range) { |
97
|
3 |
|
$min = $constraint->min; |
98
|
3 |
|
$max = $constraint->max; |
99
|
4 |
|
} elseif ($constraint instanceof Assert\LessThanOrEqual |
100
|
4 |
|
|| $constraint instanceof Assert\LessThan |
101
|
|
|
) { |
102
|
2 |
|
$max = $constraint->value; |
103
|
2 |
|
} elseif ($constraint instanceof Assert\GreaterThanOrEqual |
104
|
2 |
|
|| $constraint instanceof Assert\GreaterThan |
105
|
|
|
) { |
106
|
2 |
|
$min = $constraint->value; |
107
|
|
|
} |
108
|
|
|
|
109
|
7 |
|
$this->setMinMaxIfNotNull($doc, $min, $max); |
110
|
7 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param CollectionDoc $doc |
114
|
|
|
* @param Constraint $constraint |
115
|
|
|
*/ |
116
|
11 |
|
private function appendLessGreaterThanMinMaxItem(CollectionDoc $doc, Constraint $constraint): void |
117
|
|
|
{ |
118
|
11 |
|
$min = $max = null; |
119
|
11 |
|
if ($constraint instanceof Assert\GreaterThan || $constraint instanceof Assert\GreaterThanOrEqual) { |
120
|
2 |
|
$min = $constraint instanceof Assert\GreaterThanOrEqual |
121
|
1 |
|
? $constraint->value |
122
|
2 |
|
: ($constraint->value + 1) |
123
|
|
|
; |
124
|
9 |
|
} elseif ($constraint instanceof Assert\LessThan || $constraint instanceof Assert\LessThanOrEqual) { |
125
|
2 |
|
$max = $constraint instanceof Assert\LessThanOrEqual |
126
|
1 |
|
? $constraint->value |
127
|
2 |
|
: $constraint->value - 1 |
128
|
|
|
; |
129
|
|
|
} |
130
|
|
|
|
131
|
11 |
|
$this->setMinMaxItemIfNotNull($doc, $min, $max); |
132
|
11 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param StringDoc $doc |
136
|
|
|
* @param null|int $min |
137
|
|
|
* @param null|int $max |
138
|
|
|
*/ |
139
|
5 |
|
private function setMinMaxLengthIfNotNull(StringDoc $doc, $min, $max): void |
140
|
|
|
{ |
141
|
5 |
|
if (null !== $min) { |
142
|
3 |
|
$doc->setMinLength((int)$min); |
143
|
|
|
} |
144
|
5 |
|
if (null !== $max) { |
145
|
3 |
|
$doc->setMaxLength((int)$max); |
146
|
|
|
} |
147
|
5 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param CollectionDoc $doc |
151
|
|
|
* @param null|int $min |
152
|
|
|
* @param null|int $max |
153
|
|
|
*/ |
154
|
11 |
|
private function setMinMaxItemIfNotNull(CollectionDoc $doc, $min, $max): void |
155
|
|
|
{ |
156
|
11 |
|
if (null !== $min) { |
157
|
7 |
|
$doc->setMinItem((int) $min); |
158
|
|
|
} |
159
|
11 |
|
if (null !== $max) { |
160
|
6 |
|
$doc->setMaxItem((int) $max); |
161
|
|
|
} |
162
|
11 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param NumberDoc $doc |
166
|
|
|
* @param null|int|float $min |
167
|
|
|
* @param null|int|float $max |
168
|
|
|
*/ |
169
|
7 |
|
private function setMinMaxIfNotNull(NumberDoc $doc, $min, $max): void |
170
|
|
|
{ |
171
|
7 |
|
if (null !== $min) { |
172
|
4 |
|
$doc->setMin($min); |
173
|
|
|
} |
174
|
7 |
|
if (null !== $max) { |
175
|
4 |
|
$doc->setMax($max); |
176
|
|
|
} |
177
|
7 |
|
} |
178
|
|
|
} |
179
|
|
|
|