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

api-platform / core / 10510640710

22 Aug 2024 03:02PM UTC coverage: 7.708% (+0.005%) from 7.703%
10510640710

push

github

web-flow
feat(laravel): search filter (#6534)

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

9646 existing lines in 299 files now uncovered.

12490 of 162048 relevant lines covered (7.71%)

22.98 hits per line

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

96.91
/src/Hydra/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\Hydra\JsonSchema;
15

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

22
/**
23
 * Decorator factory which adds Hydra properties to the JSON Schema document.
24
 *
25
 * @author Kévin Dunglas <dunglas@gmail.com>
26
 */
27
final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareInterface
28
{
29
    private const BASE_PROP = [
30
        'readOnly' => true,
31
        'type' => 'string',
32
    ];
33
    private const BASE_PROPS = [
34
        '@id' => self::BASE_PROP,
35
        '@type' => self::BASE_PROP,
36
    ];
37
    private const BASE_ROOT_PROPS = [
38
        '@context' => [
39
            'readOnly' => true,
40
            'oneOf' => [
41
                ['type' => 'string'],
42
                [
43
                    'type' => 'object',
44
                    'properties' => [
45
                        '@vocab' => [
46
                            'type' => 'string',
47
                        ],
48
                        'hydra' => [
49
                            'type' => 'string',
50
                            'enum' => [ContextBuilder::HYDRA_NS],
51
                        ],
52
                    ],
53
                    'required' => ['@vocab', 'hydra'],
54
                    'additionalProperties' => true,
55
                ],
56
            ],
57
        ],
58
    ] + self::BASE_PROPS;
59

60
    public function __construct(private readonly SchemaFactoryInterface $schemaFactory)
61
    {
UNCOV
62
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
1,847✔
UNCOV
63
            $this->schemaFactory->setSchemaFactory($this);
1,847✔
64
        }
65
    }
66

67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function buildSchema(string $className, string $format = 'jsonld', string $type = Schema::TYPE_OUTPUT, ?Operation $operation = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema
71
    {
UNCOV
72
        $schema = $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
36✔
UNCOV
73
        if ('jsonld' !== $format) {
36✔
UNCOV
74
            return $schema;
36✔
75
        }
76

UNCOV
77
        if ('input' === $type) {
36✔
UNCOV
78
            return $schema;
36✔
79
        }
80

UNCOV
81
        $definitions = $schema->getDefinitions();
36✔
UNCOV
82
        if ($key = $schema->getRootDefinitionKey()) {
36✔
UNCOV
83
            $definitions[$key]['properties'] = self::BASE_ROOT_PROPS + ($definitions[$key]['properties'] ?? []);
36✔
84

UNCOV
85
            return $schema;
36✔
86
        }
UNCOV
87
        if ($key = $schema->getItemsDefinitionKey()) {
36✔
UNCOV
88
            $definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []);
36✔
89
        }
90

UNCOV
91
        if (($schema['type'] ?? '') === 'array') {
36✔
92
            // hydra:collection
UNCOV
93
            $items = $schema['items'];
36✔
UNCOV
94
            unset($schema['items']);
36✔
95

UNCOV
96
            switch ($schema->getVersion()) {
36✔
97
                // JSON Schema + OpenAPI 3.1
UNCOV
98
                case Schema::VERSION_OPENAPI:
36✔
99
                case Schema::VERSION_JSON_SCHEMA:
×
UNCOV
100
                    $nullableStringDefinition = ['type' => ['string', 'null']];
36✔
UNCOV
101
                    break;
36✔
102
                    // Swagger
103
                default:
104
                    $nullableStringDefinition = ['type' => 'string'];
×
105
                    break;
×
106
            }
107

UNCOV
108
            $schema['type'] = 'object';
36✔
UNCOV
109
            $schema['properties'] = [
36✔
UNCOV
110
                'hydra:member' => [
36✔
UNCOV
111
                    'type' => 'array',
36✔
UNCOV
112
                    'items' => $items,
36✔
UNCOV
113
                ],
36✔
UNCOV
114
                'hydra:totalItems' => [
36✔
UNCOV
115
                    'type' => 'integer',
36✔
UNCOV
116
                    'minimum' => 0,
36✔
UNCOV
117
                ],
36✔
UNCOV
118
                'hydra:view' => [
36✔
UNCOV
119
                    'type' => 'object',
36✔
UNCOV
120
                    'properties' => [
36✔
UNCOV
121
                        '@id' => [
36✔
UNCOV
122
                            'type' => 'string',
36✔
UNCOV
123
                            'format' => 'iri-reference',
36✔
UNCOV
124
                        ],
36✔
UNCOV
125
                        '@type' => [
36✔
UNCOV
126
                            'type' => 'string',
36✔
UNCOV
127
                        ],
36✔
UNCOV
128
                        'hydra:first' => [
36✔
UNCOV
129
                            'type' => 'string',
36✔
UNCOV
130
                            'format' => 'iri-reference',
36✔
UNCOV
131
                        ],
36✔
UNCOV
132
                        'hydra:last' => [
36✔
UNCOV
133
                            'type' => 'string',
36✔
UNCOV
134
                            'format' => 'iri-reference',
36✔
UNCOV
135
                        ],
36✔
UNCOV
136
                        'hydra:previous' => [
36✔
UNCOV
137
                            'type' => 'string',
36✔
UNCOV
138
                            'format' => 'iri-reference',
36✔
UNCOV
139
                        ],
36✔
UNCOV
140
                        'hydra:next' => [
36✔
UNCOV
141
                            'type' => 'string',
36✔
UNCOV
142
                            'format' => 'iri-reference',
36✔
UNCOV
143
                        ],
36✔
UNCOV
144
                    ],
36✔
UNCOV
145
                    'example' => [
36✔
UNCOV
146
                        '@id' => 'string',
36✔
UNCOV
147
                        'type' => 'string',
36✔
UNCOV
148
                        'hydra:first' => 'string',
36✔
UNCOV
149
                        'hydra:last' => 'string',
36✔
UNCOV
150
                        'hydra:previous' => 'string',
36✔
UNCOV
151
                        'hydra:next' => 'string',
36✔
UNCOV
152
                    ],
36✔
UNCOV
153
                ],
36✔
UNCOV
154
                'hydra:search' => [
36✔
UNCOV
155
                    'type' => 'object',
36✔
UNCOV
156
                    'properties' => [
36✔
UNCOV
157
                        '@type' => ['type' => 'string'],
36✔
UNCOV
158
                        'hydra:template' => ['type' => 'string'],
36✔
UNCOV
159
                        'hydra:variableRepresentation' => ['type' => 'string'],
36✔
UNCOV
160
                        'hydra:mapping' => [
36✔
UNCOV
161
                            'type' => 'array',
36✔
UNCOV
162
                            'items' => [
36✔
UNCOV
163
                                'type' => 'object',
36✔
UNCOV
164
                                'properties' => [
36✔
UNCOV
165
                                    '@type' => ['type' => 'string'],
36✔
UNCOV
166
                                    'variable' => ['type' => 'string'],
36✔
UNCOV
167
                                    'property' => $nullableStringDefinition,
36✔
UNCOV
168
                                    'required' => ['type' => 'boolean'],
36✔
UNCOV
169
                                ],
36✔
UNCOV
170
                            ],
36✔
UNCOV
171
                        ],
36✔
UNCOV
172
                    ],
36✔
UNCOV
173
                ],
36✔
UNCOV
174
            ];
36✔
UNCOV
175
            $schema['required'] = [
36✔
UNCOV
176
                'hydra:member',
36✔
UNCOV
177
            ];
36✔
178

UNCOV
179
            return $schema;
36✔
180
        }
181

UNCOV
182
        return $schema;
36✔
183
    }
184

185
    public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
186
    {
UNCOV
187
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
1,847✔
UNCOV
188
            $this->schemaFactory->setSchemaFactory($schemaFactory);
1,847✔
189
        }
190
    }
191
}
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

© 2026 Coveralls, Inc