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

api-platform / core / 10814494863

11 Sep 2024 03:11PM UTC coverage: 7.008% (-0.7%) from 7.679%
10814494863

push

github

web-flow
fix(state): remove resource_class change (#6607)

11516 of 164321 relevant lines covered (7.01%)

22.85 hits per line

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

78.33
/src/Metadata/Extractor/XmlPropertyExtractor.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\Config\Util\XmlUtils;
18

19
/**
20
 * Extracts an array of metadata from a list of XML files.
21
 *
22
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
23
 */
24
final class XmlPropertyExtractor extends AbstractPropertyExtractor
25
{
26
    public const SCHEMA = __DIR__.'/schema/properties.xsd';
27

28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function extractPath(string $path): void
32
    {
33
        try {
34
            /** @var \SimpleXMLElement $xml */
35
            $xml = simplexml_import_dom(XmlUtils::loadFile($path, self::SCHEMA));
521✔
36
        } catch (\InvalidArgumentException $e) {
521✔
37
            // Ensure it's not a resource
38
            try {
39
                simplexml_import_dom(XmlUtils::loadFile($path, XmlResourceExtractor::SCHEMA));
521✔
40
            } catch (\InvalidArgumentException) {
×
41
                throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
×
42
            }
43

44
            // It's a resource: ignore error
45
            return;
521✔
46
        }
47

48
        foreach ($xml->property as $property) {
521✔
49
            $this->properties[$this->resolve((string) $property['resource'])][(string) $property['name']] = [
521✔
50
                'description' => $this->phpize($property, 'description', 'string'),
521✔
51
                'readable' => $this->phpize($property, 'readable', 'bool'),
521✔
52
                'writable' => $this->phpize($property, 'writable', 'bool'),
521✔
53
                'readableLink' => $this->phpize($property, 'readableLink', 'bool'),
521✔
54
                'writableLink' => $this->phpize($property, 'writableLink', 'bool'),
521✔
55
                'required' => $this->phpize($property, 'required', 'bool'),
521✔
56
                'identifier' => $this->phpize($property, 'identifier', 'bool'),
521✔
57
                'default' => $this->phpize($property, 'default', 'string'),
521✔
58
                'example' => $this->phpize($property, 'example', 'string'),
521✔
59
                'deprecationReason' => $this->phpize($property, 'deprecationReason', 'string'),
521✔
60
                'fetchable' => $this->phpize($property, 'fetchable', 'bool'),
521✔
61
                'fetchEager' => $this->phpize($property, 'fetchEager', 'bool'),
521✔
62
                'jsonldContext' => isset($property->jsonldContext->values) ? $this->buildValues($property->jsonldContext->values) : null,
521✔
63
                'openapiContext' => isset($property->openapiContext->values) ? $this->buildValues($property->openapiContext->values) : null,
521✔
64
                'jsonSchemaContext' => isset($property->jsonSchemaContext->values) ? $this->buildValues($property->jsonSchemaContext->values) : null,
521✔
65
                'push' => $this->phpize($property, 'push', 'bool'),
521✔
66
                'security' => $this->phpize($property, 'security', 'string'),
521✔
67
                'securityPostDenormalize' => $this->phpize($property, 'securityPostDenormalize', 'string'),
521✔
68
                'types' => $this->buildArrayValue($property, 'type'),
521✔
69
                'builtinTypes' => $this->buildArrayValue($property, 'builtinType'),
521✔
70
                'schema' => isset($property->schema->values) ? $this->buildValues($property->schema->values) : null,
521✔
71
                'initializable' => $this->phpize($property, 'initializable', 'bool'),
521✔
72
                'extraProperties' => $this->buildExtraProperties($property, 'extraProperties'),
521✔
73
                'iris' => $this->buildArrayValue($property, 'iri'),
521✔
74
                'genId' => $this->phpize($property, 'genId', 'bool'),
521✔
75
                'uriTemplate' => $this->phpize($property, 'uriTemplate', 'string'),
521✔
76
                'property' => $this->phpize($property, 'property', 'string'),
521✔
77
            ];
521✔
78
        }
79
    }
80

81
    private function buildExtraProperties(\SimpleXMLElement $resource, ?string $key = null): ?array
82
    {
83
        if (null !== $key) {
521✔
84
            if (!isset($resource->{$key})) {
521✔
85
                return null;
521✔
86
            }
87

88
            $resource = $resource->{$key};
×
89
        }
90

91
        return $this->buildValues($resource->values);
×
92
    }
93

94
    /**
95
     * @return string[]
96
     */
97
    private function buildValues(\SimpleXMLElement $resource): array
98
    {
99
        $data = [];
×
100
        foreach ($resource->value as $value) {
×
101
            if (null !== $value->attributes()->name) {
×
102
                $data[(string) $value->attributes()->name] = isset($value->values) ? $this->buildValues($value->values) : (string) $value;
×
103
                continue;
×
104
            }
105

106
            $data[] = isset($value->values) ? $this->buildValues($value->values) : (string) $value;
×
107
        }
108

109
        return $data;
×
110
    }
111

112
    private function buildArrayValue(?\SimpleXMLElement $resource, string $key, mixed $default = null)
113
    {
114
        if (!isset($resource->{$key.'s'}->{$key})) {
521✔
115
            return $default;
521✔
116
        }
117

118
        return (array) $resource->{$key.'s'}->{$key};
521✔
119
    }
120

121
    /**
122
     * Transforms an XML attribute's value in a PHP value.
123
     */
124
    private function phpize(\SimpleXMLElement $resource, string $key, string $type, mixed $default = null): array|bool|int|string|null
125
    {
126
        if (!isset($resource[$key])) {
521✔
127
            return $default;
521✔
128
        }
129

130
        return match ($type) {
521✔
131
            'bool|string' => \in_array((string) $resource[$key], ['1', '0', 'true', 'false'], true) ? $this->phpize($resource, $key, 'bool') : $this->phpize($resource, $key, 'string'),
×
132
            'string' => (string) $resource[$key],
521✔
133
            'integer' => (int) $resource[$key],
×
134
            'bool' => (bool) XmlUtils::phpize($resource[$key]),
521✔
135
            default => null,
521✔
136
        };
521✔
137
    }
138
}
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