13
13
*/
14
14
class MinMaxHelper
15
15
{
16
+ use ClassComparatorTrait;
17
+
16
18
/**
17
19
* @param TypeDoc $doc
18
20
* @param Constraint $constraint
@@ -34,20 +36,19 @@ public function append(TypeDoc $doc, Constraint $constraint) : void
34
36
*/
35
37
private function appendStringDoc (StringDoc $ doc , Constraint $ constraint ) : void
36
38
{
39
+ $ min = $ max = null ;
37
40
if ($ constraint instanceof Assert \Length) {
38
- if (null !== $ constraint ->min ) {
39
- $ doc ->setMinLength ((int ) $ constraint ->min );
40
- }
41
- if (null !== $ constraint ->max ) {
42
- $ doc ->setMaxLength ((int ) $ constraint ->max );
43
- }
41
+ $ min = $ constraint ->min ;
42
+ $ max = $ constraint ->max ;
44
43
} elseif ($ constraint instanceof Assert \NotBlank && null === $ doc ->getMinLength ()) {
45
44
// Not blank so minimum 1 character
46
- $ doc -> setMinLength ( 1 ) ;
45
+ $ min = 1 ;
47
46
} elseif ($ constraint instanceof Assert \Blank && null === $ doc ->getMaxLength ()) {
48
47
// Blank so maximum 0 character
49
- $ doc -> setMaxLength ( 0 ) ;
48
+ $ max = 0 ;
50
49
}
50
+
51
+ $ this ->setMinMaxLengthIfNotNull ($ doc , $ min , $ max );
51
52
}
52
53
53
54
/**
@@ -71,21 +72,20 @@ private function appendNumberDoc(NumberDoc $doc, Constraint $constraint) : void
71
72
*/
72
73
private function appendCollectionDoc (CollectionDoc $ doc , Constraint $ constraint ) : void
73
74
{
75
+ $ min = $ max = null ;
74
76
if ($ constraint instanceof Assert \Choice || $ constraint instanceof Assert \Count) {
75
- if (null !== $ constraint ->min ) {
76
- $ doc ->setMinItem ((int ) $ constraint ->min );
77
- }
78
- if (null !== $ constraint ->max ) {
79
- $ doc ->setMaxItem ((int ) $ constraint ->max );
80
- }
77
+ $ min = $ constraint ->min ;
78
+ $ max = $ constraint ->max ;
81
79
} elseif ($ constraint instanceof Assert \NotBlank && null === $ doc ->getMinItem ()) {
82
80
// Not blank so minimum 1 item
83
- $ doc -> setMinItem ( 1 ) ;
81
+ $ min = 1 ;
84
82
} /* Documentation does not mention array, counter to NotBlank constraint
85
83
elseif ($constraint instanceof Assert\Blank && null === $doc->getMaxItem()) {
86
84
// Blank so maximum 0 item
87
- $doc->setMaxItem(0) ;
85
+ $max = 0 ;
88
86
}*/
87
+
88
+ $ this ->setMinMaxItemIfNotNull ($ doc , $ min , $ max );
89
89
$ this ->appendLessGreaterThanMinMaxItem ($ doc , $ constraint );
90
90
}
91
91
@@ -95,22 +95,21 @@ private function appendCollectionDoc(CollectionDoc $doc, Constraint $constraint)
95
95
*/
96
96
private function appendNumberMinMax (NumberDoc $ doc , Constraint $ constraint ) : void
97
97
{
98
+ $ min = $ max = null ;
98
99
if ($ constraint instanceof Assert \Range) {
99
- if (null !== $ constraint ->min ) {
100
- $ doc ->setMin ($ constraint ->min );
101
- }
102
- if (null !== $ constraint ->max ) {
103
- $ doc ->setMax ($ constraint ->max );
104
- }
100
+ $ min = $ constraint ->min ;
101
+ $ max = $ constraint ->max ;
105
102
} elseif ($ constraint instanceof Assert \LessThanOrEqual
106
103
|| $ constraint instanceof Assert \LessThan
107
104
) {
108
- $ doc -> setMax ( $ constraint ->value ) ;
105
+ $ max = $ constraint ->value ;
109
106
} elseif ($ constraint instanceof Assert \GreaterThanOrEqual
110
107
|| $ constraint instanceof Assert \GreaterThan
111
108
) {
112
- $ doc -> setMin ( $ constraint ->value ) ;
109
+ $ min = $ constraint ->value ;
113
110
}
111
+
112
+ $ this ->setMinMaxIfNotNull ($ doc , $ min , $ max );
114
113
}
115
114
116
115
/**
@@ -119,18 +118,65 @@ private function appendNumberMinMax(NumberDoc $doc, Constraint $constraint) : vo
119
118
*/
120
119
private function appendLessGreaterThanMinMaxItem (CollectionDoc $ doc , Constraint $ constraint ): void
121
120
{
122
- if ($ constraint instanceof Assert \GreaterThan || $ constraint instanceof Assert \GreaterThanOrEqual) {
123
- $ doc ->setMinItem (
124
- $ constraint instanceof Assert \GreaterThanOrEqual
125
- ? $ constraint ->value
126
- : $ constraint ->value + 1
127
- );
128
- } elseif ($ constraint instanceof Assert \LessThan || $ constraint instanceof Assert \LessThanOrEqual) {
129
- $ doc ->setMaxItem (
130
- $ constraint instanceof Assert \LessThanOrEqual
131
- ? $ constraint ->value
132
- : $ constraint ->value - 1
133
- );
121
+ $ min = $ max = null ;
122
+ $ gtConstraintList = [Assert \GreaterThan::class, Assert \GreaterThanOrEqual::class];
123
+ $ ltConstraintList = [Assert \LessThan::class, Assert \LessThanOrEqual::class];
124
+ if (null !== ($ match = $ this ->getMatchingClassNameIn ($ constraint , $ gtConstraintList ))) {
125
+ $ min = ($ match === Assert \GreaterThanOrEqual::class)
126
+ ? $ constraint ->value
127
+ : $ constraint ->value + 1 ;
128
+ } elseif (null !== ($ match = $ this ->getMatchingClassNameIn ($ constraint , $ ltConstraintList ))) {
129
+ $ max = ($ match === Assert \LessThanOrEqual::class)
130
+ ? $ constraint ->value
131
+ : $ constraint ->value - 1
132
+ ;
133
+ }
134
+
135
+ $ this ->setMinMaxItemIfNotNull ($ doc , $ min , $ max );
136
+ }
137
+
138
+ /**
139
+ * @param StringDoc $doc
140
+ * @param null|int|mixed $min
141
+ * @param null|int|mixed $max
142
+ */
143
+ private function setMinMaxLengthIfNotNull (StringDoc $ doc , $ min , $ max ): void
144
+ {
145
+ if (null !== $ min ) {
146
+ $ doc ->setMinLength ((int )$ min );
147
+ }
148
+ if (null !== $ max ) {
149
+ $ doc ->setMaxLength ((int )$ max );
150
+ }
151
+ }
152
+
153
+ /**
154
+ * @param CollectionDoc $doc
155
+ * @param null|int|mixed $min
156
+ * @param null|int|mixed $max
157
+ */
158
+ private function setMinMaxItemIfNotNull (CollectionDoc $ doc , $ min , $ max ): void
159
+ {
160
+ if (null !== $ min ) {
161
+ $ doc ->setMinItem ((int ) $ min );
162
+ }
163
+ if (null !== $ max ) {
164
+ $ doc ->setMaxItem ((int ) $ max );
165
+ }
166
+ }
167
+
168
+ /**
169
+ * @param NumberDoc $doc
170
+ * @param null|int|mixed $min
171
+ * @param null|int|mixed $max
172
+ */
173
+ private function setMinMaxIfNotNull (NumberDoc $ doc , $ min , $ max ): void
174
+ {
175
+ if (null !== $ min ) {
176
+ $ doc ->setMin ($ min );
177
+ }
178
+ if (null !== $ max ) {
179
+ $ doc ->setMax ($ max );
134
180
}
135
181
}
136
182
}
0 commit comments