You are here

class FieldNormalizer in Commerce Cart API 8

Field normalizer which flattens output.

Hierarchy

  • class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
    • class \Drupal\serialization\Normalizer\ListNormalizer
      • class \Drupal\serialization\Normalizer\FieldNormalizer implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface

Expanded class hierarchy of FieldNormalizer

1 string reference to 'FieldNormalizer'
commerce_cart_api.services.yml in ./commerce_cart_api.services.yml
commerce_cart_api.services.yml
1 service uses FieldNormalizer
commerce_cart_api.normalizer.field in ./commerce_cart_api.services.yml
Drupal\commerce_cart_api\Normalizer\FieldNormalizer

File

src/Normalizer/FieldNormalizer.php, line 12

Namespace

Drupal\commerce_cart_api\Normalizer
View source
class FieldNormalizer extends CoreFieldNormalizer {

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Constructs a new FieldNormalizer object.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   */
  public function __construct(RouteMatchInterface $route_match) {
    $this->routeMatch = $route_match;
  }

  /**
   * {@inheritdoc}
   */
  public function supportsNormalization($data, $format = NULL) {
    $supported = parent::supportsNormalization($data, $format);
    if ($supported) {
      $route = $this->routeMatch
        ->getRouteObject();
      return $route && $route
        ->hasRequirement('_cart_api');
    }
    return $supported;
  }

  /**
   * {@inheritdoc}
   */
  public function normalize($field_item, $format = NULL, array $context = []) {

    /** @var \Drupal\Core\Field\FieldItemListInterface $field_item */
    $cardinality = $field_item
      ->getFieldDefinition()
      ->getFieldStorageDefinition()
      ->getCardinality();
    $data = parent::normalize($field_item, $format, $context);
    if ($cardinality > 1 || $cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
      return $data;
    }
    if (empty($data)) {
      return NULL;
    }
    return reset($data);
  }

  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = NULL, array $context = []) {
    if (!is_array($data)) {
      $data = [
        $data,
      ];
    }
    return parent::denormalize($data, $class, $format, $context);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
FieldNormalizer::$routeMatch protected property The current route match.
FieldNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides ListNormalizer::$supportedInterfaceOrClass
FieldNormalizer::denormalize public function Denormalizes data back into an object of the given class. Overrides FieldNormalizer::denormalize
FieldNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. Overrides ListNormalizer::normalize
FieldNormalizer::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. Overrides NormalizerBase::supportsNormalization
FieldNormalizer::__construct public function Constructs a new FieldNormalizer object.
NormalizerBase::$format protected property List of formats which supports (de-)normalization. 3
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. 2
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() 1