Skip to content

Commit

Permalink
* Ability to provide custom dtd to serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
mathielen committed Jan 28, 2025
1 parent 6855c48 commit ff5103a
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/CXml/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
{
public const DOC_TYPE_VERSION = '1.2.063';

private function __construct(private SerializerInterface $jmsSerializer)
{
}
private function __construct(
private SerializerInterface $jmsSerializer,
private string $dtdUri
)
{}

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

return new self($jmsSerializer);
return new self($jmsSerializer, $dtdUri);
}

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

public function serialize(CXml $cxml, string $docTypeVersion = self::DOC_TYPE_VERSION): string
public function serialize(CXml $cxml): string
{
$xml = $this->jmsSerializer->serialize($cxml, 'xml');

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

// add doctype, as it is mandatory in cXML
Expand Down

0 comments on commit ff5103a

Please sign in to comment.