|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Kreta package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
|
7
|
|
|
* (c) Gorka Laucirica <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Kreta\SimpleApiDocBundle\Parser; |
|
14
|
|
|
|
|
15
|
|
|
use Nelmio\ApiDocBundle\Parser\ValidationParser as BaseValidationParser; |
|
16
|
|
|
use Symfony\Component\Validator\Constraint; |
|
17
|
|
|
use Symfony\Component\Validator\Constraints\Count; |
|
18
|
|
|
use Symfony\Component\Validator\Constraints\Length; |
|
19
|
|
|
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface; |
|
20
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Validation parser class. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Beñat Espiña <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class ValidationParser extends BaseValidationParser |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* The Symfony validator component. |
|
31
|
|
|
* |
|
32
|
|
|
* @var \Symfony\Component\Validator\Validator\ValidatorInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $validator; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param MetadataFactoryInterface $factory The metadata factory |
|
40
|
|
|
* @param ValidatorInterface $validator The Symfony validator component |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(MetadataFactoryInterface $factory, ValidatorInterface $validator) |
|
43
|
|
|
{ |
|
44
|
|
|
parent::__construct($factory); |
|
45
|
|
|
$this->validator = $validator; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Gets the validations. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string|null $class The class namespace |
|
52
|
|
|
* |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getValidations($class = null) |
|
56
|
|
|
{ |
|
57
|
|
|
$reflectionClass = $class ? new \ReflectionClass($class) : false; |
|
58
|
|
|
if ($reflectionClass === false) { |
|
59
|
|
|
return []; |
|
60
|
|
|
} |
|
61
|
|
|
$result = $this->buildValidation($class); |
|
62
|
|
|
$validations = []; |
|
63
|
|
|
foreach ($result as $keyElement => $element) { |
|
64
|
|
|
if (is_array($element)) { |
|
65
|
|
|
foreach ($element as $keyChild => $child) { |
|
66
|
|
|
if (is_array($child)) { |
|
67
|
|
|
foreach ($child as $grandChild) { |
|
68
|
|
|
$validations[] = sprintf('%s => %s', $keyChild, $grandChild); |
|
69
|
|
|
} |
|
70
|
|
|
} else { |
|
71
|
|
|
$validations[] = sprintf('%s => %s', $keyElement, $child); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} else { |
|
75
|
|
|
$validations[] = $element; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $validations; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Parses and builds the validation. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string|null $class The class namespace |
|
86
|
|
|
* |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function buildValidation($class = null) |
|
90
|
|
|
{ |
|
91
|
|
|
$validations = []; |
|
92
|
|
|
$metadata = $this->validator->getMetadataFor($class); |
|
93
|
|
|
$entityConstraints = []; |
|
94
|
|
|
foreach ($metadata->getConstraints() as $constraint) { |
|
95
|
|
|
$class = new \ReflectionClass($constraint); |
|
96
|
|
|
$fields = $constraint->fields; |
|
97
|
|
|
$entityConstraint = [implode(', ', (array) $fields) => $constraint->message]; |
|
98
|
|
|
$entityConstraints = array_merge($entityConstraints, $entityConstraint); |
|
99
|
|
|
$validations[$class->getShortName()] = $entityConstraint; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
foreach ($metadata->getConstrainedProperties() as $property) { |
|
103
|
|
|
$constraints = $metadata->getPropertyMetadata($property)[0]->constraints; |
|
104
|
|
|
$result = []; |
|
105
|
|
|
foreach ($constraints as $constraint) { |
|
106
|
|
|
$class = new \ReflectionObject($constraint); |
|
107
|
|
|
switch ($name = $class->getShortName()) { |
|
108
|
|
|
case 'True': |
|
109
|
|
|
$result[$name] = $constraint->message; |
|
110
|
|
|
break; |
|
111
|
|
|
case 'NotBlank': |
|
112
|
|
|
$result[$name] = $constraint->message; |
|
113
|
|
|
break; |
|
114
|
|
|
case 'Type': |
|
115
|
|
|
$result[$name] = $constraint->type; |
|
116
|
|
|
break; |
|
117
|
|
|
case 'Length': |
|
118
|
|
|
$result[$name] = $this->constraintMessages($constraint); |
|
119
|
|
|
break; |
|
120
|
|
|
case 'Count': |
|
121
|
|
|
$result[$name] = $this->constraintMessages($constraint); |
|
122
|
|
|
break; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
$validations[$property] = $result; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $validations; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Parses the constraint message. |
|
133
|
|
|
* |
|
134
|
|
|
* @param \Symfony\Component\Validator\Constraint $constraint The constraint |
|
135
|
|
|
* |
|
136
|
|
|
* @return array |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function constraintMessages(Constraint $constraint) |
|
139
|
|
|
{ |
|
140
|
|
|
$result = []; |
|
141
|
|
View Code Duplication |
if (isset($constraint->min) && $constraint->min !== null) { |
|
|
|
|
|
|
142
|
|
|
$count = new Count(['min' => $constraint->min]); |
|
143
|
|
|
$length = new Length(['min' => $constraint->min]); |
|
144
|
|
|
$result[] = $constraint->minMessage; |
|
145
|
|
|
} |
|
146
|
|
View Code Duplication |
if (isset($constraint->max) && $constraint->max !== null) { |
|
|
|
|
|
|
147
|
|
|
$count = new Count(['max' => $constraint->min]); |
|
148
|
|
|
$length = new Length(['max' => $constraint->min]); |
|
149
|
|
|
$result[] = $constraint->maxMessage; |
|
150
|
|
|
} |
|
151
|
|
|
if (isset($constraint->exactMessage) |
|
152
|
|
|
&& $constraint->exactMessage !== $count->exactMessage |
|
|
|
|
|
|
153
|
|
|
&& $constraint->exactMessage !== $length->exactMessage |
|
|
|
|
|
|
154
|
|
|
) { |
|
155
|
|
|
$result[] = $constraint->exactMessage; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $result; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.