You are here

protected function FormatterHalJson::extractFieldValues in RESTful 7.2

Extracts the actual values from the resource fields.

Parameters

array|\Traversable|\stdClass $data: The array of rows.

Return value

array[] The array of prepared data.

Throws

\Drupal\restful\Exception\InternalServerErrorException

1 call to FormatterHalJson::extractFieldValues()
FormatterHalJson::prepare in src/Plugin/formatter/FormatterHalJson.php
Massages the raw data to create a structured array to pass to the renderer.

File

src/Plugin/formatter/FormatterHalJson.php, line 176
Contains \Drupal\restful\Plugin\formatter\FormatterHalJson.

Class

FormatterHalJson
Class FormatterHalJson @package Drupal\restful\Plugin\formatter

Namespace

Drupal\restful\Plugin\formatter

Code

protected function extractFieldValues($data, array $parents = array(), array &$parent_hashes = array()) {
  $output = array();
  if ($this
    ->isCacheEnabled($data)) {
    $parent_hashes[] = $this
      ->getCacheHash($data);
    if ($cache = $this
      ->getCachedData($data)) {
      return $cache->data;
    }
  }
  foreach ($data as $public_field_name => $resource_field) {
    if (!$resource_field instanceof ResourceFieldInterface) {

      // If $resource_field is not a ResourceFieldInterface it means that we
      // are dealing with a nested structure of some sort. If it is an array
      // we process it as a set of rows, if not then use the value directly.
      $parents[] = $public_field_name;
      $output[$public_field_name] = static::isIterable($resource_field) ? $this
        ->extractFieldValues($resource_field, $parents, $parent_hashes) : $resource_field;
      continue;
    }
    if (!$data instanceof ResourceFieldCollectionInterface) {
      throw new InternalServerErrorException('Inconsistent output.');
    }

    // This feels a bit awkward, but if the result is going to be cached, it
    // pays off the extra effort of generating the whole resource entity. That
    // way we can get a different field set with the previously cached entity.
    // If the entity is not going to be cached, then avoid generating the
    // field data altogether.
    $limit_fields = $data
      ->getLimitFields();
    if ($this
      ->isCacheEnabled($data) && $limit_fields && !in_array($resource_field
      ->getPublicName(), $limit_fields)) {

      // We are not going to cache this and this field is not in the output.
      continue;
    }
    $value = $resource_field
      ->render($data
      ->getInterpreter());

    // If the field points to a resource that can be included, include it
    // right away.
    if (static::isIterable($value) && $resource_field instanceof ResourceFieldResourceInterface) {
      $output += array(
        '_embedded' => array(),
      );
      $output['_embedded'][$this
        ->withCurie($public_field_name)] = $this
        ->extractFieldValues($value, $parents, $parent_hashes);
      continue;
    }
    $output[$public_field_name] = $value;
  }
  if ($this
    ->isCacheEnabled($data)) {
    $this
      ->setCachedData($data, $output, $parent_hashes);
  }
  return $output;
}