Skip to content

Extract regex patterns to its own class #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/Lib/XmlPatterns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace RefactorStudio\PhpArrayToXml\Lib;

class XmlPatterns
{
/**
* Get a regex pattern for valid tag names
*
* @return string
*/
public static function getValidXmlTagNamePattern()
{
return '~
# XML 1.0 Name symbol PHP PCRE regex <http://www.w3.org/TR/REC-xml/#NT-Name>
(?(DEFINE)
(?<NameStartChar> [: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}])
(?<NameChar> (?&NameStartChar) | [.\\-0-9\\xB7\\x{0300}-\\x{036F}\\x{203F}-\\x{2040}])
(?<Name> (?&NameStartChar) (?&NameChar)*)
)
^(?&Name)$
~ux';
}

/**
* Get a regex pattern for valid tag chars
*
* @return string
*/
public static function getValidXmlTagNameChar()
{
return '~
(?(DEFINE)
(?<NameStartChar> [: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}])
(?<NameChar> (?&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';
}
}
51 changes: 4 additions & 47 deletions src/PhpArrayToXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DOMDocument;
use DOMElement;
use RefactorStudio\PhpArrayToXml\Lib\XmlPatterns;

class PhpArrayToXml
{
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 <http://www.w3.org/TR/REC-xml/#NT-Name>
(?(DEFINE)
(?<NameStartChar> [: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}])
(?<NameChar> (?&NameStartChar) | [.\\-0-9\\xB7\\x{0300}-\\x{036F}\\x{203F}-\\x{2040}])
(?<Name> (?&NameStartChar) (?&NameChar)*)
)
^(?&Name)$
~ux';
}

/**
* Get a regex pattern for valid tag chars
*
* @return string
*/
protected static function getValidXmlTagNameChar()
{
return '~
(?(DEFINE)
(?<NameStartChar> [: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}])
(?<NameChar> (?&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
*
Expand Down