BooleanNormalizer.php

Same filename and directory in other branches
  1. 9 core/modules/serialization/tests/modules/test_datatype_boolean_emoji_normalizer/src/Normalizer/BooleanNormalizer.php
  2. 8.9.x core/modules/serialization/tests/modules/test_datatype_boolean_emoji_normalizer/src/Normalizer/BooleanNormalizer.php
  3. 11.x core/modules/serialization/tests/modules/test_datatype_boolean_emoji_normalizer/src/Normalizer/BooleanNormalizer.php

Namespace

Drupal\test_datatype_boolean_emoji_normalizer\Normalizer

File

core/modules/serialization/tests/modules/test_datatype_boolean_emoji_normalizer/src/Normalizer/BooleanNormalizer.php

View source
<?php

namespace Drupal\test_datatype_boolean_emoji_normalizer\Normalizer;

use Drupal\Core\TypedData\Plugin\DataType\BooleanData;
use Drupal\serialization\Normalizer\NormalizerBase;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

/**
 * Normalizes boolean data weirdly: renders them as 👍 (TRUE) or 👎 (FALSE).
 */
class BooleanNormalizer extends NormalizerBase implements DenormalizerInterface {
  
  /**
   * {@inheritdoc}
   */
  public function normalize($object, $format = NULL, array $context = []) : array|string|int|float|bool|\ArrayObject|null {
    return $object->getValue() ? '👍' : '👎';
  }
  
  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = NULL, array $context = []) : mixed {
    if (!in_array($data, [
      '👍',
      '👎',
    ], TRUE)) {
      throw new \UnexpectedValueException('Only 👍 and 👎 are acceptable values.');
    }
    return $data === '👍';
  }
  
  /**
   * {@inheritdoc}
   */
  public function getSupportedTypes(?string $format) : array {
    return [
      BooleanData::class => TRUE,
    ];
  }

}

Classes

Title Deprecated Summary
BooleanNormalizer Normalizes boolean data weirdly: renders them as 👍 (TRUE) or 👎 (FALSE).

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.