function XmlEntityNormalizationQuirksTrait::applyXmlFieldDecodingQuirks

Same name in other branches
  1. 9 core/modules/rest/tests/src/Functional/EntityResource/XmlEntityNormalizationQuirksTrait.php \Drupal\Tests\rest\Functional\EntityResource\XmlEntityNormalizationQuirksTrait::applyXmlFieldDecodingQuirks()
  2. 10 core/modules/rest/tests/src/Functional/EntityResource/XmlEntityNormalizationQuirksTrait.php \Drupal\Tests\rest\Functional\EntityResource\XmlEntityNormalizationQuirksTrait::applyXmlFieldDecodingQuirks()
  3. 11.x core/modules/rest/tests/src/Functional/EntityResource/XmlEntityNormalizationQuirksTrait.php \Drupal\Tests\rest\Functional\EntityResource\XmlEntityNormalizationQuirksTrait::applyXmlFieldDecodingQuirks()

Applies the XML entity field encoding quirks that remain after decoding.

The XML encoding:

  • loses type data (int and bool become string)

Parameters

array $normalization: An entity normalization.

Return value

array The updated fieldable entity normalization.

See also

\Symfony\Component\Serializer\Encoder\XmlEncoder

1 call to XmlEntityNormalizationQuirksTrait::applyXmlFieldDecodingQuirks()
XmlEntityNormalizationQuirksTrait::getExpectedNormalizedEntity in core/modules/rest/tests/src/Functional/EntityResource/XmlEntityNormalizationQuirksTrait.php

File

core/modules/rest/tests/src/Functional/EntityResource/XmlEntityNormalizationQuirksTrait.php, line 57

Class

XmlEntityNormalizationQuirksTrait
Trait for EntityResourceTestBase subclasses testing $format='xml'.

Namespace

Drupal\Tests\rest\Functional\EntityResource

Code

protected function applyXmlFieldDecodingQuirks(array $normalization) {
    foreach ($this->entity
        ->getFields(TRUE) as $field_name => $field) {
        // Not every field is accessible.
        if (!isset($normalization[$field_name])) {
            continue;
        }
        for ($i = 0; $i < count($normalization[$field_name]); $i++) {
            switch ($field->getItemDefinition()
                ->getClass()) {
                case BooleanItem::class:
                case StatusItem::class:
                    // @todo Remove the StatusItem case in
                    //   https://www.drupal.org/project/drupal/issues/2936864.
                    $value =& $normalization[$field_name][$i]['value'];
                    $value = $value === TRUE ? '1' : '0';
                    break;
                case IntegerItem::class:
                case ListIntegerItem::class:
                    $value =& $normalization[$field_name][$i]['value'];
                    $value = (string) $value;
                    break;
                case PathItem::class:
                    $pid =& $normalization[$field_name][$i]['pid'];
                    $pid = (string) $pid;
                    break;
                case EntityReferenceItem::class:
                case FileItem::class:
                    $target_id =& $normalization[$field_name][$i]['target_id'];
                    $target_id = (string) $target_id;
                    break;
                case ChangedItem::class:
                case CreatedItem::class:
                case TimestampItem::class:
                    $value =& $normalization[$field_name][$i]['value'];
                    if (is_numeric($value)) {
                        $value = (string) $value;
                    }
                    break;
                case ImageItem::class:
                    $height =& $normalization[$field_name][$i]['height'];
                    $height = (string) $height;
                    $width =& $normalization[$field_name][$i]['width'];
                    $width = (string) $width;
                    $target_id =& $normalization[$field_name][$i]['target_id'];
                    $target_id = (string) $target_id;
                    break;
            }
        }
        if (count($normalization[$field_name]) === 1) {
            $normalization[$field_name] = $normalization[$field_name][0];
        }
    }
    return $normalization;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.