コード例 #1
0
 /**
  * {@inheritdoc}
  */
 public function normalize($object, $format = null, array $context = [])
 {
     $data = $this->collectionNormalizer->normalize($object, $format, $context);
     if (isset($context['jsonld_sub_level']) || !$object instanceof PaginatorInterface) {
         return $data;
     }
     $resource = $this->resourceResolver->guessResource($object, $context);
     list($parts, $parameters) = $this->parseRequestUri($resource, $context['request_uri']);
     $data['@type'] = self::HYDRA_PAGED_COLLECTION;
     $currentPage = $object->getCurrentPage();
     $lastPage = $object->getLastPage();
     if (1.0 !== $currentPage) {
         $previousPage = $currentPage - 1.0;
         $data['hydra:previousPage'] = $this->getPageUrl($resource, $parts, $parameters, $previousPage);
     }
     if ($currentPage !== $lastPage) {
         $data['hydra:nextPage'] = $this->getPageUrl($resource, $parts, $parameters, $currentPage + 1.0);
     }
     $data['hydra:totalItems'] = $object->getTotalItems();
     $data['hydra:itemsPerPage'] = $object->getItemsPerPage();
     $data['hydra:firstPage'] = $this->getPageUrl($resource, $parts, $parameters, 1.0);
     $data['hydra:lastPage'] = $this->getPageUrl($resource, $parts, $parameters, $lastPage);
     // Reorder the `hydra:member` key to the end
     $members = $data['hydra:member'];
     unset($data['hydra:member']);
     $data['hydra:member'] = $members;
     return $data;
 }
コード例 #2
0
 /**
  * {@inheritdoc}
  */
 public function normalize($object, $format = null, array $context = [])
 {
     $data = $this->collectionNormalizer->normalize($object, $format, $context);
     $resource = $this->resourceResolver->guessResource($object, $context);
     if (!isset($context['jsonld_sub_level'])) {
         $filters = $resource->getFilters();
         if (!empty($filters)) {
             $requestParts = parse_url($context['request_uri']);
             if (is_array($requestParts)) {
                 $data['hydra:search'] = $this->getSearch($resource, $requestParts, $filters);
             }
         }
     }
     return $data;
 }
コード例 #3
0
 /**
  * {@inheritdoc}
  */
 public function normalize($object, $format = null, array $context = [])
 {
     $resource = $this->resourceResolver->guessResource($object, $context);
     if (isset($context['jsonld_sub_level'])) {
         $data = [];
         foreach ($object as $index => $obj) {
             $data[$index] = $this->serializer->normalize($obj, $format, $context);
         }
     } else {
         $context = $this->createContext($resource, $context);
         $data = ['@context' => $this->contextBuilder->getResourceContext($resource, $context), '@id' => $context['request_uri'], '@type' => self::HYDRA_COLLECTION, 'hydra:member' => []];
         foreach ($object as $obj) {
             $data['hydra:member'][] = $this->serializer->normalize($obj, $format, $context);
         }
     }
     return $data;
 }
コード例 #4
0
 /**
  * {@inheritdoc}
  *
  * @throws RuntimeException
  * @throws InvalidArgumentException
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     if (!$this->serializer instanceof DenormalizerInterface) {
         throw new RuntimeException('The serializer must implement the DenormalizerInterface to denormalize relations.');
     }
     $resource = $this->resourceResolver->guessResource($data, $context, true);
     $normalizedData = $this->prepareForDenormalization($data);
     $context = $this->createContext($resource, $context);
     $attributesMetadata = $this->getMetadata($resource, $context)->getAttributesMetadata();
     $allowedAttributes = [];
     foreach ($attributesMetadata as $attributeName => $attributeMetadata) {
         if ($attributeMetadata->isWritable()) {
             $allowedAttributes[] = $attributeName;
         }
     }
     $reflectionClass = new \ReflectionClass($class);
     if (isset($data['@id']) && !isset($context['object_to_populate'])) {
         $context['object_to_populate'] = $this->iriConverter->getItemFromIri($data['@id']);
         // Avoid issues with proxies if we populated the object
         $overrideClass = true;
     } else {
         $overrideClass = false;
     }
     $object = $this->instantiateObject($normalizedData, $overrideClass ? get_class($context['object_to_populate']) : $class, $context, $reflectionClass, $allowedAttributes);
     foreach ($normalizedData as $attributeName => $attributeValue) {
         // Ignore JSON-LD special attributes
         if ('@' === $attributeName[0]) {
             continue;
         }
         if ($this->nameConverter) {
             $attributeName = $this->nameConverter->denormalize($attributeName);
         }
         if (!in_array($attributeName, $allowedAttributes) || in_array($attributeName, $this->ignoredAttributes)) {
             continue;
         }
         $type = $attributesMetadata[$attributeName]->getType();
         if ($type && $attributeValue) {
             if ($type->isCollection() && ($collectionType = $type->getCollectionType()) && ($class = $collectionType->getClass())) {
                 if (!is_array($attributeValue)) {
                     continue;
                 }
                 $values = [];
                 foreach ($attributeValue as $index => $obj) {
                     $values[$index] = $this->denormalizeRelation($resource, $attributeName, $attributesMetadata[$attributeName], $class, $obj, $context);
                 }
                 $this->setValue($object, $attributeName, $values);
                 continue;
             }
             if ($class = $type->getClass()) {
                 $this->setValue($object, $attributeName, $this->denormalizeRelation($resource, $attributeName, $attributesMetadata[$attributeName], $class, $attributeValue, $context));
                 continue;
             }
         }
         $this->setValue($object, $attributeName, $attributeValue);
     }
     return $object;
 }