You are here

BooleanItemNormalizer.php in Drupal 8

File

core/modules/serialization/tests/modules/test_fieldtype_boolean_emoji_normalizer/src/Normalizer/BooleanItemNormalizer.php
View source
<?php

namespace Drupal\test_fieldtype_boolean_emoji_normalizer\Normalizer;

use Drupal\Core\Field\Plugin\Field\FieldType\BooleanItem;
use Drupal\serialization\Normalizer\FieldItemNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

/**
 * Normalizes boolean fields weirdly: renders them as ๐Ÿ‘ (TRUE) or ๐Ÿ‘Ž (FALSE).
 */
class BooleanItemNormalizer extends FieldItemNormalizer implements DenormalizerInterface {

  /**
   * {@inheritdoc}
   */
  protected $supportedInterfaceOrClass = BooleanItem::class;

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

  /**
   * {@inheritdoc}
   */
  protected function constructValue($data, $context) {

    // Just like \Drupal\serialization\Normalizer\FieldItemNormalizer's logic
    // for denormalization, which uses TypedDataInterface::setValue(), allow the
    // keying by main property name ("value") to be implied.
    if (!is_array($data)) {
      $data = [
        'value' => $data,
      ];
    }
    if (!in_array($data['value'], [
      '๐Ÿ‘',
      '๐Ÿ‘Ž',
    ], TRUE)) {
      throw new \UnexpectedValueException('Only ๐Ÿ‘ and ๐Ÿ‘Ž are acceptable values.');
    }
    $data['value'] = $data['value'] === '๐Ÿ‘';
    return $data;
  }

}

Classes

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