Skip to content

Commit 6b8a9a1

Browse files
committed
Improve
1 parent b0ae259 commit 6b8a9a1

File tree

3 files changed

+138
-123
lines changed

3 files changed

+138
-123
lines changed

src/App/Helper/DocTypeHelper.php

Lines changed: 93 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -101,74 +101,75 @@ private function guessTypeFromConstraintList(array $constraintList)
101101
*/
102102
private function guessTypeFromConstraint(Constraint $constraint)
103103
{
104-
// Try to guess primary types
105-
switch (true) {
106-
case $constraint instanceof Assert\Existence:
107-
return $this->guess($constraint->constraints);
108-
case $constraint instanceof Assert\Length:// << Applied on string only
109-
case $constraint instanceof Assert\Date: // << validator expect a string with specific format
110-
case $constraint instanceof Assert\Time: // << validator expect a string with specific format
111-
case $constraint instanceof Assert\Bic:
112-
case $constraint instanceof Assert\CardScheme:
113-
case $constraint instanceof Assert\Country:
114-
case $constraint instanceof Assert\Currency:
115-
case $constraint instanceof Assert\Email:
116-
case $constraint instanceof Assert\File:
117-
case $constraint instanceof Assert\Iban:
118-
case $constraint instanceof Assert\Ip:
119-
case $constraint instanceof Assert\Isbn:
120-
case $constraint instanceof Assert\Issn:
121-
case $constraint instanceof Assert\Language:
122-
case $constraint instanceof Assert\Locale:
123-
case $constraint instanceof Assert\Luhn:
124-
case $constraint instanceof Assert\Regex:
125-
case $constraint instanceof Assert\Url:
126-
case $constraint instanceof Assert\Uuid:
127-
return new StringDoc();
128-
case $constraint instanceof Assert\DateTime:
129-
if ('U' === $constraint->format) {
130-
return new ScalarDoc();// Don't know if value will be an number as string or as integer
131-
}
104+
static $stringConstraintClassList = [
105+
Assert\Length::class, // << Applied on string only
106+
Assert\Date::class, // << validator expect a string with specific format
107+
Assert\Time::class, // << validator expect a string with specific format
108+
Assert\Bic::class,
109+
Assert\CardScheme::class,
110+
Assert\Country::class,
111+
Assert\Currency::class,
112+
Assert\Email::class,
113+
Assert\File::class,
114+
Assert\Iban::class,
115+
Assert\Ip::class,
116+
Assert\Isbn::class,
117+
Assert\Issn::class,
118+
Assert\Language::class,
119+
Assert\Locale::class,
120+
Assert\Luhn::class,
121+
Assert\Regex::class,
122+
Assert\Url::class,
123+
Assert\Uuid::class,
124+
];
125+
static $booleanConstraintClassList = [
126+
Assert\IsTrue::class,
127+
Assert\IsFalse::class,
128+
];
129+
$constraintClass = get_class($constraint);
132130

133-
return new StringDoc();
134-
case $constraint instanceof Assert\IsTrue:
135-
case $constraint instanceof Assert\IsFalse:
136-
return new BooleanDoc();
137-
case $constraint instanceof Assert\Collection:
138-
// If only integer => array, else object
139-
$integerKeyList = array_filter(array_keys($constraint->fields), 'is_int');
140-
if (count($constraint->fields) === count($integerKeyList)) {
141-
return new ArrayDoc();
142-
}
131+
// Try to guess primary types
132+
if (in_array($constraintClass, $stringConstraintClassList)) {
133+
return new StringDoc();
134+
} elseif (in_array($constraintClass, $booleanConstraintClassList)) {
135+
return new BooleanDoc();
136+
} elseif ($constraint instanceof Assert\DateTime) {
137+
if ('U' === $constraint->format) {
138+
return new ScalarDoc();// Don't know if value will be an number as string or as integer
139+
}
143140

144-
return new ObjectDoc();
145-
case $constraint instanceof Assert\Choice
146-
&& true === $constraint->multiple: // << expect an array multiple choices
147-
case $constraint instanceof Assert\All: // << Applied only on array
141+
return new StringDoc();
142+
} elseif ($constraint instanceof Assert\Collection) {
143+
// If only integer => array, else object
144+
$integerKeyList = array_filter(array_keys($constraint->fields), 'is_int');
145+
if (count($constraint->fields) === count($integerKeyList)) {
148146
return new ArrayDoc();
147+
}
148+
149+
return new ObjectDoc();
150+
} elseif (Assert\All::class === $constraintClass // << Applied only on array
151+
|| ($constraint instanceof Assert\Choice
152+
&& true === $constraint->multiple // << expect an array multiple choices
153+
)
154+
) {
155+
return new ArrayDoc();
149156
}
150157

151158
// If primary type is still not defined
152-
switch (true) {
153-
case $constraint instanceof Assert\Range:
154-
if ((null !== $constraint->min && is_float($constraint->min))
155-
|| (null !== $constraint->max && is_float($constraint->max))
156-
) {
157-
return new FloatDoc();
158-
}
159-
160-
return new NumberDoc();
161-
case $constraint instanceof Assert\GreaterThan:
162-
case $constraint instanceof Assert\GreaterThanOrEqual:
163-
case $constraint instanceof Assert\LessThan:
164-
case $constraint instanceof Assert\LessThanOrEqual:
165-
if (null !== $constraint->value && is_float($constraint->value)) {
166-
return new FloatDoc();
167-
}
168-
169-
return new NumberDoc();
170-
case $constraint instanceof Assert\Count:
171-
return new CollectionDoc();
159+
static $numberOrFloatConstraintClassList = [
160+
Assert\GreaterThan::class,
161+
Assert\GreaterThanOrEqual::class,
162+
Assert\LessThan::class,
163+
Assert\LessThanOrEqual::class,
164+
];
165+
if ($constraint instanceof Assert\Range) {
166+
return $this->floatOrNumber([$constraint->min, $constraint->max]);
167+
} elseif (in_array($constraintClass, $numberOrFloatConstraintClassList)) {
168+
return $this->floatOrNumber([$constraint->value]);
169+
} elseif (Assert\Count::class == $constraintClass) {
170+
return new CollectionDoc();
171+
} elseif ($constraint instanceof Assert\Existence) {
172+
return $this->guess($constraint->constraints);
172173
}
173174

174175
return null;
@@ -181,21 +182,20 @@ private function guessTypeFromConstraint(Constraint $constraint)
181182
*/
182183
private function getDocFromType(string $type)
183184
{
184-
switch (true) {
185-
case 'scalar' === $type:
186-
return new ScalarDoc();
187-
case 'string' === $type:
188-
return new StringDoc();
189-
case 'bool' === $type || 'boolean' === $type:
190-
return new BooleanDoc();
191-
case 'int' === $type || 'integer' === $type:
192-
return new IntegerDoc();
193-
case in_array($type, ['float', 'long', 'double', 'real', 'numeric']):
194-
return new FloatDoc();
195-
case 'array' === $type:
196-
return new ArrayDoc();
197-
case 'object' === $type:
198-
return new ObjectDoc();
185+
if ('scalar' === $type) {
186+
return new ScalarDoc();
187+
} elseif ('string' === $type) {
188+
return new StringDoc();
189+
} elseif ('bool' === $type || 'boolean' === $type) {
190+
return new BooleanDoc();
191+
} elseif ('int' === $type || 'integer' === $type) {
192+
return new IntegerDoc();
193+
} elseif (in_array($type, ['float', 'long', 'double', 'real', 'numeric'])) {
194+
return new FloatDoc();
195+
} elseif ('array' === $type) {
196+
return new ArrayDoc();
197+
} elseif ('object' === $type) {
198+
return new ObjectDoc();
199199
}
200200

201201
return null;
@@ -216,4 +216,20 @@ private function isAbstractType(TypeDoc $doc) : bool
216216
|| ScalarDoc::class === $class
217217
;
218218
}
219+
220+
/**
221+
* @param array $basedOnList
222+
*
223+
* @return FloatDoc|NumberDoc
224+
*/
225+
private function floatOrNumber(array $basedOnList)
226+
{
227+
foreach ($basedOnList as $value) {
228+
if (null !== $value && is_float($value)) {
229+
return new FloatDoc();
230+
}
231+
}
232+
233+
return new NumberDoc();
234+
}
219235
}

src/App/Helper/MinMaxHelper.php

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,27 @@ private function appendStringDoc(StringDoc $doc, Constraint $constraint)
5050
*/
5151
private function appendNumberDoc(NumberDoc $doc, Constraint $constraint)
5252
{
53-
switch (true) {
54-
case $constraint instanceof Assert\Range:
55-
if (null !== $constraint->min) {
56-
$doc->setMin($constraint->min);
57-
}
58-
if (null !== $constraint->max) {
59-
$doc->setMax($constraint->max);
60-
}
61-
break;
62-
case $constraint instanceof Assert\LessThanOrEqual:
63-
case $constraint instanceof Assert\LessThan:
64-
$doc->setMax($constraint->value);
65-
break;
66-
case $constraint instanceof Assert\GreaterThanOrEqual:
67-
case $constraint instanceof Assert\GreaterThan:
68-
$doc->setMin($constraint->value);
69-
break;
70-
}
71-
switch (true) {
72-
case $constraint instanceof Assert\GreaterThan:
73-
$doc->setInclusiveMin(false);
74-
break;
75-
case $constraint instanceof Assert\LessThan:
53+
if ($constraint instanceof Assert\Range) {
54+
if (null !== $constraint->min) {
55+
$doc->setMin($constraint->min);
56+
}
57+
if (null !== $constraint->max) {
58+
$doc->setMax($constraint->max);
59+
}
60+
} elseif ($constraint instanceof Assert\LessThanOrEqual
61+
|| $constraint instanceof Assert\LessThan
62+
) {
63+
$doc->setMax($constraint->value);
64+
if ($constraint instanceof Assert\LessThan) {
7665
$doc->setInclusiveMax(false);
77-
break;
66+
}
67+
} elseif ($constraint instanceof Assert\GreaterThanOrEqual
68+
|| $constraint instanceof Assert\GreaterThan
69+
) {
70+
$doc->setMin($constraint->value);
71+
if ($constraint instanceof Assert\GreaterThan) {
72+
$doc->setInclusiveMin(false);
73+
}
7874
}
7975
}
8076

src/App/Helper/StringDocHelper.php

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,31 @@ public function append(TypeDoc $doc, Constraint $constraint)
2222
return;
2323
}
2424

25-
switch (true) {
26-
case $constraint instanceof Assert\Bic:
27-
case $constraint instanceof Assert\CardScheme:
28-
case $constraint instanceof Assert\Country:
29-
case $constraint instanceof Assert\Currency:
30-
case $constraint instanceof Assert\Date:
31-
case $constraint instanceof Assert\DateTime:
32-
case $constraint instanceof Assert\Email:
33-
case $constraint instanceof Assert\File:
34-
case $constraint instanceof Assert\Iban:
35-
case $constraint instanceof Assert\Ip:
36-
case $constraint instanceof Assert\Isbn:
37-
case $constraint instanceof Assert\Issn:
38-
case $constraint instanceof Assert\Language:
39-
case $constraint instanceof Assert\Locale:
40-
case $constraint instanceof Assert\Luhn:
41-
case $constraint instanceof Assert\Regex:
42-
case $constraint instanceof Assert\Time:
43-
case $constraint instanceof Assert\Url:
44-
case $constraint instanceof Assert\Uuid:
45-
$doc->setFormat(lcfirst((new \ReflectionClass($constraint))->getShortName()));
46-
break;
25+
$constraintClass = get_class($constraint);
26+
$constraintForFormatList = [
27+
Assert\Bic::class,
28+
Assert\CardScheme::class,
29+
Assert\Country::class,
30+
Assert\Currency::class,
31+
Assert\Date::class,
32+
Assert\DateTime::class,
33+
Assert\Email::class,
34+
Assert\File::class,
35+
Assert\Iban::class,
36+
Assert\Ip::class,
37+
Assert\Isbn::class,
38+
Assert\Issn::class,
39+
Assert\Language::class,
40+
Assert\Locale::class,
41+
Assert\Luhn::class,
42+
Assert\Regex::class,
43+
Assert\Time::class,
44+
Assert\Url::class,
45+
Assert\Uuid::class,
46+
];
47+
48+
if (in_array($constraintClass, $constraintForFormatList)) {
49+
$doc->setFormat(lcfirst((new \ReflectionClass($constraint))->getShortName()));
4750
}
4851
}
4952
}

0 commit comments

Comments
 (0)