Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
Overrides NormalizerInterface::normalize
public function normalize($entity, $format = NULL, array $context = array()) {
// Create the array of normalized properties, starting with the URI.
$normalized = array(
'_links' => array(
'self' => array(
'href' => $this
->getEntityUri($entity),
),
'type' => array(
'href' => $this->linkManager
->getTypeUri($entity
->entityType(), $entity
->bundle()),
),
),
);
// If the properties to use were specified, only output those properties.
// Otherwise, output all properties except internal ID.
if (isset($context['included_fields'])) {
foreach ($context['included_fields'] as $property_name) {
$properties[] = $entity
->get($property_name);
}
}
else {
$properties = $entity
->getProperties();
}
foreach ($properties as $property) {
// In some cases, Entity API will return NULL array items. Ensure this is
// a real property and that it is not the internal id.
if (!is_object($property) || $property
->getName() == 'id') {
continue;
}
$normalized_property = $this->serializer
->normalize($property, $format, $context);
$normalized = NestedArray::mergeDeep($normalized, $normalized_property);
}
return $normalized;
}