Skip to content

Commit 4a21e8e

Browse files
authored
Extract regex patterns to its own class (#7)
* Extract regex patterns to its own class * temp * Add file again ( src/Lib/ )
1 parent ac9d006 commit 4a21e8e

File tree

2 files changed

+54
-47
lines changed

2 files changed

+54
-47
lines changed

src/Lib/XmlPatterns.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace RefactorStudio\PhpArrayToXml\Lib;
4+
5+
class XmlPatterns
6+
{
7+
/**
8+
* Get a regex pattern for valid tag names
9+
*
10+
* @return string
11+
*/
12+
public static function getValidXmlTagNamePattern()
13+
{
14+
return '~
15+
# XML 1.0 Name symbol PHP PCRE regex <http://www.w3.org/TR/REC-xml/#NT-Name>
16+
(?(DEFINE)
17+
(?<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}])
18+
(?<NameChar> (?&NameStartChar) | [.\\-0-9\\xB7\\x{0300}-\\x{036F}\\x{203F}-\\x{2040}])
19+
(?<Name> (?&NameStartChar) (?&NameChar)*)
20+
)
21+
^(?&Name)$
22+
~ux';
23+
}
24+
25+
/**
26+
* Get a regex pattern for valid tag chars
27+
*
28+
* @return string
29+
*/
30+
public static function getValidXmlTagNameChar()
31+
{
32+
return '~
33+
(?(DEFINE)
34+
(?<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}])
35+
(?<NameChar> (?&NameStartChar) | [.\\-0-9\\xB7\\x{0300}-\\x{036F}\\x{203F}-\\x{2040}])
36+
)
37+
^(?&NameChar)$
38+
~ux';
39+
}
40+
41+
/**
42+
* Get a regex pattern for valid tag starting characters
43+
*
44+
* @return string
45+
*/
46+
public static function getValidXmlTagStartPattern()
47+
{
48+
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';
49+
}
50+
}

src/PhpArrayToXml.php

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DOMDocument;
66
use DOMElement;
7+
use RefactorStudio\PhpArrayToXml\Lib\XmlPatterns;
78

89
class PhpArrayToXml
910
{
@@ -340,7 +341,7 @@ public function getCastNullValue()
340341
*/
341342
public static function hasValidXmlTagStartingChar($value = null)
342343
{
343-
if (preg_match(self::getValidXmlTagStartPattern(), $value) === 1) {
344+
if (preg_match(XmlPatterns::getValidXmlTagStartPattern(), $value) === 1) {
344345
return true;
345346
}
346347
return false;
@@ -354,7 +355,7 @@ public static function hasValidXmlTagStartingChar($value = null)
354355
*/
355356
public static function isValidXmlTagChar($value = null)
356357
{
357-
if (preg_match(self::getValidXmlTagNameChar(), $value) === 1) {
358+
if (preg_match(XmlPatterns::getValidXmlTagNameChar(), $value) === 1) {
358359
return true;
359360
}
360361
return false;
@@ -372,7 +373,7 @@ public static function isValidXmlTag($value = null)
372373
return false;
373374
}
374375

375-
if (preg_match(self::getValidXmlTagNamePattern(), $value) === 1) {
376+
if (preg_match(XmlPatterns::getValidXmlTagNamePattern(), $value) === 1) {
376377
return true;
377378
}
378379
return false;
@@ -398,50 +399,6 @@ public function toXmlString($array = [])
398399
return $this->_doc->saveXML();
399400
}
400401

401-
/**
402-
* Get a regex pattern for valid tag names
403-
*
404-
* @return string
405-
*/
406-
protected static function getValidXmlTagNamePattern()
407-
{
408-
return '~
409-
# XML 1.0 Name symbol PHP PCRE regex <http://www.w3.org/TR/REC-xml/#NT-Name>
410-
(?(DEFINE)
411-
(?<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}])
412-
(?<NameChar> (?&NameStartChar) | [.\\-0-9\\xB7\\x{0300}-\\x{036F}\\x{203F}-\\x{2040}])
413-
(?<Name> (?&NameStartChar) (?&NameChar)*)
414-
)
415-
^(?&Name)$
416-
~ux';
417-
}
418-
419-
/**
420-
* Get a regex pattern for valid tag chars
421-
*
422-
* @return string
423-
*/
424-
protected static function getValidXmlTagNameChar()
425-
{
426-
return '~
427-
(?(DEFINE)
428-
(?<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}])
429-
(?<NameChar> (?&NameStartChar) | [.\\-0-9\\xB7\\x{0300}-\\x{036F}\\x{203F}-\\x{2040}])
430-
)
431-
^(?&NameChar)$
432-
~ux';
433-
}
434-
435-
/**
436-
* Get a regex pattern for valid tag starting characters
437-
*
438-
* @return string
439-
*/
440-
protected static function getValidXmlTagStartPattern()
441-
{
442-
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';
443-
}
444-
445402
/**
446403
* Converts arrays to DOMDocument elements
447404
*

0 commit comments

Comments
 (0)