Skip to content

Commit a44d8e0

Browse files
committed
if comment has a textual value, we dont serialize the <Attachment> element anymore. See DTD:
1 parent bfb9aa2 commit a44d8e0

File tree

6 files changed

+72
-3
lines changed

6 files changed

+72
-3
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@
8787
]
8888
},
8989
"config": {
90+
"platform": {
91+
"php": "8.2"
92+
},
9093
"allow-plugins": {
9194
"phpstan/extension-installer": true
9295
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CXml\Jms;
6+
7+
use CXml\Model\Comment;
8+
use JMS\Serializer\Context;
9+
use JMS\Serializer\Exclusion\ExclusionStrategyInterface;
10+
use JMS\Serializer\Metadata\ClassMetadata;
11+
use JMS\Serializer\Metadata\PropertyMetadata;
12+
use JMS\Serializer\SerializationContext;
13+
14+
class CXmlExclusionStrategy implements ExclusionStrategyInterface
15+
{
16+
public function shouldSkipClass(ClassMetadata $metadata, Context $context): bool
17+
{
18+
return false;
19+
}
20+
21+
/**
22+
* @param SerializationContext $context
23+
*/
24+
public function shouldSkipProperty(PropertyMetadata $property, Context $context): bool
25+
{
26+
// Check if we are serializing to XML
27+
if ('xml' !== $context->getFormat()) {
28+
return false;
29+
}
30+
31+
if (Comment::class === $property->class) {
32+
if ('attachment' === $property->name) {
33+
/** @var Comment $object */
34+
$object = $context->getObject();
35+
36+
// if comment has a textual value, we dont serialize the <Attachment> element anymore. See DTD:
37+
// <!ELEMENT Comments ( #PCDATA | Attachment )* >
38+
if (isset($object->value)) {
39+
return true;
40+
}
41+
}
42+
}
43+
44+
return false;
45+
}
46+
}

src/CXml/Model/Comment.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public function __construct(
1919
public ?string $type = null,
2020
#[Serializer\XmlAttribute(namespace: 'http://www.w3.org/XML/1998/namespace')]
2121
public ?string $lang = null,
22-
?string $attachment = null,
22+
?string $attachmentUrl = null,
2323
) {
24-
$this->attachment = (null === $attachment ? null : new Url($attachment));
24+
$this->attachment = (null === $attachmentUrl ? null : new Url($attachmentUrl));
2525
}
2626
}

src/CXml/Serializer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace CXml;
66

7+
use CXml\Jms\CXmlExclusionStrategy;
78
use CXml\Jms\CXmlWrappingNodeJmsEventSubscriber;
89
use CXml\Jms\JmsDateTimeHandler;
910
use CXml\Model\CXml;
@@ -15,6 +16,7 @@
1516
use JMS\Serializer\GraphNavigatorInterface;
1617
use JMS\Serializer\Handler\HandlerRegistry;
1718
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
19+
use JMS\Serializer\SerializationContext;
1820
use JMS\Serializer\SerializerBuilder;
1921
use JMS\Serializer\SerializerInterface;
2022
use JMS\Serializer\XmlDeserializationVisitor;
@@ -52,6 +54,10 @@ public static function create(): self
5254
->setPropertyNamingStrategy(
5355
new IdenticalPropertyNamingStrategy(),
5456
)
57+
->setSerializationContextFactory(function () {
58+
return SerializationContext::create()
59+
->addExclusionStrategy(new CXmlExclusionStrategy());
60+
})
5561
->build();
5662

5763
return new self($jmsSerializer);

tests/CXmlTest/Model/OrderRequestTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,20 @@ final class OrderRequestTest extends TestCase implements PayloadIdentityFactoryI
4141
{
4242
private DtdValidator $dtdValidator;
4343

44+
private Serializer $serializer;
45+
4446
protected function setUp(): void
4547
{
4648
$this->dtdValidator = DtdValidator::fromDtdDirectory(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/');
49+
$this->serializer = Serializer::create();
50+
}
51+
52+
public function testReference(): void
53+
{
54+
// test whether deserialize/serialize of the reference file works
55+
$xml = file_get_contents(__DIR__ . '/../../metadata/cxml/samples/OrderRequest.xml');
56+
$cxml = Serializer::create()->deserialize($xml);
57+
Serializer::create()->serialize($cxml);
4758
}
4859

4960
public function testMinimumExample(): void
@@ -171,6 +182,8 @@ public function testMinimumExample(): void
171182
),
172183
new DateTime('2020-02-28'),
173184
);
185+
$item->addComment(new Comment('Test comment', 'test', ''));
186+
174187
$orderRequest->addItem($item);
175188

176189
$cxml = Builder::create('Platform Order Fulfillment Hub', null, $this)
@@ -183,7 +196,7 @@ public function testMinimumExample(): void
183196
$this->assertSame('OrderRequest_1625586002.193314.7293@dev', (string)$cxml);
184197

185198
$xml = Serializer::create()->serialize($cxml);
186-
$this->assertXmlStringEqualsXmlFile('tests/metadata/cxml/samples/OrderRequest.xml', $xml);
199+
$this->assertXmlStringEqualsXmlFile(__DIR__ . '/../../metadata/cxml/samples/OrderRequest.xml', $xml);
187200

188201
$this->dtdValidator->validateAgainstDtd($xml);
189202
}

tests/metadata/cxml/samples/OrderRequest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
<UnitOfMeasure>EA</UnitOfMeasure>
102102
<Classification domain="custom">0</Classification>
103103
</ItemDetail>
104+
<Comments type="test" xml:lang="">Test comment</Comments>
104105
</ItemOut>
105106
</OrderRequest>
106107
</Request>

0 commit comments

Comments
 (0)