Skip to content

Commit 3ab1344

Browse files
committed
* Ability to provide custom dtd to serializer
1 parent 6855c48 commit 3ab1344

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/CXml/Serializer.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
{
3131
public const DOC_TYPE_VERSION = '1.2.063';
3232

33-
private function __construct(private SerializerInterface $jmsSerializer)
34-
{
33+
private function __construct(
34+
private SerializerInterface $jmsSerializer,
35+
private string $dtdUri,
36+
) {
3537
}
3638

37-
public static function create(): self
39+
public static function create(string $dtdUri = 'http://xml.cxml.org/schemas/cXML/' . self::DOC_TYPE_VERSION . '/cXML.dtd'): self
3840
{
3941
$jmsSerializer = SerializerBuilder::create()
4042
->configureListeners(static function (EventDispatcherInterface $dispatcher): void {
@@ -55,7 +57,7 @@ public static function create(): self
5557
)
5658
->build();
5759

58-
return new self($jmsSerializer);
60+
return new self($jmsSerializer, $dtdUri);
5961
}
6062

6163
public function deserialize(string $xml): CXml
@@ -71,11 +73,11 @@ public function deserialize(string $xml): CXml
7173
return $this->jmsSerializer->deserialize($xml, CXml::class, 'xml');
7274
}
7375

74-
public function serialize(CXml $cxml, string $docTypeVersion = self::DOC_TYPE_VERSION): string
76+
public function serialize(CXml $cxml): string
7577
{
7678
$xml = $this->jmsSerializer->serialize($cxml, 'xml');
7779

78-
$docType = '<!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/' . $docTypeVersion . '/cXML.dtd">';
80+
$docType = '<!DOCTYPE cXML SYSTEM "' . $this->dtdUri . '">' . "\n";
7981
$xmlPrefix = '<?xml version="1.0" encoding="UTF-8"?>';
8082

8183
// add doctype, as it is mandatory in cXML

0 commit comments

Comments
 (0)