platine-php /
stdlib
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Platine Stdlib |
||
| 5 | * |
||
| 6 | * Platine Stdlib is a the collection of frequently used php features |
||
| 7 | * |
||
| 8 | * This content is released under the MIT License (MIT) |
||
| 9 | * |
||
| 10 | * Copyright (c) 2020 Platine Stdlib |
||
| 11 | * |
||
| 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
||
| 13 | * of this software and associated documentation files (the "Software"), to deal |
||
| 14 | * in the Software without restriction, including without limitation the rights |
||
| 15 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||
| 16 | * copies of the Software, and to permit persons to whom the Software is |
||
| 17 | * furnished to do so, subject to the following conditions: |
||
| 18 | * |
||
| 19 | * The above copyright notice and this permission notice shall be included in all |
||
| 20 | * copies or substantial portions of the Software. |
||
| 21 | * |
||
| 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
| 23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
| 24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||
| 25 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
| 26 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||
| 27 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||
| 28 | * SOFTWARE. |
||
| 29 | */ |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @file Xml.php |
||
| 33 | * |
||
| 34 | * The XML helper class |
||
| 35 | * |
||
| 36 | * @package Platine\Stdlib\Helper |
||
| 37 | * @author Platine Developers Team |
||
| 38 | * @copyright Copyright (c) 2020 |
||
| 39 | * @license http://opensource.org/licenses/MIT MIT License |
||
| 40 | * @link https://www.platine-php.com |
||
| 41 | * @version 1.0.0 |
||
| 42 | * @filesource |
||
| 43 | */ |
||
| 44 | |||
| 45 | declare(strict_types=1); |
||
| 46 | |||
| 47 | namespace Platine\Stdlib\Helper; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @class Xml |
||
| 51 | * @package Platine\Stdlib\Helper |
||
| 52 | */ |
||
| 53 | class Xml |
||
| 54 | { |
||
| 55 | /** |
||
| 56 | * Transform an XML string to array |
||
| 57 | * @param string $xml |
||
| 58 | * @return array<string, mixed> |
||
| 59 | */ |
||
| 60 | public static function decode(string $xml): array |
||
| 61 | { |
||
| 62 | return self::xmlToArray($xml); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Transform an array to XML |
||
| 67 | * @param array<int|string, mixed|iterable> $data |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public static function encode(iterable $data): string |
||
| 71 | { |
||
| 72 | $xml = '<xml>'; |
||
| 73 | $xml .= self::arrayToXml($data); |
||
| 74 | $xml .= '</xml>'; |
||
| 75 | |||
| 76 | return $xml; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Transform an XML string to array |
||
| 81 | * @param string $xml |
||
| 82 | * @return array<string, mixed> |
||
| 83 | */ |
||
| 84 | public static function xmlToArray(string $xml): array |
||
| 85 | { |
||
| 86 | $string = simplexml_load_string( |
||
| 87 | $xml, |
||
| 88 | 'SimpleXMLElement', |
||
| 89 | LIBXML_NOCDATA | LIBXML_NOBLANKS |
||
| 90 | ); |
||
| 91 | |||
| 92 | $jsonString = Json::encode($string); |
||
| 93 | |||
| 94 | /** @var array<mixed> $data */ |
||
| 95 | $data = Json::decode($jsonString, true); |
||
| 96 | |||
| 97 | return $data; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Transform an array to XML |
||
| 102 | * @param array<int|string, mixed|iterable> $data |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public static function arrayToXml(iterable $data): string |
||
| 106 | { |
||
| 107 | $xml = ''; |
||
| 108 | if (is_array($data) === false) { |
||
| 109 | $data = iterator_to_array($data); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 110 | } |
||
| 111 | |||
| 112 | if (count($data) > 0) { |
||
| 113 | foreach ($data as $key => $value) { |
||
| 114 | if (is_int($key) === false) { |
||
| 115 | $xml .= '<' . $key . '>'; |
||
| 116 | } |
||
| 117 | if (is_iterable($value)) { |
||
| 118 | /** @var array<string, mixed|iterable> $value */ |
||
| 119 | $xml .= self::arrayToXml($value); |
||
| 120 | } elseif (is_numeric($value)) { |
||
| 121 | $xml .= $value; |
||
| 122 | } else { |
||
| 123 | $xml .= self::addCharacterData($value); |
||
| 124 | } |
||
| 125 | if (is_int($key) === false) { |
||
| 126 | $xml .= '</' . $key . '>'; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | return $xml; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Add CDATA to the given string |
||
| 136 | * @param string $value |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | protected static function addCharacterData(string $value): string |
||
| 140 | { |
||
| 141 | if (empty($value)) { |
||
| 142 | return $value; |
||
| 143 | } |
||
| 144 | |||
| 145 | return sprintf('<![CDATA[%s]]>', $value); |
||
| 146 | } |
||
| 147 | } |
||
| 148 |