JsonSimpleXMLElementDecorator::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
c 2
b 0
f 0
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 10
cc 4
nc 8
nop 4
crap 4
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
    ];
32
33
    /**
34
     * Constructor.
35
     */
36 2
    public function __construct(
37
        SimpleXMLElement $element,
38
        bool $useAttributes = true,
39
        bool $useText = true,
40
        int $depth = self::DEF_DEPTH
41
    ) {
42 2
        $this->subject = $element;
43
44 2
        if (null !== $useAttributes) {
0 ignored issues
show
introduced by
The condition null !== $useAttributes is always true.
Loading history...
45 2
            $this->useAttributes($useAttributes);
46
        }
47 2
        if (null !== $useText) {
0 ignored issues
show
introduced by
The condition null !== $useText is always true.
Loading history...
48 2
            $this->useText($useText);
49
        }
50 2
        if (null !== $depth) {
0 ignored issues
show
introduced by
The condition null !== $depth is always true.
Loading history...
51 2
            $this->setDepth($depth);
52
        }
53
    }
54
55
    /**
56
     * Should use attributes.
57
     *
58
     * @param bool $bool
59
     */
60 2
    public function useAttributes($bool): void
61
    {
62 2
        $this->options['@attributes'] = (bool) $bool;
63
    }
64
65
    /**
66
     * Should use text.
67
     *
68
     * @param bool $bool
69
     */
70 2
    public function useText($bool): void
71
    {
72 2
        $this->options['@text'] = (bool) $bool;
73
    }
74
75
    /**
76
     * Set depth.
77
     *
78
     * @param int $depth
79
     */
80 2
    public function setDepth($depth): void
81
    {
82 2
        $this->options['depth'] = (int) max(0, $depth);
83
    }
84
85
    /**
86
     * Specify data which should be serialized to JSON.
87
     *
88
     * @return mixed Data which can be serialized by json_encode
89
     */
90 2
    #[\ReturnTypeWillChange]
91
    public function jsonSerialize()
92
    {
93 2
        $subject = $this->subject;
94
95 2
        $array = [];
96
97
        // json encode attributes if any.
98 2
        if ($this->options['@attributes']) {
99 2
            if ($attributes = $subject->attributes()) {
100
                $array['@attributes'] = array_map('strval', iterator_to_array($attributes));
101
            }
102
        }
103
104
        // traverse into children if applicable
105 2
        $children = $subject;
106 2
        $this->options = (array) $this->options;
107 2
        $depth = $this->options['depth'] - 1;
108 2
        if ($depth <= 0) {
109
            $children = [];
110
        }
111
112
        // json encode child elements if any. group on duplicate names as an array.
113 2
        foreach ($children as $name => $element) {
114
            /** @var SimpleXMLElement $element */
115 2
            $decorator = new self($element);
116 2
            $decorator->options = ['depth' => $depth] + $this->options;
117
118 2
            if (isset($array[$name])) {
119
                if (! \is_array($array[$name])) {
120
                    $array[$name] = [$array[$name]];
121
                }
122
                $array[$name][] = $decorator;
123
            } else {
124 2
                $array[$name] = $decorator;
125
            }
126
        }
127
128
        // json encode non-whitespace element simplexml text values.
129 2
        $text = trim($subject);
130 2
        if (\strlen($text)) {
131 2
            if ($array) {
132
                $this->options['@text'] && $array['@text'] = $text;
133
            } else {
134 2
                $array = $text;
135
            }
136
        }
137
138
        // return empty elements as NULL (self-closing or empty tags)
139 2
        if (! $array && '0' !== $array) {
140
            $array = null;
141
        }
142
143 2
        return $array;
144
    }
145
}
146