public function FieldNormalizer::denormalize

Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize()

Overrides DenormalizerInterface::denormalize

File

drupal/core/modules/hal/lib/Drupal/hal/Normalizer/FieldNormalizer.php, line 65
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 LogicException('$context[\'target_instance\'] must be set to denormalize with the FieldNormalizer');
  }
  if ($context['target_instance']
    ->getParent() == NULL) {
    throw new LogicException('The field passed in via $context[\'target_instance\'] must have a parent set.');
  }
  $field = $context['target_instance'];
  foreach ($data as $field_item_data) {
    $count = $field
      ->count();

    // Get the next field item instance. The offset will serve as the field
    // item name.
    $field_item = $field
      ->offsetGet($count);
    $field_item_class = get_class($field_item);

    // Pass in the empty field item object as the target instance.
    $context['target_instance'] = $field_item;
    $this->serializer
      ->denormalize($field_item_data, $field_item_class, $format, $context);
  }
  return $field;
}