BooleanNormalizer.php

Same filename and directory in other branches
  1. 8.9.x core/modules/serialization/tests/modules/test_datatype_boolean_emoji_normalizer/src/Normalizer/BooleanNormalizer.php
  2. 10 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}
     */
    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 === '👍';
    }
    
    /**
     * {@inheritdoc}
     */
    public function hasCacheableSupportsMethod() : bool {
        return 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.