Passed
Push — master ( d8fa97...555fd2 )
by Yo
01:32
created

MinMaxHelper::appendStringDoc()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 4
nop 2
dl 0
loc 15
ccs 10
cts 10
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
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
    use ClassComparatorTrait;
17
18
    /**
19
     * @param TypeDoc    $doc
20
     * @param Constraint $constraint
21
     */
22 23
    public function append(TypeDoc $doc, Constraint $constraint) : void
23
    {
24 23
        if ($doc instanceof StringDoc) {
25 5
            $this->appendStringDoc($doc, $constraint);
26 18
        } elseif ($doc instanceof NumberDoc) {
27 7
            $this->appendNumberDoc($doc, $constraint);
28 11
        } elseif ($doc instanceof CollectionDoc) {
29 11
            $this->appendCollectionDoc($doc, $constraint);
30
        }
31 23
    }
32
33
    /**
34
     * @param StringDoc  $doc
35
     * @param Constraint $constraint
36
     */
37 5
    private function appendStringDoc(StringDoc $doc, Constraint $constraint) : void
38
    {
39 5
        $min = $max = null;
40 5
        if ($constraint instanceof Assert\Length) {
41 3
            $min = $constraint->min;
42 3
            $max = $constraint->max;
43 2
        } elseif ($constraint instanceof Assert\NotBlank && null === $doc->getMinLength()) {
44
            // Not blank so minimum 1 character
45 1
            $min = 1;
46 1
        } elseif ($constraint instanceof Assert\Blank && null === $doc->getMaxLength()) {
47
            // Blank so maximum 0 character
48 1
            $max = 0;
49
        }
50
51 5
        $this->setMinMaxLengthIfNotNull($doc, $min, $max);
52 5
    }
53
54
    /**
55
     * @param NumberDoc  $doc
56
     * @param Constraint $constraint
57
     */
58 7
    private function appendNumberDoc(NumberDoc $doc, Constraint $constraint) : void
59
    {
60 7
        $this->appendNumberMinMax($doc, $constraint);
61
62 7
        if ($constraint instanceof Assert\LessThan) {
63 1
            $doc->setInclusiveMax(false);
64 6
        } elseif ($constraint instanceof Assert\GreaterThan) {
65 1
            $doc->setInclusiveMin(false);
66
        }
67 7
    }
68
69
    /**
70
     * @param CollectionDoc $doc
71
     * @param Constraint    $constraint
72
     */
73 11
    private function appendCollectionDoc(CollectionDoc $doc, Constraint $constraint) : void
74
    {
75 11
        $min = $max = null;
76 11
        if ($constraint instanceof Assert\Choice || $constraint instanceof Assert\Count) {
77 6
            $min = $constraint->min;
78 6
            $max = $constraint->max;
79 5
        } elseif ($constraint instanceof Assert\NotBlank && null === $doc->getMinItem()) {
80
            // Not blank so minimum 1 item
81 1
            $min = 1;
82
        } /* Documentation does not mention array, counter to NotBlank constraint
83
         elseif ($constraint instanceof Assert\Blank && null === $doc->getMaxItem()) {
84
            // Blank so maximum 0 item
85
            $max = 0;
86
        }*/
87
88 11
        $this->setMinMaxItemIfNotNull($doc, $min, $max);
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
        $min = $max = null;
99 7
        if ($constraint instanceof Assert\Range) {
100 3
            $min = $constraint->min;
101 3
            $max = $constraint->max;
102 4
        } elseif ($constraint instanceof Assert\LessThanOrEqual
103 4
            || $constraint instanceof Assert\LessThan
104
        ) {
105 2
            $max = $constraint->value;
106 2
        } elseif ($constraint instanceof Assert\GreaterThanOrEqual
107 2
            || $constraint instanceof Assert\GreaterThan
108
        ) {
109 2
            $min = $constraint->value;
110
        }
111
112 7
        $this->setMinMaxIfNotNull($doc, $min, $max);
113 7
    }
114
115
    /**
116
     * @param CollectionDoc $doc
117
     * @param Constraint    $constraint
118
     */
119 11
    private function appendLessGreaterThanMinMaxItem(CollectionDoc $doc, Constraint $constraint): void
120
    {
121 11
        $min = $max = null;
122 11
        $gtConstraintList = [Assert\GreaterThan::class, Assert\GreaterThanOrEqual::class];
123 11
        $ltConstraintList = [Assert\LessThan::class, Assert\LessThanOrEqual::class];
124 11
        if (null !== ($match = $this->getMatchingClassNameIn($constraint, $gtConstraintList))) {
125 2
            $min = ($match === Assert\GreaterThanOrEqual::class)
126 1
                ? $constraint->value
127 2
                : $constraint->value + 1;
128 9
        } elseif (null !== ($match = $this->getMatchingClassNameIn($constraint, $ltConstraintList))) {
129 2
            $max = ($match === Assert\LessThanOrEqual::class)
130 1
                ? $constraint->value
131 2
                : $constraint->value - 1
132
            ;
133
        }
134
135 11
        $this->setMinMaxItemIfNotNull($doc, $min, $max);
136 11
    }
137
138
    /**
139
     * @param StringDoc      $doc
140
     * @param null|int|mixed $min
141
     * @param null|int|mixed $max
142
     */
143 5
    private function setMinMaxLengthIfNotNull(StringDoc $doc, $min, $max): void
144
    {
145 5
        if (null !== $min) {
146 3
            $doc->setMinLength((int)$min);
147
        }
148 5
        if (null !== $max) {
149 3
            $doc->setMaxLength((int)$max);
150
        }
151 5
    }
152
153
    /**
154
     * @param CollectionDoc  $doc
155
     * @param null|int|mixed $min
156
     * @param null|int|mixed $max
157
     */
158 11
    private function setMinMaxItemIfNotNull(CollectionDoc $doc, $min, $max): void
159
    {
160 11
        if (null !== $min) {
161 7
            $doc->setMinItem((int) $min);
162
        }
163 11
        if (null !== $max) {
164 6
            $doc->setMaxItem((int) $max);
165
        }
166 11
    }
167
168
    /**
169
     * @param NumberDoc      $doc
170
     * @param null|int|mixed $min
171
     * @param null|int|mixed $max
172
     */
173 7
    private function setMinMaxIfNotNull(NumberDoc $doc, $min, $max): void
174
    {
175 7
        if (null !== $min) {
176 4
            $doc->setMin($min);
177
        }
178 7
        if (null !== $max) {
179 4
            $doc->setMax($max);
180
        }
181 7
    }
182
}
183