Skip to content

Commit 20696f5

Browse files
author
Maximilian Ruta
committed
[Serializer] Add CDATA_WRAPPING_NAME_PATTERN support to XmlEncoder
1 parent 98b9edc commit 20696f5

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/Symfony/Component/Serializer/Encoder/XmlEncoder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ private function buildXml(\DOMNode $parentNode, mixed $data, string $format, arr
418418
return $this->appendNode($parentNode, $data, $format, $context, 'data');
419419
}
420420

421-
throw new NotEncodableValueException('An unexpected value could not be serialized: '.(!\is_resource($data) ? var_export($data, true) : \sprintf('%s resource', get_resource_type($data))));
421+
throw new NotEncodableValueException('An unexpected value could not be serialized: '.(!\is_resource($data) ? var_export($data, true) : \sprintf('"%s" resource', get_resource_type($data))));
422422
}
423423

424424
/**
@@ -447,10 +447,10 @@ private function appendNode(\DOMNode $parentNode, mixed $data, string $format, a
447447
*/
448448
private function needsCdataWrapping(string $name, string $val, array $context): bool
449449
{
450-
return ($context[self::CDATA_WRAPPING] ?? $this->defaultContext[self::CDATA_WRAPPING]) &&
451-
(preg_match($context[self::CDATA_WRAPPING_PATTERN] ?? $this->defaultContext[self::CDATA_WRAPPING_PATTERN], $val) ||
452-
(($context[self::CDATA_WRAPPING_NAME_PATTERN] ?? $this->defaultContext[self::CDATA_WRAPPING_NAME_PATTERN]) && preg_match($context[self::CDATA_WRAPPING_NAME_PATTERN] ?? $this->defaultContext[self::CDATA_WRAPPING_NAME_PATTERN], $name))
453-
);
450+
return ($context[self::CDATA_WRAPPING] ?? $this->defaultContext[self::CDATA_WRAPPING])
451+
&& (preg_match($context[self::CDATA_WRAPPING_PATTERN] ?? $this->defaultContext[self::CDATA_WRAPPING_PATTERN], $val)
452+
|| (($context[self::CDATA_WRAPPING_NAME_PATTERN] ?? $this->defaultContext[self::CDATA_WRAPPING_NAME_PATTERN]) && preg_match($context[self::CDATA_WRAPPING_NAME_PATTERN] ?? $this->defaultContext[self::CDATA_WRAPPING_NAME_PATTERN], $name))
453+
);
454454
}
455455

456456
/**

src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,16 +572,16 @@ public function testCDataNamePattern()
572572
{
573573
$expected = <<<'XML'
574574
<?xml version="1.0"?>
575-
<response><person><firstname><![CDATA[Benjamin]]></firstname><lastname><![CDATA[Alexandre]]></lastname><other><![CDATA[data]]></other></person><person><firstname><![CDATA[Damien]]></firstname><lastname><![CDATA[Clay]]></lastname><other><![CDATA[data]]></other></person></response>
575+
<response><person><firstname><![CDATA[Benjamin]]></firstname><lastname><![CDATA[Alexandre]]></lastname><other>data</other></person><person><firstname><![CDATA[Damien]]></firstname><lastname><![CDATA[Clay]]></lastname><other>data</other></person></response>
576576

577577
XML;
578578
$source = ['person' => [
579579
['firstname' => 'Benjamin', 'lastname' => 'Alexandre', 'other' => 'data'],
580580
['firstname' => 'Damien', 'lastname' => 'Clay', 'other' => 'data'],
581581
]];
582582

583-
$this->assertEquals($expected, $this->encoder->encode($source, 'xml',[
584-
XmlEncoder::CDATA_WRAPPING_NAME_PATTERN => '/(firstname|lastname)/'
583+
$this->assertEquals($expected, $this->encoder->encode($source, 'xml', [
584+
XmlEncoder::CDATA_WRAPPING_NAME_PATTERN => '/(firstname|lastname)/',
585585
]));
586586
}
587587

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy