public function EntityNormalizer::normalize

Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()

Overrides NormalizerInterface::normalize

File

drupal/core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php, line 30
Contains \Drupal\hal\Normalizer\EntityNormalizer.

Class

EntityNormalizer
Converts the Drupal entity object structure to a HAL array structure.

Namespace

Drupal\hal\Normalizer

Code

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;
}