X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fcore%2Fmodules%2Frest%2Ftests%2Fsrc%2FFunctional%2FEntityResource%2FXmlEntityNormalizationQuirksTrait.php;fp=web%2Fcore%2Fmodules%2Frest%2Ftests%2Fsrc%2FFunctional%2FEntityResource%2FXmlEntityNormalizationQuirksTrait.php;h=962d8e9d2ef470f2c466d81260a04076062bd9db;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hp=0000000000000000000000000000000000000000;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0;p=yaffs-website diff --git a/web/core/modules/rest/tests/src/Functional/EntityResource/XmlEntityNormalizationQuirksTrait.php b/web/core/modules/rest/tests/src/Functional/EntityResource/XmlEntityNormalizationQuirksTrait.php new file mode 100644 index 000000000..962d8e9d2 --- /dev/null +++ b/web/core/modules/rest/tests/src/Functional/EntityResource/XmlEntityNormalizationQuirksTrait.php @@ -0,0 +1,157 @@ +entity instanceof FieldableEntityInterface) { + $normalization = $this->applyXmlFieldDecodingQuirks($default_normalization); + } + else { + $normalization = $this->applyXmlConfigEntityDecodingQuirks($default_normalization); + } + $normalization = $this->applyXmlDecodingQuirks($normalization); + + return $normalization; + } + + /** + * Applies the XML entity field encoding quirks that remain after decoding. + * + * The XML encoding: + * - loses type data (int and bool become string) + * + * @param array $normalization + * An entity normalization. + * + * @return array + * The updated fieldable entity normalization. + * + * @see \Symfony\Component\Serializer\Encoder\XmlEncoder + */ + 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: + $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 (!empty($normalization[$field_name])) { + $normalization[$field_name] = $normalization[$field_name][0]; + } + } + + return $normalization; + } + + + /** + * Applies the XML config entity encoding quirks that remain after decoding. + * + * The XML encoding: + * - loses type data (int and bool become string) + * - converts single-item arrays into single items (non-arrays) + * + * @param array $normalization + * An entity normalization. + * + * @return array + * The updated config entity normalization. + * + * @see \Symfony\Component\Serializer\Encoder\XmlEncoder + */ + protected function applyXmlConfigEntityDecodingQuirks(array $normalization) { + $normalization = static::castToString($normalization); + + // When a single dependency is listed, it's not decoded into an array. + if (isset($normalization['dependencies'])) { + foreach ($normalization['dependencies'] as $dependency_type => $dependency_list) { + if (count($dependency_list) === 1) { + $normalization['dependencies'][$dependency_type] = $dependency_list[0]; + } + } + } + + return $normalization; + } + + /** + * {@inheritdoc} + */ + public function testPost() { + // Deserialization of the XML format is not supported. + $this->markTestSkipped(); + } + + /** + * {@inheritdoc} + */ + public function testPatch() { + // Deserialization of the XML format is not supported. + $this->markTestSkipped(); + } + +}