|
| 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 | + ['&', '<', '>', '"', '''], |
| 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 | +} |
0 commit comments