Skip to content

Commit 3dd0578

Browse files
committed
new helper for xml files
1 parent 084b71c commit 3dd0578

10 files changed

Lines changed: 502 additions & 25 deletions

File tree

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ Filter::ucFirst(string $input): string; // First char to upper, other to lower
328328

329329
Filter::up(string $string): string; // String to upper and trim
330330

331-
Filter::xml(string $string): string; // Alias of "Str::escXml($string)"
331+
Filter::xml(string $string): string; // Alias of "Xml::escape($string)"
332332

333333
```
334334

@@ -524,7 +524,9 @@ Str::clean(string $string, bool $toLower = false, bool $addSlashes = false, bool
524524

525525
Str::esc(string $string): string; // Escape UTF-8 strings
526526

527-
Str::escXml(string $string): string; // Escape string before save it as xml content
527+
// Escape string before save it as xml content.
528+
// The function is moved. Please, use \JBZoo\Utils\Xml::escape($string). It'll be deprecated soon.
529+
Str::escXml(string $string): string;
528530

529531
Str::getClassName($object, bool $toLower = false): string; // Get class name without namespace
530532

@@ -638,7 +640,7 @@ Sys::hasPHPDBGCodeCoverage(): bool;
638640

639641
Sys::hasXdebug(): bool; // Returns true when the runtime used is PHP and Xdebug is loaded.
640642

641-
Sys::iniGet(string $varName): ?string; // Alias fo ini_get function
643+
Sys::iniGet(string $varName): string; // Alias fo ini_get function
642644

643645
Sys::iniSet(string $phpIniKey, string $newValue): bool; // Alias fo ini_set function
644646

@@ -746,6 +748,20 @@ Vars::relativePercent(float $normal, float $current): string; // Get relative pe
746748
```
747749

748750

751+
### JBZoo\Utils\Xml
752+
753+
```php
754+
Xml::array2Dom(array $xmlAsArray, ?DOMElement $domElement = null, ?DOMDocument $document = null): DOMDocument; // Convert array to PHP DOMDocument object
755+
756+
Xml::createFromString(?string $source = null): DOMDocument; // Create DOMDocument object from XML-string
757+
758+
Xml::dom2Array(DOMNode $element): array; // Convert PHP \DOMDocument or \DOMNode object to simple array
759+
760+
Xml::escape(string $string): string; // Escape string before save it as xml content
761+
762+
```
763+
764+
749765
## Links (ideas and some functions)
750766
* utilphp - https://github.com/brandonwamboldt/utilphp
751767
* PHPBinString - https://github.com/Grandt/PHPBinString

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"ext-posix" : "*",
3131
"ext-mbstring" : "*",
3232
"ext-gd" : "*",
33-
"ext-filter" : "*"
33+
"ext-filter" : "*",
34+
"ext-dom" : "*"
3435
},
3536

3637
"require-dev" : {

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
stopOnRisky="false"
2626
>
2727
<testsuites>
28-
<testsuite name="All">
28+
<testsuite name="PHPUnit">
2929
<directory suffix="Test.php">tests</directory>
3030
</testsuite>
3131
</testsuites>

src/Filter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,14 @@ public static function html(string $string): string
387387
}
388388

389389
/**
390-
* Alias of "Str::escXml($string)"
390+
* Alias of "Xml::escape($string)"
391391
*
392392
* @param string $string
393393
* @return string
394394
*/
395395
public static function xml(string $string): string
396396
{
397-
return Str::escXml($string);
397+
return Xml::escape($string);
398398
}
399399

400400
/**

src/Str.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -619,26 +619,16 @@ public static function trim(string $value, bool $extendMode = false): string
619619
}
620620

621621
/**
622-
* Escape string before save it as xml content
622+
* Escape string before save it as xml content.
623+
* The function is moved. Please, use \JBZoo\Utils\Xml::escape($string). It'll be deprecated soon.
623624
*
624625
* @param string $string
625626
* @return string
627+
* @deprecated
626628
*/
627629
public static function escXml(string $string): string
628630
{
629-
$string = (string)preg_replace(
630-
'/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u',
631-
' ',
632-
$string
633-
);
634-
635-
$string = str_replace(
636-
['&', '<', '>', '"', "'"],
637-
['&amp;', '&lt;', '&gt;', '&quot;', '&apos;'],
638-
$string
639-
);
640-
641-
return $string;
631+
return Xml::escape($string);
642632
}
643633

644634
/**

src/Xml.php

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
3+
/**
4+
* JBZoo Toolbox - Utils
5+
*
6+
* This file is part of the JBZoo Toolbox project.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @package Utils
11+
* @license MIT
12+
* @copyright Copyright (C) JBZoo.com, All rights reserved.
13+
* @link https://github.com/JBZoo/Utils
14+
* @author Denis Smetannikov <denis@jbzoo.com>
15+
*/
16+
17+
namespace JBZoo\Utils;
18+
19+
/**
20+
* Class Xml
21+
* @package JBZoo\Utils
22+
*/
23+
class Xml
24+
{
25+
public const VERSION = '1.0';
26+
public const ENCODING = 'UTF-8';
27+
28+
/**
29+
* Escape string before save it as xml content
30+
*
31+
* @param string $string
32+
* @return string
33+
*/
34+
public static function escape(string $string): string
35+
{
36+
$string = (string)preg_replace(
37+
'/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u',
38+
' ',
39+
$string
40+
);
41+
42+
$string = str_replace(
43+
['&', '<', '>', '"', "'"],
44+
['&amp;', '&lt;', '&gt;', '&quot;', '&apos;'],
45+
$string
46+
);
47+
48+
return $string;
49+
}
50+
51+
/**
52+
* Create DOMDocument object from XML-string
53+
*
54+
* @param string|null $source
55+
* @return \DOMDocument
56+
*/
57+
public static function createFromString(?string $source = null): \DOMDocument
58+
{
59+
$document = new \DOMDocument();
60+
$document->preserveWhiteSpace = false;
61+
62+
if ($source) {
63+
$document->loadXML($source);
64+
}
65+
66+
$document->version = self::VERSION;
67+
$document->encoding = self::ENCODING;
68+
$document->formatOutput = true;
69+
70+
return $document;
71+
}
72+
73+
/**
74+
* Convert array to PHP DOMDocument object
75+
*
76+
* @param array $xmlAsArray
77+
* @param \DOMElement|null $domElement
78+
* @param \DOMDocument|null $document
79+
* @return \DOMDocument
80+
* @SuppressWarnings(PHPMD.NPathComplexity)
81+
*/
82+
public static function array2Dom(
83+
array $xmlAsArray,
84+
?\DOMElement $domElement = null,
85+
?\DOMDocument $document = null
86+
): \DOMDocument {
87+
if (null === $document) {
88+
$document = self::createFromString();
89+
}
90+
91+
$domElement = $domElement ?? $document;
92+
93+
if (array_key_exists('_text', $xmlAsArray) && $xmlAsArray['_text'] !== null) {
94+
$domElement->appendChild($document->createTextNode($xmlAsArray['_text']));
95+
}
96+
97+
if (array_key_exists('_cdata', $xmlAsArray) && $xmlAsArray['_cdata'] !== null) {
98+
$domElement->appendChild($document->createCDATASection($xmlAsArray['_cdata']));
99+
}
100+
101+
if ($domElement instanceof \DOMElement && array_key_exists('_attrs', $xmlAsArray)) {
102+
foreach ($xmlAsArray['_attrs'] as $name => $value) {
103+
$domElement->setAttribute($name, $value);
104+
}
105+
}
106+
107+
if (array_key_exists('_children', $xmlAsArray)) {
108+
foreach ($xmlAsArray['_children'] as $mixedElement) {
109+
if (
110+
array_key_exists('_node', $mixedElement) &&
111+
'#comment' !== $mixedElement['_node'] &&
112+
'#document' !== $mixedElement['_node']
113+
) {
114+
$node = $document->createElement($mixedElement['_node']);
115+
$domElement->appendChild($node);
116+
117+
/** @phan-suppress-next-line PhanPossiblyFalseTypeArgument */
118+
self::array2Dom($mixedElement, $node, $document);
119+
}
120+
}
121+
}
122+
123+
return $document;
124+
}
125+
126+
/**
127+
* Convert PHP \DOMDocument or \DOMNode object to simple array
128+
*
129+
* @param \DOMNode|\DOMElement|\DOMDocument $element
130+
* @return array
131+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
132+
*/
133+
public static function dom2Array(\DOMNode $element): array
134+
{
135+
$result = [
136+
'_node' => $element->nodeName,
137+
'_text' => null,
138+
'_cdata' => null,
139+
'_attrs' => [],
140+
'_children' => [],
141+
];
142+
143+
if ($element->attributes && $element->hasAttributes()) {
144+
foreach ($element->attributes as $attr) {
145+
$result['_attrs'][$attr->name] = $attr->value;
146+
}
147+
}
148+
149+
if ($element->hasChildNodes()) {
150+
$children = $element->childNodes;
151+
152+
if ($children->length === 1 && $child = $children->item(0)) {
153+
if ($child->nodeType === XML_TEXT_NODE) {
154+
$result['_text'] = $child->nodeValue;
155+
return $result;
156+
}
157+
158+
if ($child->nodeType === XML_CDATA_SECTION_NODE) {
159+
$result['_cdata'] = $child->nodeValue;
160+
return $result;
161+
}
162+
}
163+
164+
foreach ($children as $child) {
165+
if ($child->nodeType !== XML_COMMENT_NODE) {
166+
$result['_children'][] = self::dom2Array($child);
167+
}
168+
}
169+
}
170+
171+
return $result;
172+
}
173+
}

tests/ReadmeTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use JBZoo\Utils\Timer;
3636
use JBZoo\Utils\Url;
3737
use JBZoo\Utils\Vars;
38+
use JBZoo\Utils\Xml;
3839
use ReflectionClass;
3940

4041
/**
@@ -46,7 +47,9 @@ class ReadmeTest extends PHPUnit
4647
{
4748
public function testDocs()
4849
{
49-
skip("Disabled test. It's only for local using");
50+
if (Env::isExists('TRAVIS')) {
51+
skip("Disabled test. It's only for local using");
52+
}
5053

5154
$classes = [
5255
Arr::class,
@@ -69,6 +72,7 @@ public function testDocs()
6972
Url::class,
7073
Vars::class,
7174
PhpDocs::class,
75+
Xml::class,
7276
];
7377

7478
sort($classes, SORT_NATURAL);

tests/UtilsCodestyleTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
namespace JBZoo\PHPUnit;
1717

1818
/**
19-
* Class UtilsCodestyleTest
19+
* Class UtilsCopyrightsTest
2020
* @package JBZoo\PHPUnit
2121
*/
22-
class UtilsCodestyleTest extends AbstractCopyrightTest
22+
class UtilsCopyrightsTest extends AbstractCopyrightTest
2323
{
2424
protected $packageName = "Utils";
2525
}

tests/UtilsCopyrightsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* Class UtilsCopyrightsTest
2222
* @package JBZoo\PHPUnit
2323
*/
24-
class UtilsCopyrightsTest extends AbstractCodestyleTest
24+
class UtilsCodestyleTest extends AbstractCodestyleTest
2525
{
2626
public function testCyrillic(): void
2727
{

0 commit comments

Comments
 (0)