1515
1616readonly class DtdValidator
1717{
18- public function __construct (private string $ pathToCxmlDtds )
18+ public function __construct (
19+ private array $ pathToDtds ,
20+ ) {
21+ Assertion::notEmpty ($ pathToDtds );
22+ }
23+
24+ public static function forDtdDirectory (string $ directory ): self
1925 {
20- Assertion::directory ($ pathToCxmlDtds );
21- Assertion::file ($ pathToCxmlDtds . '/cXML.dtd ' );
22- Assertion::file ($ pathToCxmlDtds . '/Fulfill.dtd ' );
23- Assertion::file ($ pathToCxmlDtds . '/Quote.dtd ' );
26+ Assertion::directory ($ directory );
27+
28+ $ pathToDtds = glob ($ directory . '/*.dtd ' );
29+
30+ return new self ($ pathToDtds );
2431 }
2532
2633 /**
@@ -38,12 +45,7 @@ public function validateAgainstDtd(string $xml): void
3845 $ old = new DOMDocument ();
3946 $ old ->loadXML ($ xml );
4047
41- $ validateFiles = ['cXML.dtd ' , 'Fulfill.dtd ' , 'Quote.dtd ' ];
42- if (file_exists ($ this ->pathToCxmlDtds . '/Custom.dtd ' )) {
43- $ validateFiles [] = 'Custom.dtd ' ;
44- }
45-
46- $ this ->validateAgainstMultipleDtd ($ validateFiles , $ old );
48+ $ this ->validateAgainstMultipleDtd ($ this ->pathToDtds , $ old );
4749
4850 // reset throwing of php errors for libxml
4951 libxml_use_internal_errors ($ internalErrors );
@@ -57,7 +59,7 @@ private function injectDtd(DOMDocument $originalDomDocument, string $dtdFilename
5759 $ creator = new DOMImplementation ();
5860
5961 try {
60- $ doctype = $ creator ->createDocumentType ('cXML ' , '' , $ this -> pathToCxmlDtds . ' / ' . $ dtdFilename );
62+ $ doctype = $ creator ->createDocumentType ('cXML ' , '' , $ dtdFilename );
6163 $ new = $ creator ->createDocument ('' , '' , $ doctype );
6264 } catch (DOMException $ domException ) {
6365 throw new CXmlInvalidException ($ domException ->getMessage (), (string )$ originalDomDocument ->saveXML (), $ domException );
0 commit comments