function PrimitiveDataNormalizer::doNormalize
Same name and namespace in other branches
- 11.x core/modules/serialization/src/Normalizer/PrimitiveDataNormalizer.php \Drupal\serialization\Normalizer\PrimitiveDataNormalizer::doNormalize()
Normalizes an object into a set of arrays/scalars.
Parameters
mixed $object: Object to normalize.
string|null $format: Format the normalization result will be encoded as.
array $context: Context options for the normalizer.
Return value
array|string|int|float|bool|\ArrayObject|null The normalization. An \ArrayObject is used to make sure an empty object is encoded as an object not an array.
Overrides SchematicNormalizerTrait::doNormalize
File
-
core/
modules/ serialization/ src/ Normalizer/ PrimitiveDataNormalizer.php, line 21
Class
- PrimitiveDataNormalizer
- Converts primitive data objects to their casted values.
Namespace
Drupal\serialization\NormalizerCode
public function doNormalize($object, $format = NULL, array $context = []) : array|string|int|float|bool|\ArrayObject|null {
// Add cacheability if applicable.
$this->addCacheableDependency($context, $object);
$parent = $object->getParent();
if ($parent instanceof FieldItemInterface && $object->getValue()) {
$serialized_property_names = $this->getCustomSerializedPropertyNames($parent);
if (in_array($object->getName(), $serialized_property_names, TRUE)) {
return unserialize($object->getValue());
}
}
// Typed data casts NULL objects to their empty variants, so for example
// the empty string ('') for string type data, or 0 for integer typed data.
// In a better world with typed data implementing algebraic data types,
// getCastedValue would return NULL, but as typed data is not aware of real
// optional values on the primitive level, we implement our own optional
// value normalization here.
return $object->getValue() === NULL ? NULL : $object->getCastedValue();
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.