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

api-platform / core / 9710836697

28 Jun 2024 09:35AM UTC coverage: 63.285% (+1.2%) from 62.122%
9710836697

push

github

soyuka
docs: changelog v3.3.7

11104 of 17546 relevant lines covered (63.29%)

52.26 hits per line

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

81.36
/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));
87✔
36
        } catch (\InvalidArgumentException $e) {
87✔
37
            // Ensure it's not a resource
38
            try {
39
                simplexml_import_dom(XmlUtils::loadFile($path, XmlResourceExtractor::SCHEMA));
87✔
40
            } catch (\InvalidArgumentException) {
×
41
                throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
×
42
            }
43

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

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

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

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

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

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

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

108
        return $data;
×
109
    }
110

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

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

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

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