• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

api-platform / core / 13203378522

07 Feb 2025 03:56PM UTC coverage: 8.501% (+0.7%) from 7.837%
13203378522

push

github

soyuka
Merge 4.1

111 of 490 new or added lines in 51 files covered. (22.65%)

5590 existing lines in 163 files now uncovered.

13345 of 156987 relevant lines covered (8.5%)

22.88 hits per line

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

0.0
/src/Metadata/Tests/Extractor/Adapter/XmlPropertyAdapter.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\Tests\Extractor\Adapter;
15

16
use ApiPlatform\Metadata\Tests\Extractor\PropertyMetadataCompatibilityTest;
17

18
/**
19
 * XML adapter for PropertyMetadataCompatibilityTest.
20
 *
21
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
22
 */
23
final class XmlPropertyAdapter implements PropertyAdapterInterface
24
{
25
    private const ATTRIBUTES = [
26
        'resource',
27
        'name',
28
        'description',
29
        'readable',
30
        'writable',
31
        'readableLink',
32
        'writableLink',
33
        'required',
34
        'identifier',
35
        'default',
36
        'example',
37
        'deprecationReason',
38
        'fetchable',
39
        'fetchEager',
40
        'push',
41
        'security',
42
        'securityPostDenormalize',
43
        'initializable',
44
        'iris',
45
        'genId',
46
        'uriTemplate',
47
        'hydra',
48
        'property',
49
    ];
50

51
    // TODO: add serialize support for XML (policy is Laravel-only)
52
    private const EXCLUDE = ['policy', 'serialize'];
53

54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function __invoke(string $resourceClass, string $propertyName, array $parameters, array $fixtures): array
58
    {
UNCOV
59
        $xml = new \SimpleXMLElement(<<<XML_WRAP
×
60
<?xml version="1.0" encoding="UTF-8" ?>
61
<properties xmlns="https://api-platform.com/schema/metadata/properties-3.0"
62
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
63
           xsi:schemaLocation="https://api-platform.com/schema/metadata/properties-3.0
64
           https://api-platform.com/schema/metadata/properties-3.0.xsd">
65
</properties>
UNCOV
66
XML_WRAP
×
67
        );
×
68

69
        $property = $xml->addChild('property');
×
UNCOV
70
        $property->addAttribute('name', $propertyName);
×
71
        $property->addAttribute('resource', $resourceClass);
×
72

73
        foreach ($parameters as $parameter) {
×
74
            $parameterName = $parameter->getName();
×
UNCOV
75
            if (\in_array($parameterName, self::EXCLUDE, true)) {
×
UNCOV
76
                continue;
×
77
            }
78

79
            $value = \array_key_exists($parameterName, $fixtures) ? $fixtures[$parameterName] : null;
×
80

81
            if (method_exists($this, 'build'.ucfirst($parameterName))) {
×
UNCOV
82
                $this->{'build'.ucfirst($parameterName)}($property, $value);
×
UNCOV
83
                continue;
×
84
            }
85

86
            if (\in_array($parameterName, self::ATTRIBUTES, true) && \is_scalar($value)) {
×
UNCOV
87
                $property->addAttribute($parameterName, $this->parse($value));
×
UNCOV
88
                continue;
×
89
            }
90

UNCOV
91
            throw new \LogicException(\sprintf('Cannot adapt attribute or child "%s". Please add fixtures in '.PropertyMetadataCompatibilityTest::class.' and create a "%s" method in %s.', $parameterName, 'build'.ucfirst($parameterName), self::class));
×
92
        }
93

UNCOV
94
        $filename = __DIR__.'/properties.xml';
×
95
        $xml->asXML($filename);
×
96

UNCOV
97
        return [$filename];
×
98
    }
99

100
    private function buildBuiltinTypes(\SimpleXMLElement $resource, array $values): void
101
    {
102
        $node = $resource->addChild('builtinTypes');
×
UNCOV
103
        foreach ($values as $value) {
×
UNCOV
104
            $node->addChild('builtinType', $value);
×
105
        }
106
    }
107

108
    private function buildSchema(\SimpleXMLElement $resource, array $values): void
109
    {
UNCOV
110
        $this->buildValues($resource->addChild('schema'), $values);
×
111
    }
112

113
    private function buildTypes(\SimpleXMLElement $resource, array $values): void
114
    {
115
        $node = $resource->addChild('types');
×
UNCOV
116
        foreach ($values as $value) {
×
UNCOV
117
            $node->addChild('type', $value);
×
118
        }
119
    }
120

121
    private function buildIris(\SimpleXMLElement $resource, array $values): void
122
    {
123
        $node = $resource->addChild('iris');
×
UNCOV
124
        foreach ($values as $value) {
×
UNCOV
125
            $node->addChild('iri', $value);
×
126
        }
127
    }
128

129
    private function buildJsonldContext(\SimpleXMLElement $resource, array $values): void
130
    {
UNCOV
131
        $this->buildValues($resource->addChild('jsonldContext'), $values);
×
132
    }
133

134
    private function buildOpenapiContext(\SimpleXMLElement $resource, array $values): void
135
    {
UNCOV
136
        $this->buildValues($resource->addChild('openapiContext'), $values);
×
137
    }
138

139
    private function buildJsonSchemaContext(\SimpleXMLElement $resource, array $values): void
140
    {
UNCOV
141
        $this->buildValues($resource->addChild('jsonSchemaContext'), $values);
×
142
    }
143

144
    private function buildExtraProperties(\SimpleXMLElement $resource, array $values): void
145
    {
UNCOV
146
        $this->buildValues($resource->addChild('extraProperties'), $values);
×
147
    }
148

149
    private function buildValues(\SimpleXMLElement $resource, array $values): void
150
    {
151
        $node = $resource->addChild('values');
×
152
        foreach ($values as $key => $value) {
×
153
            if (\is_array($value)) {
×
UNCOV
154
                $child = $node->addChild('value');
×
155
                $this->buildValues($child, $value);
×
156
            } else {
157
                $child = $node->addChild('value', (string) $value);
×
158
            }
UNCOV
159
            if (\is_string($key)) {
×
UNCOV
160
                $child->addAttribute('name', $key);
×
161
            }
162
        }
163
    }
164

165
    private function parse(string|int|float|bool|null $value): ?string
166
    {
UNCOV
167
        if (null === $value) {
×
UNCOV
168
            return null;
×
169
        }
170

UNCOV
171
        return \is_bool($value) ? (true === $value ? 'true' : 'false') : (string) $value;
×
172
    }
173
}
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