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

api-platform / core / 15023181448

14 May 2025 02:19PM UTC coverage: 0.0% (-8.4%) from 8.418%
15023181448

Pull #7139

github

web-flow
Merge 9f45709da into 1862d03b7
Pull Request #7139: refactor(symfony): remove obsolete option `validator.query-parameter-validation`

0 of 4 new or added lines in 1 file covered. (0.0%)

11266 existing lines in 366 files now uncovered.

0 of 50828 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/Metadata/Extractor/YamlPropertyExtractor.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\Metadata\Extractor;
15

16
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
17
use Symfony\Component\Yaml\Exception\ParseException;
18
use Symfony\Component\Yaml\Yaml;
19

20
/**
21
 * Extracts an array of metadata from a list of YAML files.
22
 *
23
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
24
 */
25
final class YamlPropertyExtractor extends AbstractPropertyExtractor
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function extractPath(string $path): void
31
    {
32
        try {
UNCOV
33
            $propertiesYaml = Yaml::parse((string) file_get_contents($path), Yaml::PARSE_CONSTANT);
×
34
        } catch (ParseException $e) {
×
35
            $e->setParsedFile($path);
×
36

37
            throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
×
38
        }
39

UNCOV
40
        if (null === $propertiesYaml = $propertiesYaml['properties'] ?? $propertiesYaml) {
×
41
            return;
×
42
        }
43

UNCOV
44
        if (!\is_array($propertiesYaml)) {
×
45
            throw new InvalidArgumentException(\sprintf('"properties" setting is expected to be null or an array, %s given in "%s".', \gettype($propertiesYaml), $path));
×
46
        }
47

UNCOV
48
        $this->buildProperties($propertiesYaml);
×
49
    }
50

51
    private function buildProperties(array $resourcesYaml): void
52
    {
UNCOV
53
        foreach ($resourcesYaml as $resourceName => $resourceYaml) {
×
UNCOV
54
            if (null === $resourceYaml) {
×
55
                continue;
×
56
            }
57

UNCOV
58
            $resourceName = $this->resolve($resourceName);
×
59

UNCOV
60
            foreach ($resourceYaml as $propertyName => $propertyValues) {
×
UNCOV
61
                if (null === $propertyValues) {
×
62
                    $this->properties[$resourceName][$propertyName] = null;
×
63
                    continue;
×
64
                }
65

UNCOV
66
                if (!\is_array($propertyValues)) {
×
67
                    throw new InvalidArgumentException(\sprintf('"%s" setting is expected to be null or an array, %s given.', $propertyName, \gettype($propertyValues)));
×
68
                }
69

UNCOV
70
                $this->properties[$resourceName][$propertyName] = [
×
UNCOV
71
                    'description' => $this->phpize($propertyValues, 'description', 'string'),
×
UNCOV
72
                    'readable' => $this->phpize($propertyValues, 'readable', 'bool'),
×
UNCOV
73
                    'writable' => $this->phpize($propertyValues, 'writable', 'bool'),
×
UNCOV
74
                    'readableLink' => $this->phpize($propertyValues, 'readableLink', 'bool'),
×
UNCOV
75
                    'writableLink' => $this->phpize($propertyValues, 'writableLink', 'bool'),
×
UNCOV
76
                    'required' => $this->phpize($propertyValues, 'required', 'bool'),
×
UNCOV
77
                    'identifier' => $this->phpize($propertyValues, 'identifier', 'bool'),
×
UNCOV
78
                    'deprecationReason' => $this->phpize($propertyValues, 'deprecationReason', 'string'),
×
UNCOV
79
                    'fetchable' => $this->phpize($propertyValues, 'fetchable', 'bool'),
×
UNCOV
80
                    'fetchEager' => $this->phpize($propertyValues, 'fetchEager', 'bool'),
×
UNCOV
81
                    'push' => $this->phpize($propertyValues, 'push', 'bool'),
×
UNCOV
82
                    'security' => $this->phpize($propertyValues, 'security', 'string'),
×
UNCOV
83
                    'securityPostDenormalize' => $this->phpize($propertyValues, 'securityPostDenormalize', 'string'),
×
UNCOV
84
                    'initializable' => $this->phpize($propertyValues, 'initializable', 'bool'),
×
UNCOV
85
                    'iris' => $this->buildAttribute($propertyValues, 'iris'),
×
UNCOV
86
                    'jsonldContext' => $this->buildAttribute($propertyValues, 'jsonldContext'),
×
UNCOV
87
                    'openapiContext' => $this->buildAttribute($propertyValues, 'openapiContext'),
×
UNCOV
88
                    'jsonSchemaContext' => $this->buildAttribute($propertyValues, 'jsonSchemaContext'),
×
UNCOV
89
                    'types' => $this->buildAttribute($propertyValues, 'types'),
×
UNCOV
90
                    'extraProperties' => $this->buildAttribute($propertyValues, 'extraProperties'),
×
UNCOV
91
                    'default' => $propertyValues['default'] ?? null,
×
UNCOV
92
                    'example' => $propertyValues['example'] ?? null,
×
UNCOV
93
                    'builtinTypes' => $this->buildAttribute($propertyValues, 'builtinTypes'),
×
UNCOV
94
                    'schema' => $this->buildAttribute($propertyValues, 'schema'),
×
UNCOV
95
                    'genId' => $this->phpize($propertyValues, 'genId', 'bool'),
×
UNCOV
96
                    'uriTemplate' => $this->phpize($propertyValues, 'uriTemplate', 'string'),
×
UNCOV
97
                    'property' => $this->phpize($propertyValues, 'property', 'string'),
×
UNCOV
98
                    'nativeType' => $this->phpize($propertyValues, 'nativeType', 'string'),
×
UNCOV
99
                ];
×
100
            }
101
        }
102
    }
103

104
    private function buildAttribute(array $resource, string $key, mixed $default = null)
105
    {
UNCOV
106
        if (empty($resource[$key])) {
×
UNCOV
107
            return $default;
×
108
        }
109

110
        if (!\is_array($resource[$key])) {
×
111
            throw new InvalidArgumentException(\sprintf('"%s" setting is expected to be an array, %s given', $key, \gettype($resource[$key])));
×
112
        }
113

114
        return $resource[$key];
×
115
    }
116

117
    /**
118
     * Transforms an XML attribute's value in a PHP value.
119
     */
120
    private function phpize(?array $resource, string $key, string $type, mixed $default = null): array|bool|int|string|null
121
    {
UNCOV
122
        if (!isset($resource[$key])) {
×
UNCOV
123
            return $default;
×
124
        }
125

UNCOV
126
        return match ($type) {
×
127
            'bool|string' => \in_array($resource[$key], ['1', '0', 1, 0, 'true', 'false', true, false], true) ? $this->phpize($resource, $key, 'bool') : $this->phpize($resource, $key, 'string'),
×
UNCOV
128
            'string' => (string) $resource[$key],
×
129
            'integer' => (int) $resource[$key],
×
UNCOV
130
            'bool' => \in_array($resource[$key], ['1', 'true', 1, true], false),
×
UNCOV
131
            default => throw new InvalidArgumentException(\sprintf('The property "%s" must be a "%s", "%s" given.', $key, $type, \gettype($resource[$key]))),
×
UNCOV
132
        };
×
133
    }
134
}
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