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
|
13 |
|
public function append(TypeDoc $doc, Constraint $constraint) |
21
|
|
|
{ |
22
|
13 |
|
if ($doc instanceof StringDoc) { |
23
|
3 |
|
$this->appendStringDoc($doc, $constraint); |
24
|
10 |
|
} elseif ($doc instanceof NumberDoc) { |
25
|
7 |
|
$this->appendNumberDoc($doc, $constraint); |
26
|
3 |
|
} elseif ($doc instanceof CollectionDoc) { |
27
|
3 |
|
$this->appendCollectionDoc($doc, $constraint); |
28
|
|
|
} |
29
|
13 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param StringDoc $doc |
33
|
|
|
* @param Constraint $constraint |
34
|
|
|
*/ |
35
|
3 |
|
private function appendStringDoc(StringDoc $doc, Constraint $constraint) |
36
|
|
|
{ |
37
|
3 |
|
if ($constraint instanceof Assert\Length) { |
38
|
3 |
|
if (null !== $constraint->min) { |
39
|
2 |
|
$doc->setMinLength($constraint->min); |
|
|
|
|
40
|
|
|
} |
41
|
3 |
|
if (null !== $constraint->max) { |
42
|
2 |
|
$doc->setMaxLength($constraint->max); |
43
|
|
|
} |
44
|
|
|
} |
45
|
3 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param NumberDoc $doc |
49
|
|
|
* @param Constraint $constraint |
50
|
|
|
*/ |
51
|
7 |
|
private function appendNumberDoc(NumberDoc $doc, Constraint $constraint) |
52
|
|
|
{ |
53
|
|
|
switch (true) { |
54
|
7 |
|
case $constraint instanceof Assert\Range: |
55
|
3 |
|
if (null !== $constraint->min) { |
56
|
2 |
|
$doc->setMin($constraint->min); |
|
|
|
|
57
|
|
|
} |
58
|
3 |
|
if (null !== $constraint->max) { |
59
|
2 |
|
$doc->setMax($constraint->max); |
60
|
|
|
} |
61
|
3 |
|
break; |
62
|
4 |
|
case $constraint instanceof Assert\LessThanOrEqual: |
63
|
3 |
|
case $constraint instanceof Assert\LessThan: |
64
|
2 |
|
$doc->setMax($constraint->value); |
65
|
2 |
|
break; |
66
|
2 |
|
case $constraint instanceof Assert\GreaterThanOrEqual: |
67
|
1 |
|
case $constraint instanceof Assert\GreaterThan: |
68
|
2 |
|
$doc->setMin($constraint->value); |
69
|
2 |
|
break; |
70
|
|
|
} |
71
|
|
|
switch (true) { |
72
|
7 |
|
case $constraint instanceof Assert\GreaterThan: |
73
|
1 |
|
$doc->setInclusiveMin(false); |
74
|
1 |
|
break; |
75
|
6 |
|
case $constraint instanceof Assert\LessThan: |
76
|
1 |
|
$doc->setInclusiveMax(false); |
77
|
1 |
|
break; |
78
|
|
|
} |
79
|
7 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param CollectionDoc $doc |
83
|
|
|
* @param Constraint $constraint |
84
|
|
|
*/ |
85
|
3 |
|
private function appendCollectionDoc(CollectionDoc $doc, Constraint $constraint) |
86
|
|
|
{ |
87
|
3 |
|
if ($constraint instanceof Assert\Count) { |
88
|
3 |
|
if (null !== $constraint->min) { |
89
|
2 |
|
$doc->setMinItem($constraint->min); |
|
|
|
|
90
|
|
|
} |
91
|
3 |
|
if (null !== $constraint->max) { |
92
|
2 |
|
$doc->setMaxItem($constraint->max); |
93
|
|
|
} |
94
|
|
|
} |
95
|
3 |
|
} |
96
|
|
|
} |
97
|
|
|
|