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($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($data): string |
106
|
|
|
{ |
107
|
|
|
$xml = ''; |
108
|
|
|
if (!empty($data)) { |
109
|
|
|
foreach ($data as $key => $value) { |
110
|
|
|
if (is_int($key) === false) { |
111
|
|
|
$xml .= '<' . $key . '>'; |
112
|
|
|
} |
113
|
|
|
if (is_iterable($value)) { |
114
|
|
|
/** @var array<string, mixed|iterable> $value */ |
115
|
|
|
$xml .= self::arrayToXml($value); |
116
|
|
|
} elseif (is_numeric($value)) { |
117
|
|
|
$xml .= $value; |
118
|
|
|
} else { |
119
|
|
|
$xml .= self::addCharacterData($value); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
if (is_int($key) === false) { |
122
|
|
|
$xml .= '</' . $key . '>'; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $xml; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Add CDATA to the given string |
132
|
|
|
* @param string $value |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
protected static function addCharacterData(string $value): string |
136
|
|
|
{ |
137
|
|
|
return sprintf('<![CDATA[%s]]>', $value); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|