1313 */
1414class MinMaxHelper
1515{
16+ use ClassComparatorTrait;
17+
1618 /**
1719 * @param TypeDoc $doc
1820 * @param Constraint $constraint
@@ -34,20 +36,19 @@ public function append(TypeDoc $doc, Constraint $constraint) : void
3436 */
3537 private function appendStringDoc (StringDoc $ doc , Constraint $ constraint ) : void
3638 {
39+ $ min = $ max = null ;
3740 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 ;
4443 } elseif ($ constraint instanceof Assert \NotBlank && null === $ doc ->getMinLength ()) {
4544 // Not blank so minimum 1 character
46- $ doc -> setMinLength ( 1 ) ;
45+ $ min = 1 ;
4746 } elseif ($ constraint instanceof Assert \Blank && null === $ doc ->getMaxLength ()) {
4847 // Blank so maximum 0 character
49- $ doc -> setMaxLength ( 0 ) ;
48+ $ max = 0 ;
5049 }
50+
51+ $ this ->setMinMaxLengthIfNotNull ($ doc , $ min , $ max );
5152 }
5253
5354 /**
@@ -71,21 +72,20 @@ private function appendNumberDoc(NumberDoc $doc, Constraint $constraint) : void
7172 */
7273 private function appendCollectionDoc (CollectionDoc $ doc , Constraint $ constraint ) : void
7374 {
75+ $ min = $ max = null ;
7476 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 ;
8179 } elseif ($ constraint instanceof Assert \NotBlank && null === $ doc ->getMinItem ()) {
8280 // Not blank so minimum 1 item
83- $ doc -> setMinItem ( 1 ) ;
81+ $ min = 1 ;
8482 } /* Documentation does not mention array, counter to NotBlank constraint
8583 elseif ($constraint instanceof Assert\Blank && null === $doc->getMaxItem()) {
8684 // Blank so maximum 0 item
87- $doc->setMaxItem(0) ;
85+ $max = 0 ;
8886 }*/
87+
88+ $ this ->setMinMaxItemIfNotNull ($ doc , $ min , $ max );
8989 $ this ->appendLessGreaterThanMinMaxItem ($ doc , $ constraint );
9090 }
9191
@@ -95,22 +95,21 @@ private function appendCollectionDoc(CollectionDoc $doc, Constraint $constraint)
9595 */
9696 private function appendNumberMinMax (NumberDoc $ doc , Constraint $ constraint ) : void
9797 {
98+ $ min = $ max = null ;
9899 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 ;
105102 } elseif ($ constraint instanceof Assert \LessThanOrEqual
106103 || $ constraint instanceof Assert \LessThan
107104 ) {
108- $ doc -> setMax ( $ constraint ->value ) ;
105+ $ max = $ constraint ->value ;
109106 } elseif ($ constraint instanceof Assert \GreaterThanOrEqual
110107 || $ constraint instanceof Assert \GreaterThan
111108 ) {
112- $ doc -> setMin ( $ constraint ->value ) ;
109+ $ min = $ constraint ->value ;
113110 }
111+
112+ $ this ->setMinMaxIfNotNull ($ doc , $ min , $ max );
114113 }
115114
116115 /**
@@ -119,18 +118,65 @@ private function appendNumberMinMax(NumberDoc $doc, Constraint $constraint) : vo
119118 */
120119 private function appendLessGreaterThanMinMaxItem (CollectionDoc $ doc , Constraint $ constraint ): void
121120 {
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 );
134180 }
135181 }
136182}
0 commit comments