Because I used 4 spaces indentation (for my XML documents) rather than default 2 spaces provided by the DOMDocument, I wanted a solution to configure the XMLSerializer.
$xml = $document->saveXML();
$xml = \preg_replace('/^[ ]+(?=<)/m','$0$0', $xml);
Of course adding an option argument on class constructor to make it configurable at runtime.
Hello,
Because I used 4 spaces indentation (for my XML documents) rather than default 2 spaces provided by the DOMDocument, I wanted a solution to configure the
XMLSerializer.As we cannot alter indendation via DOMDocument
I propose two alternatives :
the most simple and easy way (through a regex like suggested https://stackoverflow.com/questions/3325488/php-increase-indentation-of-domdocument-savexml)
Change DOMDocument by XMLWriter that is able to configure it (for example : https://github.com/phar-io/manifest/blob/2.0.3/src/ManifestSerializer.php#L43-L44)
I've already tested solution 1 and it works as expected :
Patching:
https://github.com/CycloneDX/cyclonedx-php-library/blob/v2.1.2/src/Core/Serialization/XmlSerializer.php#L78
Of course adding an option argument on class constructor to make it configurable at runtime.
What do you think ?