Skip to content

Commit 555fd2d

Browse files
authored
Polish (#20)
1 parent d8fa97c commit 555fd2d

File tree

6 files changed

+423
-249
lines changed

6 files changed

+423
-249
lines changed
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
namespace Yoanm\JsonRpcParamsSymfonyConstraintDoc\App\Helper;
3+
4+
/**
5+
* Trait ClassComparatorTrait
6+
*/
7+
trait ClassComparatorTrait
8+
{
9+
/**
10+
* @param $object
11+
* @param array $classList
12+
*
13+
* @return string|null
14+
*/
15+
protected function getMatchingClassNameIn($object, array $classList) : ?string
16+
{
17+
$actualClassList = array_merge(
18+
[get_class($object)],
19+
class_implements($object),
20+
class_uses($object)
21+
);
22+
$parentClass = get_parent_class($object);
23+
while (false !== $parentClass) {
24+
$actualClassList[] = $parentClass;
25+
$parentClass = get_parent_class($parentClass);
26+
}
27+
28+
$matchList = array_intersect($actualClassList, $classList);
29+
30+
return array_pop($matchList);
31+
}
32+
}

src/App/Helper/DocTypeHelper.php

+76-33
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ class DocTypeHelper
2323
/** @var TypeGuesser */
2424
private $typeGuesser;
2525

26+
const MANAGED_TYPE_CLASS_LIST = [
27+
'scalar' => ScalarDoc::class,
28+
'string' => StringDoc::class,
29+
'bool' => BooleanDoc::class,
30+
'boolean' => BooleanDoc::class,
31+
'int' => IntegerDoc::class,
32+
'integer' => IntegerDoc::class,
33+
'float' => FloatDoc::class,
34+
'long' => FloatDoc::class,
35+
'double' => FloatDoc::class,
36+
'real' => FloatDoc::class,
37+
'numeric' => NumberDoc::class,
38+
'array' => ArrayDoc::class,
39+
'object' => ObjectDoc::class,
40+
];
41+
2642
/**
2743
* @param ConstraintPayloadDocHelper $constraintPayloadDocHelper
2844
* @param TypeGuesser $typeGuesser
@@ -56,23 +72,7 @@ protected function getDocFromTypeConstraintOrPayloadDocIfExist(array $constraint
5672
$doc = null;
5773
// Check if a Type constraint exist or if a constraint have a type documentation
5874
foreach ($constraintList as $constraint) {
59-
if (null !== ($typeFromPayload = $this->constraintPayloadDocHelper->getTypeIfExist($constraint))) {
60-
$doc = $this->normalizeType($typeFromPayload);
61-
} elseif ($constraint instanceof Assert\Type) {
62-
$doc = $this->normalizeType(strtolower($constraint->type));
63-
} elseif ($constraint instanceof Assert\Existence && count($constraint->constraints) > 0) {
64-
$doc = $this->guess($constraint->constraints);
65-
} elseif ($constraint instanceof Assert\IdenticalTo) {
66-
// Strict comparison so value define the type
67-
$doc = $this->normalizeType(gettype($constraint->value));
68-
} elseif ($constraint instanceof Assert\Callback) {
69-
$callbackResult = call_user_func($constraint->callback);
70-
$doc = $this->guess(
71-
is_array($callbackResult)
72-
? $callbackResult
73-
: [$callbackResult]
74-
);
75-
}
75+
$doc = $this->createDocFromConstraint($constraint);
7676

7777
if (null !== $doc) {
7878
break;
@@ -89,24 +89,67 @@ protected function getDocFromTypeConstraintOrPayloadDocIfExist(array $constraint
8989
*/
9090
private function normalizeType(string $type) : ?TypeDoc
9191
{
92-
if ('scalar' === $type) {
93-
return new ScalarDoc();
94-
} elseif ('string' === $type) {
95-
return new StringDoc();
96-
} elseif (in_array($type, ['bool', 'boolean'])) {
97-
return new BooleanDoc();
98-
} elseif (in_array($type, ['int', 'integer'])) {
99-
return new IntegerDoc();
100-
} elseif (in_array($type, ['float', 'long', 'double', 'real'])) {
101-
return new FloatDoc();
102-
} elseif ('numeric' === $type) {
103-
return new NumberDoc();
104-
} elseif ('array' === $type) {
105-
return new ArrayDoc();
106-
} elseif ('object' === $type) {
107-
return new ObjectDoc();
92+
if (array_key_exists($type, self::MANAGED_TYPE_CLASS_LIST)) {
93+
$class = self::MANAGED_TYPE_CLASS_LIST[$type];
94+
95+
return new $class();
10896
}
10997

11098
return null;
11199
}
100+
101+
/**
102+
* @param Constraint $constraint
103+
*
104+
* @return TypeDoc|null
105+
*/
106+
private function createDocFromConstraint(Constraint $constraint) : ?TypeDoc
107+
{
108+
$doc = null;
109+
110+
if (null !== ($stringType = $this->getStringType($constraint))) {
111+
$doc = $this->normalizeType($stringType);
112+
} elseif ($constraint instanceof Assert\Callback) {
113+
$doc = $this->getTypeFromCallbackConstraint($constraint);
114+
} elseif ($constraint instanceof Assert\Existence && count($constraint->constraints) > 0) {
115+
$doc = $this->guess($constraint->constraints);
116+
}
117+
118+
return $doc;
119+
}
120+
121+
/**
122+
* @param Assert\Callback $constraint
123+
*
124+
* @return TypeDoc
125+
*/
126+
private function getTypeFromCallbackConstraint(Assert\Callback $constraint): TypeDoc
127+
{
128+
$callbackResult = call_user_func($constraint->callback);
129+
$doc = $this->guess(
130+
is_array($callbackResult)
131+
? $callbackResult
132+
: [$callbackResult]
133+
);
134+
return $doc;
135+
}
136+
137+
/**
138+
* @param Constraint $constraint
139+
*
140+
* @return string|null
141+
*/
142+
private function getStringType(Constraint $constraint) : ?string
143+
{
144+
$stringType = null;
145+
if (null !== ($typeFromPayload = $this->constraintPayloadDocHelper->getTypeIfExist($constraint))) {
146+
$stringType = $typeFromPayload;
147+
} elseif ($constraint instanceof Assert\Type) {
148+
$stringType = strtolower($constraint->type);
149+
} elseif ($constraint instanceof Assert\IdenticalTo) {// Strict comparison so value define the type
150+
$stringType = gettype($constraint->value);
151+
}
152+
153+
return $stringType;
154+
}
112155
}

src/App/Helper/MinMaxHelper.php

+82-36
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
class 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

Comments
 (0)