You are here

public function FieldNormalizer::denormalize in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/hal/src/Normalizer/FieldNormalizer.php \Drupal\hal\Normalizer\FieldNormalizer::denormalize()

Denormalizes data back into an object of the given class.

Parameters

mixed $data data to restore:

string $class the expected class to instantiate:

string $format format the given data was extracted from:

array $context options available to the denormalizer:

Return value

object

Overrides DenormalizerInterface::denormalize

File

core/modules/hal/src/Normalizer/FieldNormalizer.php, line 63
Contains \Drupal\hal\Normalizer\FieldNormalizer.

Class

FieldNormalizer
Converts the Drupal field structure to HAL array structure.

Namespace

Drupal\hal\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = array()) {
  if (!isset($context['target_instance'])) {
    throw new InvalidArgumentException('$context[\'target_instance\'] must be set to denormalize with the FieldNormalizer');
  }
  if ($context['target_instance']
    ->getParent() == NULL) {
    throw new InvalidArgumentException('The field passed in via $context[\'target_instance\'] must have a parent set.');
  }
  $items = $context['target_instance'];
  $item_class = $items
    ->getItemDefinition()
    ->getClass();
  foreach ($data as $item_data) {

    // Create a new item and pass it as the target for the unserialization of
    // $item_data. Note: if $item_data is about a different language than the
    // default, FieldItemNormalizer::denormalize() will dismiss this item and
    // create a new one for the right language.
    $context['target_instance'] = $items
      ->appendItem();
    $this->serializer
      ->denormalize($item_data, $item_class, $format, $context);
  }
  return $items;
}