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
|
7 |
|
if ($constraint instanceof Assert\Range) { |
54
|
3 |
|
if (null !== $constraint->min) { |
55
|
2 |
|
$doc->setMin($constraint->min); |
|
|
|
|
56
|
|
|
} |
57
|
3 |
|
if (null !== $constraint->max) { |
58
|
3 |
|
$doc->setMax($constraint->max); |
59
|
|
|
} |
60
|
4 |
|
} elseif ($constraint instanceof Assert\LessThanOrEqual |
61
|
4 |
|
|| $constraint instanceof Assert\LessThan |
62
|
|
|
) { |
63
|
2 |
|
$doc->setMax($constraint->value); |
64
|
2 |
|
if ($constraint instanceof Assert\LessThan) { |
65
|
2 |
|
$doc->setInclusiveMax(false); |
66
|
|
|
} |
67
|
2 |
|
} elseif ($constraint instanceof Assert\GreaterThanOrEqual |
68
|
2 |
|
|| $constraint instanceof Assert\GreaterThan |
69
|
|
|
) { |
70
|
2 |
|
$doc->setMin($constraint->value); |
71
|
2 |
|
if ($constraint instanceof Assert\GreaterThan) { |
72
|
1 |
|
$doc->setInclusiveMin(false); |
73
|
|
|
} |
74
|
|
|
} |
75
|
7 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param CollectionDoc $doc |
79
|
|
|
* @param Constraint $constraint |
80
|
|
|
*/ |
81
|
3 |
|
private function appendCollectionDoc(CollectionDoc $doc, Constraint $constraint) |
82
|
|
|
{ |
83
|
3 |
|
if ($constraint instanceof Assert\Count) { |
84
|
3 |
|
if (null !== $constraint->min) { |
85
|
2 |
|
$doc->setMinItem($constraint->min); |
|
|
|
|
86
|
|
|
} |
87
|
3 |
|
if (null !== $constraint->max) { |
88
|
2 |
|
$doc->setMaxItem($constraint->max); |
89
|
|
|
} |
90
|
|
|
} |
91
|
3 |
|
} |
92
|
|
|
} |
93
|
|
|
|