BooleanNormalizer.php in Drupal 9
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;
class BooleanNormalizer extends NormalizerBase implements DenormalizerInterface {
protected $supportedInterfaceOrClass = BooleanData::class;
public function normalize($object, $format = NULL, array $context = []) {
return $object
->getValue() ? '๐' : '๐';
}
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
Name |
Description |
BooleanNormalizer |
Normalizes boolean data weirdly: renders them as ๐ (TRUE) or ๐ (FALSE). |