|
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 |
|
if ($constraint instanceof Assert\Length) { |
|
38
|
3 |
|
if (null !== $constraint->min) { |
|
39
|
2 |
|
$doc->setMinLength((int) $constraint->min); |
|
40
|
|
|
} |
|
41
|
3 |
|
if (null !== $constraint->max) { |
|
42
|
3 |
|
$doc->setMaxLength((int) $constraint->max); |
|
43
|
|
|
} |
|
44
|
2 |
|
} elseif ($constraint instanceof Assert\NotBlank && null === $doc->getMinLength()) { |
|
45
|
|
|
// Not blank so minimum 1 character |
|
46
|
1 |
|
$doc->setMinLength(1); |
|
47
|
1 |
|
} elseif ($constraint instanceof Assert\Blank && null === $doc->getMaxLength()) { |
|
48
|
|
|
// Blank so maximum 0 character |
|
49
|
1 |
|
$doc->setMaxLength(0); |
|
50
|
|
|
} |
|
51
|
5 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param NumberDoc $doc |
|
55
|
|
|
* @param Constraint $constraint |
|
56
|
|
|
*/ |
|
57
|
7 |
|
private function appendNumberDoc(NumberDoc $doc, Constraint $constraint) : void |
|
58
|
|
|
{ |
|
59
|
7 |
|
$this->appendNumberMinMax($doc, $constraint); |
|
60
|
|
|
|
|
61
|
7 |
|
if ($constraint instanceof Assert\LessThan) { |
|
62
|
1 |
|
$doc->setInclusiveMax(false); |
|
63
|
6 |
|
} elseif ($constraint instanceof Assert\GreaterThan) { |
|
64
|
1 |
|
$doc->setInclusiveMin(false); |
|
65
|
|
|
} |
|
66
|
7 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param CollectionDoc $doc |
|
70
|
|
|
* @param Constraint $constraint |
|
71
|
|
|
*/ |
|
72
|
11 |
|
private function appendCollectionDoc(CollectionDoc $doc, Constraint $constraint) : void |
|
73
|
|
|
{ |
|
74
|
11 |
|
if ($constraint instanceof Assert\Choice || $constraint instanceof Assert\Count) { |
|
75
|
6 |
|
if (null !== $constraint->min) { |
|
76
|
4 |
|
$doc->setMinItem((int) $constraint->min); |
|
77
|
|
|
} |
|
78
|
6 |
|
if (null !== $constraint->max) { |
|
79
|
6 |
|
$doc->setMaxItem((int) $constraint->max); |
|
80
|
|
|
} |
|
81
|
5 |
|
} elseif ($constraint instanceof Assert\NotBlank && null === $doc->getMinItem()) { |
|
82
|
|
|
// Not blank so minimum 1 item |
|
83
|
1 |
|
$doc->setMinItem(1); |
|
84
|
|
|
} /* Documentation does not mention array, counter to NotBlank constraint |
|
85
|
|
|
elseif ($constraint instanceof Assert\Blank && null === $doc->getMaxItem()) { |
|
86
|
|
|
// Blank so maximum 0 item |
|
87
|
|
|
$doc->setMaxItem(0); |
|
88
|
|
|
}*/ |
|
89
|
11 |
|
$this->appendLessGreaterThanMinMaxItem($doc, $constraint); |
|
90
|
11 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param NumberDoc $doc |
|
94
|
|
|
* @param Constraint $constraint |
|
95
|
|
|
*/ |
|
96
|
7 |
|
private function appendNumberMinMax(NumberDoc $doc, Constraint $constraint) : void |
|
97
|
|
|
{ |
|
98
|
7 |
|
if ($constraint instanceof Assert\Range) { |
|
99
|
3 |
|
if (null !== $constraint->min) { |
|
100
|
2 |
|
$doc->setMin($constraint->min); |
|
101
|
|
|
} |
|
102
|
3 |
|
if (null !== $constraint->max) { |
|
103
|
3 |
|
$doc->setMax($constraint->max); |
|
104
|
|
|
} |
|
105
|
4 |
|
} elseif ($constraint instanceof Assert\LessThanOrEqual |
|
106
|
4 |
|
|| $constraint instanceof Assert\LessThan |
|
107
|
|
|
) { |
|
108
|
2 |
|
$doc->setMax($constraint->value); |
|
109
|
2 |
|
} elseif ($constraint instanceof Assert\GreaterThanOrEqual |
|
110
|
2 |
|
|| $constraint instanceof Assert\GreaterThan |
|
111
|
|
|
) { |
|
112
|
2 |
|
$doc->setMin($constraint->value); |
|
113
|
|
|
} |
|
114
|
7 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param CollectionDoc $doc |
|
118
|
|
|
* @param Constraint $constraint |
|
119
|
|
|
*/ |
|
120
|
11 |
|
private function appendLessGreaterThanMinMaxItem(CollectionDoc $doc, Constraint $constraint): void |
|
121
|
|
|
{ |
|
122
|
11 |
|
if ($constraint instanceof Assert\GreaterThan || $constraint instanceof Assert\GreaterThanOrEqual) { |
|
123
|
2 |
|
$doc->setMinItem( |
|
124
|
2 |
|
$constraint instanceof Assert\GreaterThanOrEqual |
|
125
|
1 |
|
? $constraint->value |
|
126
|
2 |
|
: $constraint->value + 1 |
|
127
|
|
|
); |
|
128
|
9 |
|
} elseif ($constraint instanceof Assert\LessThan || $constraint instanceof Assert\LessThanOrEqual) { |
|
129
|
2 |
|
$doc->setMaxItem( |
|
130
|
2 |
|
$constraint instanceof Assert\LessThanOrEqual |
|
131
|
1 |
|
? $constraint->value |
|
132
|
2 |
|
: $constraint->value - 1 |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
11 |
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|