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

api-platform / core / 18089937549

29 Sep 2025 07:56AM UTC coverage: 21.764% (-0.3%) from 22.093%
18089937549

Pull #7416

github

web-flow
Merge 061bcc790 into abe0438be
Pull Request #7416: fix(laravel): serializer attributes on Eloquent methods

0 of 151 new or added lines in 11 files covered. (0.0%)

5028 existing lines in 173 files now uncovered.

11889 of 54626 relevant lines covered (21.76%)

25.32 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
        'nativeType',
50
    ];
51

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

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

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

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

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

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

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

UNCOV
92
            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));
×
93
        }
94

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

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

101
    private function buildBuiltinTypes(\SimpleXMLElement $resource): void
102
    {
103
        // deprecated, to remove in 5.0
104
    }
×
105

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

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

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

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

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

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

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

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

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

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