ApiPlatform\Core\Hal\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 = ['_links' => ['self' => ['href' => $this->urlGenerator->generate('api_entrypoint')]]];
        foreach ($object->getResourceNameCollection() as $resourceClass) {
            $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
            if (empty($resourceMetadata->getCollectionOperations())) {
                continue;
            }
            try {
                $entrypoint['_links'][lcfirst($resourceMetadata->getShortName())]['href'] = $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();
     $normalizer = new EntrypointNormalizer($factoryProphecy->reveal(), $iriConverterProphecy->reveal(), $urlGeneratorProphecy->reveal());
     $expected = ['_links' => ['self' => ['href' => '/api'], 'dummy' => ['href' => '/api/dummies']]];
     $this->assertEquals($expected, $normalizer->normalize($entrypoint, EntrypointNormalizer::FORMAT));
 }