Skip to content

Commit ac033d7

Browse files
authored
Improve (#17)
1 parent cd7aa75 commit ac033d7

File tree

2 files changed

+52
-47
lines changed

2 files changed

+52
-47
lines changed

src/App/Helper/MinMaxHelper.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,7 @@ private function appendNumberDoc(NumberDoc $doc, Constraint $constraint) : void
7171
*/
7272
private function appendCollectionDoc(CollectionDoc $doc, Constraint $constraint) : void
7373
{
74-
if ($constraint instanceof Assert\Choice) {
75-
if (null !== $constraint->min) {
76-
$doc->setMinItem((int) $constraint->min);
77-
}
78-
if (null !== $constraint->max) {
79-
$doc->setMaxItem((int) $constraint->max);
80-
}
81-
} elseif ($constraint instanceof Assert\Count) {
74+
if ($constraint instanceof Assert\Choice || $constraint instanceof Assert\Count) {
8275
if (null !== $constraint->min) {
8376
$doc->setMinItem((int) $constraint->min);
8477
}

src/Infra/Transformer/ConstraintToParamsDocTransformer.php

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,6 @@ public function transformList(array $constraintList) : TypeDoc
8181
*/
8282
private function appendToDoc(TypeDoc $doc, Constraint $constraint) : void
8383
{
84-
static $notNullConstraintList = [
85-
Assert\NotNull::class,
86-
Assert\IsTrue::class, // If it is true, it cannot be null ...
87-
Assert\IsFalse::class, // If it is false, it cannot be null ...
88-
// If should be identical to something, it cannot be null (but can be identical to null)
89-
Assert\IdenticalTo::class,
90-
];
9184
if ($constraint instanceof Assert\Callback) {
9285
$callbackResult = call_user_func($constraint->callback);
9386
$callbackResultList = is_array($callbackResult) ? $callbackResult : [$callbackResult];
@@ -97,38 +90,7 @@ private function appendToDoc(TypeDoc $doc, Constraint $constraint) : void
9790
} elseif ($doc instanceof ArrayDoc && $constraint instanceof Assert\All) {
9891
$this->appendAllConstraintToDoc($doc, $constraint);
9992
} else {
100-
$this->stringDocHelper->append($doc, $constraint);
101-
$this->appendCollectionDoc($doc, $constraint);
102-
103-
$this->minMaxHelper->append($doc, $constraint);
104-
$this->appendValidItemListDoc($doc, $constraint);
105-
106-
if ($constraint instanceof Assert\Existence) {
107-
$doc->setRequired($constraint instanceof Assert\Required);
108-
foreach ($constraint->constraints as $subConstraint) {
109-
$this->appendToDoc($doc, $subConstraint);
110-
}
111-
} elseif ($this->isInstanceOfOneClassIn($constraint, $notNullConstraintList)) {
112-
$doc->setNullable(
113-
($constraint instanceof Assert\IdenticalTo)
114-
? is_null($constraint->value)
115-
: false
116-
);
117-
$defaultValue = $exampleValue = null;
118-
switch (true) {
119-
case $constraint instanceof Assert\IsTrue:
120-
$defaultValue = $exampleValue = true;
121-
break;
122-
case $constraint instanceof Assert\IsFalse:
123-
$defaultValue = $exampleValue = false;
124-
break;
125-
case $constraint instanceof Assert\IdenticalTo:
126-
$defaultValue = $exampleValue = $constraint->value;
127-
break;
128-
}
129-
$doc->setDefault($doc->getDefault() ?? $defaultValue);
130-
$doc->setExample($doc->getExample() ?? $exampleValue);
131-
}
93+
$this->basicAppendToDoc($doc, $constraint);
13294
}
13395
// /!\ Payload doc will override values even if already defined
13496
$this->constraintPayloadDocHelper->appendPayloadDoc($doc, $constraint);
@@ -236,4 +198,54 @@ private function addToAllowedValueListIfNotExist(TypeDoc $doc, $value) : void
236198
$doc->addAllowedValue($value);
237199
}
238200
}
201+
202+
/**
203+
* @param TypeDoc $doc
204+
* @param Constraint $constraint
205+
*
206+
* @throws \ReflectionException
207+
*/
208+
private function basicAppendToDoc(TypeDoc $doc, Constraint $constraint): void
209+
{
210+
static $notNullConstraintList = [
211+
Assert\NotNull::class,
212+
Assert\IsTrue::class, // If it is true, it cannot be null ...
213+
Assert\IsFalse::class, // If it is false, it cannot be null ...
214+
// If should be identical to something, it cannot be null (but can be identical to null)
215+
Assert\IdenticalTo::class,
216+
];
217+
218+
$this->stringDocHelper->append($doc, $constraint);
219+
$this->appendCollectionDoc($doc, $constraint);
220+
221+
$this->minMaxHelper->append($doc, $constraint);
222+
$this->appendValidItemListDoc($doc, $constraint);
223+
224+
if ($constraint instanceof Assert\Existence) {
225+
$doc->setRequired($constraint instanceof Assert\Required);
226+
foreach ($constraint->constraints as $subConstraint) {
227+
$this->appendToDoc($doc, $subConstraint);
228+
}
229+
} elseif ($this->isInstanceOfOneClassIn($constraint, $notNullConstraintList)) {
230+
$doc->setNullable(
231+
($constraint instanceof Assert\IdenticalTo)
232+
? is_null($constraint->value)
233+
: false
234+
);
235+
$defaultValue = $exampleValue = null;
236+
switch (true) {
237+
case $constraint instanceof Assert\IsTrue:
238+
$defaultValue = $exampleValue = true;
239+
break;
240+
case $constraint instanceof Assert\IsFalse:
241+
$defaultValue = $exampleValue = false;
242+
break;
243+
case $constraint instanceof Assert\IdenticalTo:
244+
$defaultValue = $exampleValue = $constraint->value;
245+
break;
246+
}
247+
$doc->setDefault($doc->getDefault() ?? $defaultValue);
248+
$doc->setExample($doc->getExample() ?? $exampleValue);
249+
}
250+
}
239251
}

0 commit comments

Comments
 (0)