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,19 @@ 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
+ $ this ->setMinMaxItemIfNotNull ($ doc , $ min , $ max );
89
88
$ this ->appendLessGreaterThanMinMaxItem ($ doc , $ constraint );
90
89
}
91
90
@@ -95,22 +94,21 @@ private function appendCollectionDoc(CollectionDoc $doc, Constraint $constraint)
95
94
*/
96
95
private function appendNumberMinMax (NumberDoc $ doc , Constraint $ constraint ) : void
97
96
{
97
+ $ min = $ max = null ;
98
98
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
- }
99
+ $ min = $ constraint ->min ;
100
+ $ max = $ constraint ->max ;
105
101
} elseif ($ constraint instanceof Assert \LessThanOrEqual
106
102
|| $ constraint instanceof Assert \LessThan
107
103
) {
108
- $ doc -> setMax ( $ constraint ->value ) ;
104
+ $ max = $ constraint ->value ;
109
105
} elseif ($ constraint instanceof Assert \GreaterThanOrEqual
110
106
|| $ constraint instanceof Assert \GreaterThan
111
107
) {
112
- $ doc -> setMin ( $ constraint ->value ) ;
108
+ $ min = $ constraint ->value ;
113
109
}
110
+
111
+ $ this ->setMinMaxIfNotNull ($ doc , $ min , $ max );
114
112
}
115
113
116
114
/**
@@ -119,18 +117,65 @@ private function appendNumberMinMax(NumberDoc $doc, Constraint $constraint) : vo
119
117
*/
120
118
private function appendLessGreaterThanMinMaxItem (CollectionDoc $ doc , Constraint $ constraint ): void
121
119
{
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
- );
120
+ $ min = $ max = null ;
121
+ $ gtConstraintList = [Assert \GreaterThan::class, Assert \GreaterThanOrEqual::class];
122
+ $ ltConstraintList = [Assert \LessThan::class, Assert \LessThanOrEqual::class];
123
+ if (null !== ($ match = $ this ->getMatchingClassNameIn ($ constraint , $ gtConstraintList ))) {
124
+ $ min = ($ match === Assert \GreaterThanOrEqual::class)
125
+ ? $ constraint ->value
126
+ : $ constraint ->value + 1 ;
127
+ } elseif (null !== ($ match = $ this ->getMatchingClassNameIn ($ constraint , $ ltConstraintList ))) {
128
+ $ max = ($ match === Assert \LessThanOrEqual::class)
129
+ ? $ constraint ->value
130
+ : $ constraint ->value - 1
131
+ ;
132
+ }
133
+
134
+ $ this ->setMinMaxItemIfNotNull ($ doc , $ min , $ max );
135
+ }
136
+
137
+ /**
138
+ * @param StringDoc $doc
139
+ * @param null|int|mixed $min
140
+ * @param null|int|mixed $max
141
+ */
142
+ private function setMinMaxLengthIfNotNull (StringDoc $ doc , $ min , $ max ): void
143
+ {
144
+ if (null !== $ min ) {
145
+ $ doc ->setMinLength ((int )$ min );
146
+ }
147
+ if (null !== $ max ) {
148
+ $ doc ->setMaxLength ((int )$ max );
149
+ }
150
+ }
151
+
152
+ /**
153
+ * @param CollectionDoc $doc
154
+ * @param null|int|mixed $min
155
+ * @param null|int|mixed $max
156
+ */
157
+ private function setMinMaxItemIfNotNull (CollectionDoc $ doc , $ min , $ max ): void
158
+ {
159
+ if (null !== $ min ) {
160
+ $ doc ->setMinItem ((int ) $ min );
161
+ }
162
+ if (null !== $ max ) {
163
+ $ doc ->setMaxItem ((int ) $ max );
164
+ }
165
+ }
166
+
167
+ /**
168
+ * @param NumberDoc $doc
169
+ * @param null|int|mixed $min
170
+ * @param null|int|mixed $max
171
+ */
172
+ private function setMinMaxIfNotNull (NumberDoc $ doc , $ min , $ max ): void
173
+ {
174
+ if (null !== $ min ) {
175
+ $ doc ->setMin ($ min );
176
+ }
177
+ if (null !== $ max ) {
178
+ $ doc ->setMax ($ max );
134
179
}
135
180
}
136
181
}
0 commit comments