/** * {@inheritdoc} */ public function normalize($object, $format = null, array $context = array()) { $resource = $this->guessResource($object, $context); list($context, $data) = $this->contextBuilder->bootstrap($resource, $context); if (isset($context['json_ld_sub_level'])) { $data = []; foreach ($object as $index => $obj) { $data[$index] = $this->serializer->normalize($obj, $format, $context); } } else { $data['@id'] = $context['request_uri']; list($parts, $parameters) = $this->parseRequestUri($context['request_uri']); if ($object instanceof PaginatorInterface) { $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($parts, $parameters, $previousPage); } if ($currentPage !== $lastPage) { $data['hydra:nextPage'] = $this->getPageUrl($parts, $parameters, $currentPage + 1.0); } $data['hydra:totalItems'] = $object->getTotalItems(); $data['hydra:itemsPerPage'] = $object->getItemsPerPage(); $data['hydra:firstPage'] = $this->getPageUrl($parts, $parameters, 1.0); $data['hydra:lastPage'] = $this->getPageUrl($parts, $parameters, $lastPage); } else { $data['@type'] = self::HYDRA_COLLECTION; } $data['hydra:member'] = []; foreach ($object as $obj) { $data['hydra:member'][] = $this->serializer->normalize($obj, $format, $context); } $filters = $resource->getFilters(); if (!empty($filters)) { $data['hydra:search'] = $this->getSearch($resource, $parts, $filters); } } return $data; }
/** * {@inheritdoc} * * @throws RuntimeException * @throws CircularReferenceException * @throws InvalidArgumentException */ public function normalize($object, $format = null, array $context = []) { if (!$this->serializer instanceof NormalizerInterface) { throw new RuntimeException('The serializer must implement the NormalizerInterface.'); } if (is_object($object) && $this->isCircularReference($object, $context)) { return $this->handleCircularReference($object); } $resource = $this->guessResource($object, $context, true); list($context, $data) = $this->contextBuilder->bootstrap($resource, $context); // Don't use hydra:Collection in sub levels $context['json_ld_sub_level'] = true; $classMetadata = $this->getMetadata($resource, $context); $attributesMetadata = $classMetadata->getAttributes(); $data['@id'] = $this->iriConverter->getIriFromItem($object); $data['@type'] = ($iri = $classMetadata->getIri()) ? $iri : $resource->getShortName(); foreach ($attributesMetadata as $attributeMetadata) { $attributeName = $attributeMetadata->getName(); if ('id' === $attributeName || !$attributeMetadata->isReadable()) { continue; } $attributeValue = $this->propertyAccessor->getValue($object, $attributeName); if ($this->nameConverter) { $attributeName = $this->nameConverter->normalize($attributeName); } if (isset($attributeMetadata->getTypes()[0])) { $type = $attributeMetadata->getTypes()[0]; if ($attributeValue && $type->isCollection() && ($collectionType = $type->getCollectionType()) && ($class = $this->getClassHavingResource($collectionType))) { $values = []; foreach ($attributeValue as $obj) { $values[] = $this->normalizeRelation($resource, $attributeMetadata, $obj, $class); } $data[$attributeName] = $values; continue; } if ($attributeValue && ($class = $this->getClassHavingResource($type))) { $data[$attributeName] = $this->normalizeRelation($resource, $attributeMetadata, $attributeValue, $class); continue; } } $data[$attributeName] = $this->serializer->normalize($attributeValue, self::FORMAT, $context); } return $data; }