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

api-platform / core / 11291330628

11 Oct 2024 11:13AM UTC coverage: 7.841% (+0.001%) from 7.84%
11291330628

push

github

soyuka
fix(jsonld): remove deprecated class

1 of 1 new or added line in 1 file covered. (100.0%)

307 existing lines in 10 files now uncovered.

12966 of 165362 relevant lines covered (7.84%)

27.05 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
        'property',
48
    ];
49

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

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

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

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

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

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

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

UNCOV
90
            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));
×
91
        }
92

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

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

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

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

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

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

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

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

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

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

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

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

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