Completed
Push — master ( 0c3139...e3501a )
by Antoine
17s
created

src/JsonLd/ContextBuilder.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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\Core\JsonLd;
15
16
use ApiPlatform\Core\Api\UrlGeneratorInterface;
17
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
18
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
19
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
20
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
21
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
22
23
/**
24
 * {@inheritdoc}
25
 *
26
 * @author Kévin Dunglas <[email protected]>
27
 */
28
final class ContextBuilder implements ContextBuilderInterface
29
{
30
    const FORMAT = 'jsonld';
31
32
    private $resourceNameCollectionFactory;
33
    private $resourceMetadataFactory;
34
    private $propertyNameCollectionFactory;
35
    private $propertyMetadataFactory;
36
    private $urlGenerator;
37
38
    /**
39
     * @var NameConverterInterface
40
     */
41
    private $nameConverter;
42
43 View Code Duplication
    public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, UrlGeneratorInterface $urlGenerator, NameConverterInterface $nameConverter = null)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    {
45
        $this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
46
        $this->resourceMetadataFactory = $resourceMetadataFactory;
47
        $this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
48
        $this->propertyMetadataFactory = $propertyMetadataFactory;
49
        $this->urlGenerator = $urlGenerator;
50
        $this->nameConverter = $nameConverter;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getBaseContext(int $referenceType = UrlGeneratorInterface::ABS_URL): array
57
    {
58
        return [
59
            '@vocab' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT], UrlGeneratorInterface::ABS_URL).'#',
60
            'hydra' => self::HYDRA_NS,
61
        ];
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getEntrypointContext(int $referenceType = UrlGeneratorInterface::ABS_PATH): array
68
    {
69
        $context = $this->getBaseContext($referenceType);
70
71
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
72
            $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
73
74
            $resourceName = lcfirst($resourceMetadata->getShortName());
75
76
            $context[$resourceName] = [
77
                '@id' => 'Entrypoint/'.$resourceName,
78
                '@type' => '@id',
79
            ];
80
        }
81
82
        return $context;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getResourceContext(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH): array
89
    {
90
        $context = $this->getBaseContext($referenceType);
91
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
92
        $prefixedShortName = sprintf('#%s', $resourceMetadata->getShortName());
93
94
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
95
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
96
97
            if ($propertyMetadata->isIdentifier() && true !== $propertyMetadata->isWritable()) {
98
                continue;
99
            }
100
101
            $convertedName = $this->nameConverter ? $this->nameConverter->normalize($propertyName) : $propertyName;
102
            $jsonldContext = $propertyMetadata->getAttributes()['jsonld_context'] ?? [];
103
104
            if (!$id = $propertyMetadata->getIri()) {
105
                $id = sprintf('%s/%s', $prefixedShortName, $convertedName);
106
            }
107
108
            if (true !== $propertyMetadata->isReadableLink()) {
109
                $jsonldContext += [
110
                    '@id' => $id,
111
                    '@type' => '@id',
112
                ];
113
            }
114
115
            if (empty($jsonldContext)) {
116
                $context[$convertedName] = $id;
117
            } else {
118
                $context[$convertedName] = $jsonldContext + [
119
                    '@id' => $id,
120
                ];
121
            }
122
        }
123
124
        return $context;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function getResourceContextUri(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH): string
131
    {
132
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
133
134
        return $this->urlGenerator->generate('api_jsonld_context', ['shortName' => $resourceMetadata->getShortName()], $referenceType);
135
    }
136
}
137