You are here

BooleanNormalizer.php in Drupal 8

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}
   */
  protected $supportedInterfaceOrClass = BooleanData::class;

  /**
   * {@inheritdoc}
   */
  public function normalize($object, $format = NULL, array $context = []) {
    return $object
      ->getValue() ? '๐Ÿ‘' : '๐Ÿ‘Ž';
  }

  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = NULL, array $context = []) {
    if (!in_array($data, [
      '๐Ÿ‘',
      '๐Ÿ‘Ž',
    ], TRUE)) {
      throw new \UnexpectedValueException('Only ๐Ÿ‘ and ๐Ÿ‘Ž are acceptable values.');
    }
    return $data === '๐Ÿ‘';
  }

}

Classes

Namesort descending Description
BooleanNormalizer Normalizes boolean data weirdly: renders them as ๐Ÿ‘ (TRUE) or ๐Ÿ‘Ž (FALSE).