Passed
Pull Request — feature/fix (#19)
by Yo
01:35
created

MinMaxHelper::setMinMaxItemIfNotNull()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 4
nop 3
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 3
rs 10
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 11
        $this->setMinMaxItemIfNotNull($doc, $min, $max);
88 11
        $this->appendLessGreaterThanMinMaxItem($doc, $constraint);
89 11
    }
90
91
    /**
92
     * @param NumberDoc $doc
93
     * @param Constraint $constraint
94
     */
95 7
    private function appendNumberMinMax(NumberDoc $doc, Constraint $constraint) : void
96
    {
97 7
        $min = $max = null;
98 7
        if ($constraint instanceof Assert\Range) {
99 3
            $min = $constraint->min;
100 3
            $max = $constraint->max;
101 4
        } elseif ($constraint instanceof Assert\LessThanOrEqual
102 4
            || $constraint instanceof Assert\LessThan
103
        ) {
104 2
            $max = $constraint->value;
105 2
        } elseif ($constraint instanceof Assert\GreaterThanOrEqual
106 2
            || $constraint instanceof Assert\GreaterThan
107
        ) {
108 2
            $min = $constraint->value;
109
        }
110
111 7
        $this->setMinMaxIfNotNull($doc, $min, $max);
112 7
    }
113
114
    /**
115
     * @param CollectionDoc $doc
116
     * @param Constraint    $constraint
117
     */
118 11
    private function appendLessGreaterThanMinMaxItem(CollectionDoc $doc, Constraint $constraint): void
119
    {
120 11
        $min = $max = null;
121 11
        $gtConstraintList = [Assert\GreaterThan::class, Assert\GreaterThanOrEqual::class];
122 11
        $ltConstraintList = [Assert\LessThan::class, Assert\LessThanOrEqual::class];
123 11
        if (null !== ($match = $this->getMatchingClassNameIn($constraint, $gtConstraintList))) {
124 2
            $min = ($match === Assert\GreaterThanOrEqual::class)
125 1
                ? $constraint->value
126 2
                : $constraint->value + 1;
127 9
        } elseif (null !== ($match = $this->getMatchingClassNameIn($constraint, $ltConstraintList))) {
128 2
            $max = ($match === Assert\LessThanOrEqual::class)
129 1
                ? $constraint->value
130 2
                : $constraint->value - 1
131
            ;
132
        }
133
134 11
        $this->setMinMaxItemIfNotNull($doc, $min, $max);
135 11
    }
136
137
    /**
138
     * @param StringDoc      $doc
139
     * @param null|int|mixed $min
140
     * @param null|int|mixed $max
141
     */
142 5
    private function setMinMaxLengthIfNotNull(StringDoc $doc, $min, $max): void
143
    {
144 5
        if (null !== $min) {
145 3
            $doc->setMinLength((int)$min);
146
        }
147 5
        if (null !== $max) {
148 3
            $doc->setMaxLength((int)$max);
149
        }
150 5
    }
151
152
    /**
153
     * @param CollectionDoc  $doc
154
     * @param null|int|mixed $min
155
     * @param null|int|mixed $max
156
     */
157 11
    private function setMinMaxItemIfNotNull(CollectionDoc $doc, $min, $max): void
158
    {
159 11
        if (null !== $min) {
160 7
            $doc->setMinItem((int) $min);
161
        }
162 11
        if (null !== $max) {
163 6
            $doc->setMaxItem((int) $max);
164
        }
165 11
    }
166
167
    /**
168
     * @param NumberDoc       $doc
169
     * @param null|int||mixed $min
0 ignored issues
show
Documentation Bug introduced by
The doc comment null|int||mixed at position 4 could not be parsed: Unknown type name '|' at position 4 in null|int||mixed.
Loading history...
170
     * @param null|int||mixed $max
171
     */
172 7
    private function setMinMaxIfNotNull(NumberDoc $doc, $min, $max): void
173
    {
174 7
        if (null !== $min) {
175 4
            $doc->setMin($min);
176
        }
177 7
        if (null !== $max) {
178 4
            $doc->setMax($max);
179
        }
180 7
    }
181
}
182