/**
  * {@inheritdoc}
  */
 public function normalize($object, $format = null, array $context = [])
 {
     $data = $this->collectionNormalizer->normalize($object, $format, $context);
     if (isset($context['api_sub_level'])) {
         return $data;
     }
     if ($paginated = $object instanceof PaginatorInterface) {
         $currentPage = $object->getCurrentPage();
         $lastPage = $object->getLastPage();
         if (1.0 === $currentPage && 1.0 === $lastPage) {
             // Consider the collection not paginated if there is only one page
             $paginated = false;
         }
     }
     $parsed = IriHelper::parseIri($context['request_uri'] ?? '/', $this->pageParameterName);
     $appliedFilters = $parsed['parameters'];
     unset($appliedFilters[$this->enabledParameterName]);
     if ([] === $appliedFilters && !$paginated) {
         return $data;
     }
     $data['hydra:view'] = ['@id' => IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $paginated ? $currentPage : null), '@type' => 'hydra:PartialCollectionView'];
     if ($paginated) {
         $data['hydra:view']['hydra:first'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, 1.0);
         $data['hydra:view']['hydra:last'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $lastPage);
         if (1.0 !== $currentPage) {
             $data['hydra:view']['hydra:previous'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage - 1.0);
         }
         if ($currentPage !== $lastPage) {
             $data['hydra:view']['hydra:next'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage + 1.0);
         }
     }
     return $data;
 }
 /**
  * {@inheritdoc}
  */
 public function normalize($object, $format = null, array $context = [])
 {
     $data = [];
     if (isset($context['api_sub_level'])) {
         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);
     $context = $this->initContext($resourceClass, $context);
     $parsed = IriHelper::parseIri($context['request_uri'] ?? '/', $this->pageParameterName);
     $paginated = $isPaginator = $object instanceof PaginatorInterface;
     if ($isPaginator) {
         $currentPage = $object->getCurrentPage();
         $lastPage = $object->getLastPage();
         $itemsPerPage = $object->getItemsPerPage();
         $paginated = 1.0 !== $lastPage;
     }
     $data = ['_links' => ['self' => IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $paginated ? $currentPage : null)]];
     if ($paginated) {
         $data['_links']['first'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, 1.0);
         $data['_links']['last'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $lastPage);
         if (1.0 !== $currentPage) {
             $data['_links']['prev'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage - 1.0);
         }
         if ($currentPage !== $lastPage) {
             $data['_links']['next'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage + 1.0);
         }
     }
     foreach ($object as $obj) {
         $item = $this->normalizer->normalize($obj, $format, $context);
         $data['_embedded']['item'][] = $item;
         $data['_links']['item'][] = $item['_links']['self'];
     }
     if (is_array($object) || $object instanceof \Countable) {
         $data['totalItems'] = $object instanceof PaginatorInterface ? (int) $object->getTotalItems() : count($object);
     }
     if ($isPaginator) {
         $data['itemsPerPage'] = (int) $itemsPerPage;
     }
     return $data;
 }
Example #3
0
 public function testHelpers()
 {
     $parsed = ['parts' => ['path' => '/hello.json', 'query' => 'foo=bar&page=2&bar=3'], 'parameters' => ['foo' => 'bar', 'bar' => '3']];
     $this->assertEquals($parsed, IriHelper::parseIri('/hello.json?foo=bar&page=2&bar=3', 'page'));
     $this->assertEquals('/hello.json?foo=bar&bar=3&page=2', IriHelper::createIri($parsed['parts'], $parsed['parameters'], 'page', 2));
 }