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

api-platform / core / 10943429050

19 Sep 2024 02:48PM UTC coverage: 7.647% (-0.03%) from 7.675%
10943429050

push

github

web-flow
feat: api-platform/json-hal component (#6621)

* feat: add hal support for laravel

* feat: quick review

* fix: typo & cs-fixer

* fix: typo in composer.json

* fix: cs-fixer & phpstan

* fix: forgot about hal item normalizer, therefore there's no more createbook nor updatebook test as Hal is a readonly format

0 of 94 new or added lines in 2 files covered. (0.0%)

9082 existing lines in 291 files now uncovered.

12629 of 165144 relevant lines covered (7.65%)

22.89 hits per line

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

97.22
/src/Hal/JsonSchema/SchemaFactory.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\Hal\JsonSchema;
15

16
use ApiPlatform\JsonSchema\Schema;
17
use ApiPlatform\JsonSchema\SchemaFactoryAwareInterface;
18
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
19
use ApiPlatform\Metadata\Operation;
20

21
/**
22
 * Decorator factory which adds HAL properties to the JSON Schema document.
23
 *
24
 * @author Kévin Dunglas <dunglas@gmail.com>
25
 * @author Jachim Coudenys <jachimcoudenys@gmail.com>
26
 */
27
final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareInterface
28
{
29
    private const HREF_PROP = [
30
        'href' => [
31
            'type' => 'string',
32
            'format' => 'iri-reference',
33
        ],
34
    ];
35
    private const BASE_PROPS = [
36
        '_links' => [
37
            'type' => 'object',
38
            'properties' => [
39
                'self' => [
40
                    'type' => 'object',
41
                    'properties' => self::HREF_PROP,
42
                ],
43
            ],
44
        ],
45
    ];
46

47
    public function __construct(private readonly SchemaFactoryInterface $schemaFactory)
48
    {
49
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
2,256✔
50
            $this->schemaFactory->setSchemaFactory($this);
2,256✔
51
        }
52
    }
53

54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function buildSchema(string $className, string $format = 'jsonhal', string $type = Schema::TYPE_OUTPUT, ?Operation $operation = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema
58
    {
59
        $schema = $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
177✔
60
        if ('jsonhal' !== $format) {
177✔
61
            return $schema;
147✔
62
        }
63

64
        $definitions = $schema->getDefinitions();
90✔
65
        if ($key = $schema->getRootDefinitionKey()) {
90✔
66
            $definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []);
84✔
67

68
            return $schema;
84✔
69
        }
70
        if ($key = $schema->getItemsDefinitionKey()) {
72✔
71
            $definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []);
72✔
72
        }
73

74
        if (($schema['type'] ?? '') === 'array') {
72✔
75
            $items = $schema['items'];
72✔
76
            unset($schema['items']);
72✔
77

78
            $schema['type'] = 'object';
72✔
79
            $schema['properties'] = [
72✔
80
                '_embedded' => [
72✔
81
                    'anyOf' => [
72✔
82
                        [
72✔
83
                            'type' => 'object',
72✔
84
                            'properties' => [
72✔
85
                                'item' => [
72✔
86
                                    'type' => 'array',
72✔
87
                                    'items' => $items,
72✔
88
                                ],
72✔
89
                            ],
72✔
90
                        ],
72✔
91
                        ['type' => 'object'],
72✔
92
                    ],
72✔
93
                ],
72✔
94
                'totalItems' => [
72✔
95
                    'type' => 'integer',
72✔
96
                    'minimum' => 0,
72✔
97
                ],
72✔
98
                'itemsPerPage' => [
72✔
99
                    'type' => 'integer',
72✔
100
                    'minimum' => 0,
72✔
101
                ],
72✔
102
                '_links' => [
72✔
103
                    'type' => 'object',
72✔
104
                    'properties' => [
72✔
105
                        'self' => [
72✔
106
                            'type' => 'object',
72✔
107
                            'properties' => self::HREF_PROP,
72✔
108
                        ],
72✔
109
                        'first' => [
72✔
110
                            'type' => 'object',
72✔
111
                            'properties' => self::HREF_PROP,
72✔
112
                        ],
72✔
113
                        'last' => [
72✔
114
                            'type' => 'object',
72✔
115
                            'properties' => self::HREF_PROP,
72✔
116
                        ],
72✔
117
                        'next' => [
72✔
118
                            'type' => 'object',
72✔
119
                            'properties' => self::HREF_PROP,
72✔
120
                        ],
72✔
121
                        'previous' => [
72✔
122
                            'type' => 'object',
72✔
123
                            'properties' => self::HREF_PROP,
72✔
124
                        ],
72✔
125
                    ],
72✔
126
                ],
72✔
127
            ];
72✔
128
            $schema['required'] = [
72✔
129
                '_links',
72✔
130
                '_embedded',
72✔
131
            ];
72✔
132

133
            return $schema;
72✔
134
        }
135

UNCOV
136
        return $schema;
39✔
137
    }
138

139
    public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
140
    {
141
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
×
142
            $this->schemaFactory->setSchemaFactory($schemaFactory);
×
143
        }
144
    }
145
}
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