ApiPlatform\Core\JsonLd\ContextBuilder::getResourceContext PHP Метод

getResourceContext() публичный метод

public getResourceContext ( string $resourceClass, integer $referenceType = UrlGeneratorInterface::ABS_PATH ) : array
$resourceClass string
$referenceType integer
Результат array
    public function getResourceContext(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH) : array
    {
        $context = $this->getBaseContext($referenceType);
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
        $prefixedShortName = sprintf('#%s', $resourceMetadata->getShortName());
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
            if ($propertyMetadata->isIdentifier() && true !== $propertyMetadata->isWritable()) {
                continue;
            }
            $convertedName = $this->nameConverter ? $this->nameConverter->normalize($propertyName) : $propertyName;
            $jsonldContext = $propertyMetadata->getAttributes()['jsonld_context'] ?? [];
            if (!($id = $propertyMetadata->getIri())) {
                $id = sprintf('%s/%s', $prefixedShortName, $convertedName);
            }
            if (true !== $propertyMetadata->isReadableLink()) {
                $jsonldContext += ['@id' => $id, '@type' => '@id'];
            }
            if (empty($jsonldContext)) {
                $context[$convertedName] = $id;
            } else {
                $context[$convertedName] = $jsonldContext + ['@id' => $id];
            }
        }
        return $context;
    }

Usage Example

Пример #1
0
 public function testResourceContextWithReverse()
 {
     $this->resourceMetadataFactoryProphecy->create($this->entityClass)->willReturn(new ResourceMetadata('DummyEntity'));
     $this->propertyNameCollectionFactoryProphecy->create($this->entityClass)->willReturn(new PropertyNameCollection(['dummyPropertyA']));
     $this->propertyMetadataFactoryProphecy->create($this->entityClass, 'dummyPropertyA')->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'Dummy property A', true, true, true, true, false, false, null, null, ['jsonld_context' => ['@reverse' => 'parent']]));
     $contextBuilder = new ContextBuilder($this->resourceNameCollectionFactoryProphecy->reveal(), $this->resourceMetadataFactoryProphecy->reveal(), $this->propertyNameCollectionFactoryProphecy->reveal(), $this->propertyMetadataFactoryProphecy->reveal(), $this->urlGeneratorProphecy->reveal());
     $expected = ['@vocab' => '#', 'hydra' => 'http://www.w3.org/ns/hydra/core#', 'dummyPropertyA' => ['@id' => '#DummyEntity/dummyPropertyA', '@reverse' => 'parent']];
     $this->assertEquals($expected, $contextBuilder->getResourceContext($this->entityClass));
 }