1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaRestrictionMetadataInterface; |
17
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
18
|
|
|
use ApiPlatform\Core\Metadata\Property\PropertyMetadata; |
19
|
|
|
use Symfony\Component\Validator\Constraint; |
20
|
|
|
use Symfony\Component\Validator\Constraints\Bic; |
21
|
|
|
use Symfony\Component\Validator\Constraints\CardScheme; |
22
|
|
|
use Symfony\Component\Validator\Constraints\Currency; |
23
|
|
|
use Symfony\Component\Validator\Constraints\Date; |
24
|
|
|
use Symfony\Component\Validator\Constraints\DateTime; |
25
|
|
|
use Symfony\Component\Validator\Constraints\Email; |
26
|
|
|
use Symfony\Component\Validator\Constraints\File; |
27
|
|
|
use Symfony\Component\Validator\Constraints\Iban; |
28
|
|
|
use Symfony\Component\Validator\Constraints\Image; |
29
|
|
|
use Symfony\Component\Validator\Constraints\Isbn; |
30
|
|
|
use Symfony\Component\Validator\Constraints\Issn; |
31
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank; |
32
|
|
|
use Symfony\Component\Validator\Constraints\NotNull; |
33
|
|
|
use Symfony\Component\Validator\Constraints\Time; |
34
|
|
|
use Symfony\Component\Validator\Constraints\Url; |
35
|
|
|
use Symfony\Component\Validator\Constraints\Uuid; |
36
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadataInterface as ValidatorClassMetadataInterface; |
37
|
|
|
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface as ValidatorMetadataFactoryInterface; |
38
|
|
|
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface as ValidatorPropertyMetadataInterface; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Decorates a metadata loader using the validator. |
42
|
|
|
* |
43
|
|
|
* @author Kévin Dunglas <[email protected]> |
44
|
|
|
*/ |
45
|
|
|
final class ValidatorPropertyMetadataFactory implements PropertyMetadataFactoryInterface |
46
|
|
|
{ |
47
|
|
|
/** |
48
|
|
|
* @var string[] A list of constraint classes making the entity required |
49
|
|
|
*/ |
50
|
|
|
public const REQUIRED_CONSTRAINTS = [NotBlank::class, NotNull::class]; |
51
|
|
|
|
52
|
|
|
public const SCHEMA_MAPPED_CONSTRAINTS = [ |
53
|
|
|
Url::class => 'http://schema.org/url', |
54
|
|
|
Email::class => 'http://schema.org/email', |
55
|
|
|
Uuid::class => 'http://schema.org/identifier', |
56
|
|
|
CardScheme::class => 'http://schema.org/identifier', |
57
|
|
|
Bic::class => 'http://schema.org/identifier', |
58
|
|
|
Iban::class => 'http://schema.org/identifier', |
59
|
|
|
Date::class => 'http://schema.org/Date', |
60
|
|
|
DateTime::class => 'http://schema.org/DateTime', |
61
|
|
|
Time::class => 'http://schema.org/Time', |
62
|
|
|
Image::class => 'http://schema.org/image', |
63
|
|
|
File::class => 'http://schema.org/MediaObject', |
64
|
|
|
Currency::class => 'http://schema.org/priceCurrency', |
65
|
|
|
Isbn::class => 'http://schema.org/isbn', |
66
|
|
|
Issn::class => 'http://schema.org/issn', |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
private $decorated; |
70
|
|
|
private $validatorMetadataFactory; |
71
|
|
|
/** |
72
|
|
|
* @var iterable<PropertySchemaRestrictionMetadataInterface> |
73
|
|
|
*/ |
74
|
|
|
private $restrictionsMetadata; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param PropertySchemaRestrictionMetadataInterface[] $restrictionsMetadata |
78
|
|
|
*/ |
79
|
|
|
public function __construct(ValidatorMetadataFactoryInterface $validatorMetadataFactory, PropertyMetadataFactoryInterface $decorated, iterable $restrictionsMetadata = []) |
80
|
|
|
{ |
81
|
|
|
$this->validatorMetadataFactory = $validatorMetadataFactory; |
82
|
|
|
$this->decorated = $decorated; |
83
|
|
|
$this->restrictionsMetadata = $restrictionsMetadata; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function create(string $resourceClass, string $name, array $options = []): PropertyMetadata |
90
|
|
|
{ |
91
|
|
|
$propertyMetadata = $this->decorated->create($resourceClass, $name, $options); |
92
|
|
|
|
93
|
|
|
$required = $propertyMetadata->isRequired(); |
94
|
|
|
$iri = $propertyMetadata->getIri(); |
95
|
|
|
$schema = $propertyMetadata->getSchema(); |
96
|
|
|
|
97
|
|
|
if (null !== $required && null !== $iri && null !== $schema) { |
98
|
|
|
return $propertyMetadata; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$validatorClassMetadata = $this->validatorMetadataFactory->getMetadataFor($resourceClass); |
102
|
|
|
|
103
|
|
|
if (!$validatorClassMetadata instanceof ValidatorClassMetadataInterface) { |
104
|
|
|
throw new \UnexpectedValueException(sprintf('Validator class metadata expected to be of type "%s".', ValidatorClassMetadataInterface::class)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$validationGroups = $this->getValidationGroups($validatorClassMetadata, $options); |
108
|
|
|
$restrictions = []; |
109
|
|
|
|
110
|
|
|
foreach ($validatorClassMetadata->getPropertyMetadata($name) as $validatorPropertyMetadata) { |
111
|
|
|
foreach ($this->getPropertyConstraints($validatorPropertyMetadata, $validationGroups) as $constraint) { |
112
|
|
|
if (null === $required && $this->isRequired($constraint)) { |
113
|
|
|
$required = true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (null === $iri) { |
117
|
|
|
$iri = self::SCHEMA_MAPPED_CONSTRAINTS[\get_class($constraint)] ?? null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
foreach ($this->restrictionsMetadata as $restrictionMetadata) { |
121
|
|
|
if ($restrictionMetadata->supports($constraint, $propertyMetadata)) { |
122
|
|
|
$restrictions[] = $restrictionMetadata->create($constraint, $propertyMetadata); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$propertyMetadata = $propertyMetadata->withIri($iri)->withRequired($required ?? false); |
129
|
|
|
|
130
|
|
|
if (!empty($restrictions)) { |
131
|
|
|
if (null === $schema) { |
132
|
|
|
$schema = []; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$schema += array_merge(...$restrictions); |
136
|
|
|
$propertyMetadata = $propertyMetadata->withSchema($schema); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $propertyMetadata; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns the list of validation groups. |
144
|
|
|
*/ |
145
|
|
|
private function getValidationGroups(ValidatorClassMetadataInterface $classMetadata, array $options): array |
146
|
|
|
{ |
147
|
|
|
if (isset($options['validation_groups'])) { |
148
|
|
|
return $options['validation_groups']; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if (!method_exists($classMetadata, 'getDefaultGroup')) { |
152
|
|
|
throw new \UnexpectedValueException(sprintf('Validator class metadata expected to have method "%s".', 'getDefaultGroup')); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return [$classMetadata->getDefaultGroup()]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Tests if the property is required because of its validation groups. |
160
|
|
|
*/ |
161
|
|
|
private function getPropertyConstraints( |
162
|
|
|
ValidatorPropertyMetadataInterface $validatorPropertyMetadata, |
163
|
|
|
array $groups |
164
|
|
|
): array { |
165
|
|
|
$constraints = []; |
166
|
|
|
|
167
|
|
|
foreach ($groups as $validationGroup) { |
168
|
|
|
if (!\is_string($validationGroup)) { |
169
|
|
|
continue; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
foreach ($validatorPropertyMetadata->findConstraints($validationGroup) as $propertyConstraint) { |
173
|
|
|
$constraints[] = $propertyConstraint; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $constraints; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Is this constraint making the related property required? |
182
|
|
|
*/ |
183
|
|
|
private function isRequired(Constraint $constraint): bool |
184
|
|
|
{ |
185
|
|
|
foreach (self::REQUIRED_CONSTRAINTS as $requiredConstraint) { |
186
|
|
|
if ($constraint instanceof $requiredConstraint) { |
187
|
|
|
return true; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return false; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|