You are here

protected function XmlNormalizationQuirksTrait::applyXmlDecodingQuirks in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/rest/tests/src/Functional/XmlNormalizationQuirksTrait.php \Drupal\Tests\rest\Functional\XmlNormalizationQuirksTrait::applyXmlDecodingQuirks()
  2. 10 core/modules/rest/tests/src/Functional/XmlNormalizationQuirksTrait.php \Drupal\Tests\rest\Functional\XmlNormalizationQuirksTrait::applyXmlDecodingQuirks()

Applies the XML encoding quirks that remain after decoding.

The XML encoding:

  • maps empty arrays to the empty string
  • maps single-item arrays to just that single item
  • restructures multiple-item arrays that lives in a single-item array

Parameters

array $normalization: A normalization.

Return value

array The updated normalization.

See also

\Symfony\Component\Serializer\Encoder\XmlEncoder

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

File

core/modules/rest/tests/src/Functional/XmlNormalizationQuirksTrait.php, line 26

Class

XmlNormalizationQuirksTrait
Trait for ResourceTestBase subclasses testing $format='xml'.

Namespace

Drupal\Tests\rest\Functional

Code

protected function applyXmlDecodingQuirks(array $normalization) {
  foreach ($normalization as $key => $value) {
    if ($value === [] || $value === NULL) {
      $normalization[$key] = '';
    }
    elseif (is_array($value)) {

      // Collapse single-item numeric arrays to just the single item.
      if (count($value) === 1 && is_numeric(array_keys($value)[0]) && is_scalar($value[0])) {
        $value = $value[0];
      }
      elseif (count($value) === 1 && is_numeric(array_keys($value)[0]) && is_array(reset($value))) {
        $rewritten_value = [];
        foreach ($value[0] as $child_key => $child_value) {
          if (is_numeric(array_keys(reset($value))[0])) {
            $rewritten_value[$child_key] = [
              '@key' => $child_key,
            ] + $child_value;
          }
          else {
            $rewritten_value[$child_key] = $child_value;
          }
        }
        $value = $rewritten_value;
      }

      // If the post-quirk value is still an array after the above, recurse.
      if (is_array($value)) {
        $value = $this
          ->applyXmlDecodingQuirks($value);
      }

      // Store post-quirk value.
      $normalization[$key] = $value;
    }
  }
  return $normalization;
}