-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathIdPropertyGenerator.php
More file actions
54 lines (43 loc) · 2.03 KB
/
IdPropertyGenerator.php
File metadata and controls
54 lines (43 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\SchemaGenerator\Schema\PropertyGenerator;
use ApiPlatform\SchemaGenerator\Model\Property;
use ApiPlatform\SchemaGenerator\Model\Type\PrimitiveType;
use ApiPlatform\SchemaGenerator\PropertyGenerator\IdPropertyGenerator as CommonIdPropertyGenerator;
use ApiPlatform\SchemaGenerator\PropertyGenerator\IdPropertyGeneratorInterface;
use ApiPlatform\SchemaGenerator\Schema\Model\Property as SchemaProperty;
use ApiPlatform\SchemaGenerator\Schema\Model\Type\PrimitiveType as SchemaPrimitiveType;
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfResource;
final class IdPropertyGenerator implements IdPropertyGeneratorInterface
{
private IdPropertyGeneratorInterface $idPropertyGenerator;
public function __construct(IdPropertyGeneratorInterface $idPropertyGenerator = null)
{
$this->idPropertyGenerator = $idPropertyGenerator ?? new CommonIdPropertyGenerator();
}
public function __invoke(string $generationStrategy, bool $supportsWritableId, Property $property = null): Property
{
$idProperty = ($this->idPropertyGenerator)($generationStrategy, $supportsWritableId, new SchemaProperty('id'));
if (!$idProperty instanceof SchemaProperty) {
throw new \LogicException(sprintf('ID property has to be an instance of "%s".', SchemaProperty::class));
}
$idProperty->type = $idProperty->type instanceof PrimitiveType ? new SchemaPrimitiveType($idProperty->type->name) : null;
$rangeName = 'Text';
$uri = 'https://schema.org/Text';
if ('auto' === $generationStrategy) {
$rangeName = 'Integer';
$uri = 'https://schema.org/Integer';
}
$idProperty->rangeName = $rangeName;
$idProperty->range = new RdfResource($uri);
return $idProperty;
}
}