You are here

ListNormalizer.php in Drupal 9

File

core/modules/serialization/src/Normalizer/ListNormalizer.php
View source
<?php

namespace Drupal\serialization\Normalizer;

use Drupal\Core\TypedData\ListInterface;

/**
 * Converts list objects to arrays.
 *
 * Ordinarily, this would be handled automatically by Serializer, but since
 * there is a TypedDataNormalizer and the Field class extends TypedData, any
 * Field will be handled by that Normalizer instead of being traversed. This
 * class ensures that TypedData classes that also implement ListInterface are
 * traversed instead of simply returning getValue().
 */
class ListNormalizer extends NormalizerBase {

  /**
   * {@inheritdoc}
   */
  protected $supportedInterfaceOrClass = ListInterface::class;

  /**
   * {@inheritdoc}
   */
  public function normalize($object, $format = NULL, array $context = []) {
    $attributes = [];
    foreach ($object as $fieldItem) {
      $attributes[] = $this->serializer
        ->normalize($fieldItem, $format, $context);
    }
    return $attributes;
  }

}

Classes

Namesort descending Description
ListNormalizer Converts list objects to arrays.