ApiPlatform\Core\Hydra\Serializer\EntrypointNormalizer::normalize PHP Method

normalize() public method

public normalize ( $object, $format = null, array $context = [] )
$context array
    public function normalize($object, $format = null, array $context = [])
    {
        $entrypoint = ['@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'Entrypoint']), '@id' => $this->urlGenerator->generate('api_entrypoint'), '@type' => 'Entrypoint'];
        foreach ($object->getResourceNameCollection() as $resourceClass) {
            $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
            if (empty($resourceMetadata->getCollectionOperations())) {
                continue;
            }
            try {
                $entrypoint[lcfirst($resourceMetadata->getShortName())] = $this->iriConverter->getIriFromResourceClass($resourceClass);
            } catch (InvalidArgumentException $ex) {
                // Ignore resources without GET operations
            }
        }
        return $entrypoint;
    }

Usage Example

 public function testNormalize()
 {
     $collection = new ResourceNameCollection([Dummy::class]);
     $entrypoint = new Entrypoint($collection);
     $factoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
     $factoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadata('Dummy', null, null, null, ['get']))->shouldBeCalled();
     $iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
     $iriConverterProphecy->getIriFromResourceClass(Dummy::class)->willReturn('/api/dummies')->shouldBeCalled();
     $urlGeneratorProphecy = $this->prophesize(UrlGeneratorInterface::class);
     $urlGeneratorProphecy->generate('api_entrypoint')->willReturn('/api')->shouldBeCalled();
     $urlGeneratorProphecy->generate('api_jsonld_context', ['shortName' => 'Entrypoint'])->willReturn('/context/Entrypoint')->shouldBeCalled();
     $normalizer = new EntrypointNormalizer($factoryProphecy->reveal(), $iriConverterProphecy->reveal(), $urlGeneratorProphecy->reveal());
     $expected = ['@context' => '/context/Entrypoint', '@id' => '/api', '@type' => 'Entrypoint', 'dummy' => '/api/dummies'];
     $this->assertEquals($expected, $normalizer->normalize($entrypoint, EntrypointNormalizer::FORMAT));
 }