Skip to main content

Class private__hUcpt8lmKwR7NOsrVVwoo_P62QKlsXwHDRwNPXgW5EU - Source Code

Primary tabs

API docs: https://api.drupal.org/api/drupal/core!modules!serialization!src!Normalizer!ListNormalizer.php/11.3.x

GitLab: https://git.drupalcode.org/project/drupal/-/blob/11.3.x/core/modules/serialization/src/Normalizer/ListNormalizer.php

Location: /core/modules/serialization/src/Normalizer/ListNormalizer.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}
   */
  public function normalize($object, $format = NULL, array $context = []): array|string|int|float|bool|\ArrayObject|NULL {
    $attributes = [];
    foreach ($object as $fieldItem) {
      $attributes[] = $this->serializer->normalize($fieldItem, $format, $context);
    }
    return $attributes;
  }

  /**
   * {@inheritdoc}
   */
  public function getSupportedTypes(?string $format): array {
    return [
      ListInterface::class => TRUE,
    ];
  }

}