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

api-platform / core / 10315659289

09 Aug 2024 07:49AM UTC coverage: 7.841% (-0.006%) from 7.847%
10315659289

push

github

soyuka
style: cs fixes

70 of 529 new or added lines in 176 files covered. (13.23%)

160 existing lines in 58 files now uncovered.

12688 of 161818 relevant lines covered (7.84%)

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

65
        $property = $xml->addChild('property');
×
66
        $property->addAttribute('name', $propertyName);
×
67
        $property->addAttribute('resource', $resourceClass);
×
68

69
        foreach ($parameters as $parameter) {
×
70
            $parameterName = $parameter->getName();
×
71
            $value = \array_key_exists($parameterName, $fixtures) ? $fixtures[$parameterName] : null;
×
72

73
            if (method_exists($this, 'build'.ucfirst($parameterName))) {
×
74
                $this->{'build'.ucfirst($parameterName)}($property, $value);
×
75
                continue;
×
76
            }
77

78
            if (\in_array($parameterName, self::ATTRIBUTES, true) && \is_scalar($value)) {
×
79
                $property->addAttribute($parameterName, $this->parse($value));
×
80
                continue;
×
81
            }
82

NEW
83
            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));
×
84
        }
85

86
        $filename = __DIR__.'/properties.xml';
×
87
        $xml->asXML($filename);
×
88

89
        return [$filename];
×
90
    }
91

92
    private function buildBuiltinTypes(\SimpleXMLElement $resource, array $values): void
93
    {
94
        $node = $resource->addChild('builtinTypes');
×
95
        foreach ($values as $value) {
×
96
            $node->addChild('builtinType', $value);
×
97
        }
98
    }
99

100
    private function buildSchema(\SimpleXMLElement $resource, array $values): void
101
    {
102
        $this->buildValues($resource->addChild('schema'), $values);
×
103
    }
104

105
    private function buildTypes(\SimpleXMLElement $resource, array $values): void
106
    {
107
        $node = $resource->addChild('types');
×
108
        foreach ($values as $value) {
×
109
            $node->addChild('type', $value);
×
110
        }
111
    }
112

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

121
    private function buildJsonldContext(\SimpleXMLElement $resource, array $values): void
122
    {
123
        $this->buildValues($resource->addChild('jsonldContext'), $values);
×
124
    }
125

126
    private function buildOpenapiContext(\SimpleXMLElement $resource, array $values): void
127
    {
128
        $this->buildValues($resource->addChild('openapiContext'), $values);
×
129
    }
130

131
    private function buildJsonSchemaContext(\SimpleXMLElement $resource, array $values): void
132
    {
133
        $this->buildValues($resource->addChild('jsonSchemaContext'), $values);
×
134
    }
135

136
    private function buildExtraProperties(\SimpleXMLElement $resource, array $values): void
137
    {
138
        $this->buildValues($resource->addChild('extraProperties'), $values);
×
139
    }
140

141
    private function buildValues(\SimpleXMLElement $resource, array $values): void
142
    {
143
        $node = $resource->addChild('values');
×
144
        foreach ($values as $key => $value) {
×
145
            if (\is_array($value)) {
×
146
                $child = $node->addChild('value');
×
147
                $this->buildValues($child, $value);
×
148
            } else {
149
                $child = $node->addChild('value', (string) $value);
×
150
            }
151
            if (\is_string($key)) {
×
152
                $child->addAttribute('name', $key);
×
153
            }
154
        }
155
    }
156

157
    private function parse(string|int|float|bool|null $value): ?string
158
    {
159
        if (null === $value) {
×
160
            return null;
×
161
        }
162

163
        return \is_bool($value) ? (true === $value ? 'true' : 'false') : (string) $value;
×
164
    }
165
}
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