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

normalize() public method

public normalize ( $object, $format = null, array $context = [] )
$context array
    public function normalize($object, $format = null, array $context = [])
    {
        if (isset($context['api_sub_level'])) {
            $data = [];
            foreach ($object as $index => $obj) {
                $data[$index] = $this->normalizer->normalize($obj, $format, $context);
            }
            return $data;
        }
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null, true);
        $data = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
        $context = $this->initContext($resourceClass, $context);
        $data['@id'] = $this->iriConverter->getIriFromResourceClass($resourceClass);
        $data['@type'] = 'hydra:Collection';
        $data['hydra:member'] = [];
        foreach ($object as $obj) {
            $data['hydra:member'][] = $this->normalizer->normalize($obj, $format, $context);
        }
        if (is_array($object) || $object instanceof \Countable) {
            $data['hydra:totalItems'] = $object instanceof PaginatorInterface ? $object->getTotalItems() : count($object);
        }
        return $data;
    }

Usage Example

 public function testNormalizePaginator()
 {
     $paginatorProphecy = $this->prophesize(PaginatorInterface::class);
     $paginatorProphecy->getCurrentPage()->willReturn(3);
     $paginatorProphecy->getLastPage()->willReturn(7);
     $paginatorProphecy->getItemsPerPage()->willReturn(12);
     $paginatorProphecy->getTotalItems()->willReturn(1312);
     $paginatorProphecy->rewind()->shouldBeCalled();
     $paginatorProphecy->valid()->willReturn(true, false)->shouldBeCalled();
     $paginatorProphecy->current()->willReturn('foo')->shouldBeCalled();
     $paginatorProphecy->next()->willReturn()->shouldBeCalled();
     $paginator = $paginatorProphecy->reveal();
     $serializer = $this->prophesize(SerializerInterface::class);
     $serializer->willImplement(NormalizerInterface::class);
     $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
     $resourceClassResolverProphecy->getResourceClass($paginator, null, true)->willReturn('Foo')->shouldBeCalled();
     $iriConvert = $this->prophesize(IriConverterInterface::class);
     $contextBuilder = $this->prophesize(ContextBuilderInterface::class);
     $contextBuilder->getResourceContextUri('Foo')->willReturn('/contexts/Foo');
     $iriConvert->getIriFromResourceClass('Foo')->willReturn('/foo/1');
     $itemNormalizer = $this->prophesize(AbstractItemNormalizer::class);
     $itemNormalizer->normalize('foo', null, ['jsonld_has_context' => true, 'api_sub_level' => true, 'resource_class' => 'Foo'])->willReturn(['name' => 'Kévin', 'friend' => 'Smail']);
     $normalizer = new CollectionNormalizer($contextBuilder->reveal(), $resourceClassResolverProphecy->reveal(), $iriConvert->reveal());
     $normalizer->setNormalizer($itemNormalizer->reveal());
     $this->assertEquals(['@context' => '/contexts/Foo', '@id' => '/foo/1', '@type' => 'hydra:Collection', 'hydra:member' => [0 => ['name' => 'Kévin', 'friend' => 'Smail']], 'hydra:totalItems' => 1312.0], $normalizer->normalize($paginator));
 }