• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 14726133838

29 Apr 2025 07:51AM UTC coverage: 0.0% (-23.4%) from 23.443%
14726133838

Pull #7114

github

web-flow
Merge b5ddc44e4 into 4b1b94cad
Pull Request #7114: Merge 4.1

0 of 88 new or added lines in 20 files covered. (0.0%)

10373 existing lines in 330 files now uncovered.

0 of 49280 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactory.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Symfony\Validator\Metadata\Property;
15

16
use ApiPlatform\JsonSchema\Metadata\Property\Factory\SchemaPropertyMetadataFactory;
17
use ApiPlatform\Metadata\ApiProperty;
18
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
19
use ApiPlatform\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaRestrictionMetadataInterface;
20
use Symfony\Component\Validator\Constraint;
21
use Symfony\Component\Validator\Constraints\Bic;
22
use Symfony\Component\Validator\Constraints\CardScheme;
23
use Symfony\Component\Validator\Constraints\Compound;
24
use Symfony\Component\Validator\Constraints\Currency;
25
use Symfony\Component\Validator\Constraints\Date;
26
use Symfony\Component\Validator\Constraints\DateTime;
27
use Symfony\Component\Validator\Constraints\Email;
28
use Symfony\Component\Validator\Constraints\File;
29
use Symfony\Component\Validator\Constraints\Iban;
30
use Symfony\Component\Validator\Constraints\Image;
31
use Symfony\Component\Validator\Constraints\Isbn;
32
use Symfony\Component\Validator\Constraints\Issn;
33
use Symfony\Component\Validator\Constraints\NotBlank;
34
use Symfony\Component\Validator\Constraints\NotNull;
35
use Symfony\Component\Validator\Constraints\Sequentially;
36
use Symfony\Component\Validator\Constraints\Time;
37
use Symfony\Component\Validator\Constraints\Url;
38
use Symfony\Component\Validator\Constraints\Uuid;
39
use Symfony\Component\Validator\Mapping\ClassMetadataInterface as ValidatorClassMetadataInterface;
40
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface as ValidatorMetadataFactoryInterface;
41
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface as ValidatorPropertyMetadataInterface;
42

43
/**
44
 * Decorates a metadata loader using the validator.
45
 *
46
 * @author Kévin Dunglas <dunglas@gmail.com>
47
 */
48
final class ValidatorPropertyMetadataFactory implements PropertyMetadataFactoryInterface
49
{
50
    /**
51
     * @var string[] A list of constraint classes making the entity required
52
     */
53
    public const REQUIRED_CONSTRAINTS = [NotBlank::class, NotNull::class];
54

55
    public const SCHEMA_MAPPED_CONSTRAINTS = [
56
        Url::class => 'https://schema.org/url',
57
        Email::class => 'https://schema.org/email',
58
        Uuid::class => 'https://schema.org/identifier',
59
        CardScheme::class => 'https://schema.org/identifier',
60
        Bic::class => 'https://schema.org/identifier',
61
        Iban::class => 'https://schema.org/identifier',
62
        Date::class => 'https://schema.org/Date',
63
        DateTime::class => 'https://schema.org/DateTime',
64
        Time::class => 'https://schema.org/Time',
65
        Image::class => 'https://schema.org/image',
66
        File::class => 'https://schema.org/MediaObject',
67
        Currency::class => 'https://schema.org/priceCurrency',
68
        Isbn::class => 'https://schema.org/isbn',
69
        Issn::class => 'https://schema.org/issn',
70
    ];
71

72
    /**
73
     * @param PropertySchemaRestrictionMetadataInterface[] $restrictionsMetadata
74
     */
75
    public function __construct(private readonly ValidatorMetadataFactoryInterface $validatorMetadataFactory, private readonly PropertyMetadataFactoryInterface $decorated, private readonly iterable $restrictionsMetadata = [])
76
    {
UNCOV
77
    }
×
78

79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function create(string $resourceClass, string $property, array $options = []): ApiProperty
83
    {
UNCOV
84
        $propertyMetadata = $this->decorated->create($resourceClass, $property, $options);
×
85

UNCOV
86
        $extraProperties = $propertyMetadata->getExtraProperties() ?? [];
×
87
        // see AttributePropertyMetadataFactory
UNCOV
88
        if (true === ($extraProperties[SchemaPropertyMetadataFactory::JSON_SCHEMA_USER_DEFINED] ?? false)) {
×
89
            // schema seems to have been declared by the user: do not override nor complete user value
UNCOV
90
            return $propertyMetadata;
×
91
        }
92

UNCOV
93
        $required = $propertyMetadata->isRequired();
×
UNCOV
94
        $types = $propertyMetadata->getTypes();
×
UNCOV
95
        $schema = $propertyMetadata->getSchema();
×
96

UNCOV
97
        if (null !== $required && $types && $schema) {
×
98
            return $propertyMetadata;
×
99
        }
100

UNCOV
101
        $validatorClassMetadata = $this->validatorMetadataFactory->getMetadataFor($resourceClass);
×
102

UNCOV
103
        if (!$validatorClassMetadata instanceof ValidatorClassMetadataInterface) {
×
104
            throw new \UnexpectedValueException(\sprintf('Validator class metadata expected to be of type "%s".', ValidatorClassMetadataInterface::class));
×
105
        }
106

UNCOV
107
        $validationGroups = $this->getValidationGroups($validatorClassMetadata, $options);
×
UNCOV
108
        $restrictions = [];
×
UNCOV
109
        $types ??= [];
×
110

UNCOV
111
        foreach ($validatorClassMetadata->getPropertyMetadata($property) as $validatorPropertyMetadata) {
×
UNCOV
112
            foreach ($this->getPropertyConstraints($validatorPropertyMetadata, $validationGroups) as $constraint) {
×
UNCOV
113
                if (null === $required && $this->isRequired($constraint)) {
×
UNCOV
114
                    $required = true;
×
115
                }
116

UNCOV
117
                $type = self::SCHEMA_MAPPED_CONSTRAINTS[$constraint::class] ?? null;
×
118

UNCOV
119
                if ($type && !\in_array($type, $types, true)) {
×
UNCOV
120
                    $types[] = $type;
×
121
                }
122

UNCOV
123
                foreach ($this->restrictionsMetadata as $restrictionMetadata) {
×
UNCOV
124
                    if ($restrictionMetadata->supports($constraint, $propertyMetadata)) {
×
UNCOV
125
                        $restrictions[] = $restrictionMetadata->create($constraint, $propertyMetadata);
×
126
                    }
127
                }
128
            }
129
        }
130

UNCOV
131
        if ($types) {
×
UNCOV
132
            $propertyMetadata = $propertyMetadata->withTypes($types);
×
133
        }
134

UNCOV
135
        $propertyMetadata = $propertyMetadata->withRequired($required ?? false);
×
136

UNCOV
137
        if (!empty($restrictions)) {
×
UNCOV
138
            if (null === $schema) {
×
UNCOV
139
                $schema = [];
×
140
            }
141

UNCOV
142
            $schema += array_merge(...$restrictions);
×
UNCOV
143
            $propertyMetadata = $propertyMetadata->withSchema($schema);
×
144
        }
145

UNCOV
146
        return $propertyMetadata;
×
147
    }
148

149
    /**
150
     * Returns the list of validation groups.
151
     */
152
    private function getValidationGroups(ValidatorClassMetadataInterface $classMetadata, array $options): array
153
    {
154
        if (
UNCOV
155
            isset($options['validation_groups'])
×
UNCOV
156
            && !\is_callable($options['validation_groups'])
×
157
        ) {
158
            return $options['validation_groups'];
×
159
        }
160

UNCOV
161
        if (!method_exists($classMetadata, 'getDefaultGroup')) {
×
162
            throw new \UnexpectedValueException(\sprintf('Validator class metadata expected to have method "%s".', 'getDefaultGroup'));
×
163
        }
164

UNCOV
165
        return [$classMetadata->getDefaultGroup()];
×
166
    }
167

168
    /**
169
     * Tests if the property is required because of its validation groups.
170
     */
171
    private function getPropertyConstraints(
172
        ValidatorPropertyMetadataInterface $validatorPropertyMetadata,
173
        array $groups,
174
    ): array {
UNCOV
175
        $constraints = [];
×
176

UNCOV
177
        foreach ($groups as $validationGroup) {
×
UNCOV
178
            if (!\is_string($validationGroup)) {
×
179
                continue;
×
180
            }
181

UNCOV
182
            foreach ($validatorPropertyMetadata->findConstraints($validationGroup) as $propertyConstraint) {
×
UNCOV
183
                if ($propertyConstraint instanceof Sequentially || $propertyConstraint instanceof Compound) {
×
184
                    $constraints[] = $propertyConstraint->getNestedConstraints();
×
185
                } else {
UNCOV
186
                    $constraints[] = [$propertyConstraint];
×
187
                }
188
            }
189
        }
190

UNCOV
191
        return array_merge([], ...$constraints);
×
192
    }
193

194
    /**
195
     * Is this constraint making the related property required?
196
     */
197
    private function isRequired(Constraint $constraint): bool
198
    {
UNCOV
199
        if ($constraint instanceof NotBlank && $constraint->allowNull) {
×
200
            return false;
×
201
        }
202

UNCOV
203
        foreach (self::REQUIRED_CONSTRAINTS as $requiredConstraint) {
×
UNCOV
204
            if ($constraint instanceof $requiredConstraint) {
×
UNCOV
205
                return true;
×
206
            }
207
        }
208

UNCOV
209
        return false;
×
210
    }
211
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc