1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bmatovu\LaravelXml\Support; |
4
|
|
|
|
5
|
|
|
use JsonSerializable; |
6
|
|
|
use SimpleXMLElement; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class JsonSimpleXMLElementDecorator. |
10
|
|
|
* |
11
|
|
|
* @see https://hakre.wordpress.com/2013/07/09/simplexml-and-json-encode-in-php-part-i |
12
|
|
|
* @see https://gist.github.com/hakre/1d9e555ac1ebb1fc4ea8 |
13
|
|
|
* @see https://gist.github.com/hakre/c36a66708259f54e564a |
14
|
|
|
*/ |
15
|
|
|
class JsonSimpleXMLElementDecorator implements JsonSerializable |
16
|
|
|
{ |
17
|
|
|
const DEF_DEPTH = 512; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \SimpleXMLElement |
21
|
|
|
*/ |
22
|
|
|
protected $subject; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $options = [ |
28
|
|
|
'@attributes' => true, |
29
|
|
|
'@text' => true, |
30
|
|
|
'depth' => self::DEF_DEPTH, |
31
|
|
|
'@allowWhiteSpace' => false, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor. |
36
|
2 |
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
SimpleXMLElement $element, |
39
|
|
|
bool $useAttributes = true, |
40
|
|
|
bool $useText = true, |
41
|
|
|
int $depth = self::DEF_DEPTH, |
42
|
2 |
|
bool $allowWhiteSpace = false |
43
|
|
|
) { |
44
|
2 |
|
$this->subject = $element; |
45
|
2 |
|
|
46
|
|
|
if (null !== $useAttributes) { |
|
|
|
|
47
|
2 |
|
$this->useAttributes($useAttributes); |
48
|
2 |
|
} |
49
|
|
|
if (null !== $useText) { |
|
|
|
|
50
|
2 |
|
$this->useText($useText); |
51
|
2 |
|
} |
52
|
|
|
if (null !== $depth) { |
|
|
|
|
53
|
|
|
$this->setDepth($depth); |
54
|
|
|
} |
55
|
|
|
if (null !== $allowWhiteSpace) { |
|
|
|
|
56
|
|
|
$this->allowWhiteSpace($allowWhiteSpace); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
/** |
61
|
|
|
* Should use attributes. |
62
|
2 |
|
* |
63
|
|
|
* @param bool $bool |
64
|
|
|
*/ |
65
|
|
|
public function useAttributes($bool): void |
66
|
|
|
{ |
67
|
|
|
$this->options['@attributes'] = (bool) $bool; |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
/** |
71
|
|
|
* Should use text. |
72
|
2 |
|
* |
73
|
|
|
* @param bool $bool |
74
|
|
|
*/ |
75
|
|
|
public function useText($bool): void |
76
|
|
|
{ |
77
|
|
|
$this->options['@text'] = (bool) $bool; |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
/** |
81
|
|
|
* Set depth. |
82
|
2 |
|
* |
83
|
|
|
* @param int $depth |
84
|
|
|
*/ |
85
|
|
|
public function setDepth($depth): void |
86
|
|
|
{ |
87
|
|
|
$this->options['depth'] = (int) max(0, $depth); |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
/** |
91
|
|
|
* Allow white space. |
92
|
|
|
* |
93
|
2 |
|
* @param bool $bool |
94
|
|
|
*/ |
95
|
2 |
|
public function allowWhiteSpace($bool): void |
96
|
|
|
{ |
97
|
|
|
$this->options['@allowWhiteSpace'] = (bool) $bool; |
98
|
2 |
|
} |
99
|
2 |
|
|
100
|
|
|
/** |
101
|
|
|
* Specify data which should be serialized to JSON. |
102
|
|
|
* |
103
|
|
|
* @return mixed Data which can be serialized by json_encode |
104
|
|
|
*/ |
105
|
2 |
|
#[\ReturnTypeWillChange] |
106
|
2 |
|
public function jsonSerialize() |
107
|
2 |
|
{ |
108
|
2 |
|
$subject = $this->subject; |
109
|
|
|
|
110
|
|
|
$array = []; |
111
|
|
|
|
112
|
|
|
// json encode attributes if any. |
113
|
2 |
|
if ($this->options['@attributes']) { |
114
|
|
|
if ($attributes = $subject->attributes()) { |
115
|
2 |
|
$array['@attributes'] = array_map('strval', iterator_to_array($attributes)); |
116
|
2 |
|
} |
117
|
|
|
} |
118
|
2 |
|
|
119
|
|
|
// traverse into children if applicable |
120
|
|
|
$children = $subject; |
121
|
|
|
$this->options = (array) $this->options; |
122
|
|
|
$depth = $this->options['depth'] - 1; |
123
|
|
|
if ($depth <= 0) { |
124
|
2 |
|
$children = []; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// json encode child elements if any. group on duplicate names as an array. |
128
|
|
|
foreach ($children as $name => $element) { |
129
|
2 |
|
/** @var SimpleXMLElement $element */ |
130
|
2 |
|
$decorator = new self($element); |
131
|
2 |
|
$decorator->options = ['depth' => $depth] + $this->options; |
132
|
|
|
|
133
|
|
|
if (isset($array[$name])) { |
134
|
2 |
|
if (! \is_array($array[$name])) { |
135
|
|
|
$array[$name] = [$array[$name]]; |
136
|
|
|
} |
137
|
|
|
$array[$name][] = $decorator; |
138
|
|
|
} else { |
139
|
2 |
|
$array[$name] = $decorator; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
2 |
|
// json encode element simplexml text values depends on allow whitespace config. |
144
|
|
|
$text = $this->options['@allowWhiteSpace'] ? $subject : trim($subject); |
145
|
|
|
if (\strlen($text)) { |
146
|
|
|
if ($array) { |
147
|
|
|
$this->options['@text'] && $array['@text'] = $text; |
148
|
|
|
} else { |
149
|
|
|
$array = $text; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// return empty elements as NULL (self-closing or empty tags) |
154
|
|
|
if (! $array && '0' !== $array) { |
155
|
|
|
$array = null; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $array; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|