diff --git a/src/Lib/XmlPatterns.php b/src/Lib/XmlPatterns.php new file mode 100644 index 0000000..86a4d1b --- /dev/null +++ b/src/Lib/XmlPatterns.php @@ -0,0 +1,50 @@ + + (?(DEFINE) + (? [:A-Z_a-z\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\x{2FF}\\x{370}-\\x{37D}\\x{37F}-\\x{1FFF}\\x{200C}-\\x{200D}\\x{2070}-\\x{218F}\\x{2C00}-\\x{2FEF}\\x{3001}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFFD}\\x{10000}-\\x{EFFFF}]) + (? (?&NameStartChar) | [.\\-0-9\\xB7\\x{0300}-\\x{036F}\\x{203F}-\\x{2040}]) + (? (?&NameStartChar) (?&NameChar)*) + ) + ^(?&Name)$ + ~ux'; + } + + /** + * Get a regex pattern for valid tag chars + * + * @return string + */ + public static function getValidXmlTagNameChar() + { + return '~ + (?(DEFINE) + (? [:A-Z_a-z\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\x{2FF}\\x{370}-\\x{37D}\\x{37F}-\\x{1FFF}\\x{200C}-\\x{200D}\\x{2070}-\\x{218F}\\x{2C00}-\\x{2FEF}\\x{3001}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFFD}\\x{10000}-\\x{EFFFF}]) + (? (?&NameStartChar) | [.\\-0-9\\xB7\\x{0300}-\\x{036F}\\x{203F}-\\x{2040}]) + ) + ^(?&NameChar)$ + ~ux'; + } + + /** + * Get a regex pattern for valid tag starting characters + * + * @return string + */ + public static function getValidXmlTagStartPattern() + { + return '~^([:A-Z_a-z\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\x{2FF}\\x{370}-\\x{37D}\\x{37F}-\\x{1FFF}\\x{200C}-\\x{200D}\\x{2070}-\\x{218F}\\x{2C00}-\\x{2FEF}\\x{3001}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFFD}\\x{10000}-\\x{EFFFF}])~ux'; + } +} \ No newline at end of file diff --git a/src/PhpArrayToXml.php b/src/PhpArrayToXml.php index af4a1e7..03c33d1 100644 --- a/src/PhpArrayToXml.php +++ b/src/PhpArrayToXml.php @@ -4,6 +4,7 @@ use DOMDocument; use DOMElement; +use RefactorStudio\PhpArrayToXml\Lib\XmlPatterns; class PhpArrayToXml { @@ -340,7 +341,7 @@ public function getCastNullValue() */ public static function hasValidXmlTagStartingChar($value = null) { - if (preg_match(self::getValidXmlTagStartPattern(), $value) === 1) { + if (preg_match(XmlPatterns::getValidXmlTagStartPattern(), $value) === 1) { return true; } return false; @@ -354,7 +355,7 @@ public static function hasValidXmlTagStartingChar($value = null) */ public static function isValidXmlTagChar($value = null) { - if (preg_match(self::getValidXmlTagNameChar(), $value) === 1) { + if (preg_match(XmlPatterns::getValidXmlTagNameChar(), $value) === 1) { return true; } return false; @@ -372,7 +373,7 @@ public static function isValidXmlTag($value = null) return false; } - if (preg_match(self::getValidXmlTagNamePattern(), $value) === 1) { + if (preg_match(XmlPatterns::getValidXmlTagNamePattern(), $value) === 1) { return true; } return false; @@ -398,50 +399,6 @@ public function toXmlString($array = []) return $this->_doc->saveXML(); } - /** - * Get a regex pattern for valid tag names - * - * @return string - */ - protected static function getValidXmlTagNamePattern() - { - return '~ - # XML 1.0 Name symbol PHP PCRE regex - (?(DEFINE) - (? [:A-Z_a-z\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\x{2FF}\\x{370}-\\x{37D}\\x{37F}-\\x{1FFF}\\x{200C}-\\x{200D}\\x{2070}-\\x{218F}\\x{2C00}-\\x{2FEF}\\x{3001}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFFD}\\x{10000}-\\x{EFFFF}]) - (? (?&NameStartChar) | [.\\-0-9\\xB7\\x{0300}-\\x{036F}\\x{203F}-\\x{2040}]) - (? (?&NameStartChar) (?&NameChar)*) - ) - ^(?&Name)$ - ~ux'; - } - - /** - * Get a regex pattern for valid tag chars - * - * @return string - */ - protected static function getValidXmlTagNameChar() - { - return '~ - (?(DEFINE) - (? [:A-Z_a-z\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\x{2FF}\\x{370}-\\x{37D}\\x{37F}-\\x{1FFF}\\x{200C}-\\x{200D}\\x{2070}-\\x{218F}\\x{2C00}-\\x{2FEF}\\x{3001}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFFD}\\x{10000}-\\x{EFFFF}]) - (? (?&NameStartChar) | [.\\-0-9\\xB7\\x{0300}-\\x{036F}\\x{203F}-\\x{2040}]) - ) - ^(?&NameChar)$ - ~ux'; - } - - /** - * Get a regex pattern for valid tag starting characters - * - * @return string - */ - protected static function getValidXmlTagStartPattern() - { - return '~^([:A-Z_a-z\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\x{2FF}\\x{370}-\\x{37D}\\x{37F}-\\x{1FFF}\\x{200C}-\\x{200D}\\x{2070}-\\x{218F}\\x{2C00}-\\x{2FEF}\\x{3001}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFFD}\\x{10000}-\\x{EFFFF}])~ux'; - } - /** * Converts arrays to DOMDocument elements *