Passed
Push — develop ( 4b5082...21cba9 )
by nguereza
02:17
created

Xml::parseToArray()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 18
rs 9.6111
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   http://www.iacademy.cf
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
    /**
57
     * Transform an XML string to array
58
     * @param string $xml
59
     * @return array<string, mixed>
60
     */
61
    public static function decode(string $xml): array
62
    {
63
        return self::xmlToArray($xml);
64
    }
65
66
    /**
67
     * Transform an array to XML
68
     * @param array<int|string, mixed|iterable> $data
69
     * @return string
70
     */
71
    public static function encode($data): string
72
    {
73
        $xml = '<xml>';
74
        $xml .= self::arrayToXml($data);
75
        $xml .= '</xml>';
76
77
        return $xml;
78
    }
79
80
    /**
81
     * Transform an XML string to array
82
     * @param string $xml
83
     * @return array<string, mixed>
84
     */
85
    public static function xmlToArray(string $xml): array
86
    {
87
        $string = simplexml_load_string(
88
            $xml,
89
            'SimpleXMLElement',
90
            LIBXML_NOCDATA | LIBXML_NOBLANKS
91
        );
92
93
        $jsonString = Json::encode($string);
94
95
        /** @var array<mixed> $data */
96
        $data = Json::decode($jsonString, true);
97
98
        return $data;
99
    }
100
101
    /**
102
     * Transform an array to XML
103
     * @param array<int|string, mixed|iterable> $data
104
     * @return string
105
     */
106
    public static function arrayToXml($data): string
107
    {
108
        $xml = '';
109
        if (!empty($data)) {
110
            foreach ($data as $key => $value) {
111
                $xml .= '<' . $key . '>';
112
                if (is_iterable($value)) {
113
                    /** @var array<string, mixed|iterable> $value */
114
                    $xml .= self::arrayToXml($value);
115
                } elseif (is_numeric($value)) {
116
                    $xml .= $value;
117
                } else {
118
                    $xml .= self::addCharacterData($value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type iterable; however, parameter $value of Platine\Stdlib\Helper\Xml::addCharacterData() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
                    $xml .= self::addCharacterData(/** @scrutinizer ignore-type */ $value);
Loading history...
119
                }
120
                $xml .= '</' . $key . '>';
121
            }
122
        }
123
124
        return $xml;
125
    }
126
127
    /**
128
     * Add CDATA to the given string
129
     * @param string $value
130
     * @return string
131
     */
132
    protected static function addCharacterData(string $value): string
133
    {
134
        return sprintf('<![CDATA[%s]]>', $value);
135
    }
136
}
137