multidimension-al /
xml-array
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * __ ___ ____ _ ___ _ __ |
||
| 4 | * / |/ /_ __/ / /_(_)___/ (_)___ ___ ___ ____ _____(_)___ ____ ____ _/ / |
||
| 5 | * / /|_/ / / / / / __/ / __ / / __ `__ \/ _ \/ __ \/ ___/ / __ \/ __ \ / __ `/ / |
||
| 6 | * / / / / /_/ / / /_/ / /_/ / / / / / / / __/ / / (__ ) / /_/ / / / // /_/ / / |
||
| 7 | * /_/ /_/\__,_/_/\__/_/\__,_/_/_/ /_/ /_/\___/_/ /_/____/_/\____/_/ /_(_)__,_/_/ |
||
| 8 | * |
||
| 9 | * XML to Array Library |
||
| 10 | * Copyright (c) Multidimension.al (http://multidimension.al) |
||
| 11 | * Github : https://github.com/multidimension-al/xml-array |
||
| 12 | * |
||
| 13 | * Licensed under The MIT License |
||
| 14 | * For full copyright and license information, please see the LICENSE file |
||
| 15 | * Redistributions of files must retain the above copyright notice. |
||
| 16 | * |
||
| 17 | * @copyright Copyright © 2017-2019 Multidimension.al (http://multidimension.al) |
||
| 18 | * @link https://github.com/multidimension-al/xml-array Github |
||
| 19 | * @license http://www.opensource.org/licenses/mit-license.php MIT License |
||
| 20 | */ |
||
| 21 | |||
| 22 | namespace Multidimensional\XmlArray; |
||
| 23 | |||
| 24 | class XMLArray |
||
| 25 | { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * This function will generate a PHP array from a string containing well formed XML code. If the XML string cannot |
||
| 29 | * be loaded, it will return null. This function has an optional parameter to return @attributes reformatted, which |
||
| 30 | * is enabled by default. |
||
| 31 | * |
||
| 32 | * @param string|null $string |
||
| 33 | * @param bool $convertAttributes |
||
| 34 | * @return array|null |
||
| 35 | */ |
||
| 36 | 16 | public static function generateArray($string, $convertAttributes = true) |
|
| 37 | { |
||
| 38 | 16 | $xml = simplexml_load_string($string); |
|
| 39 | 16 | if ($xml !== false) { |
|
| 40 | 12 | $json = json_encode([$xml->getName() => $xml]); |
|
| 41 | 12 | $array = json_decode($json, true); |
|
| 42 | 12 | if ($convertAttributes === true) { |
|
| 43 | 10 | $array = self::convertAttributes($array); |
|
| 44 | 5 | } |
|
| 45 | 12 | return $array; |
|
| 46 | } else { |
||
| 47 | 4 | return null; |
|
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * This private function will convert attributes from the standard @attributes array to an inline set of fields |
||
| 53 | * with the @ notation in front of each variable key. This function works recursively to find all @attributes in |
||
| 54 | * the entire supplied array. |
||
| 55 | * |
||
| 56 | * @param array $array |
||
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | 10 | private static function convertAttributes($array) |
|
| 60 | { |
||
| 61 | 10 | if (is_array($array)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 62 | 10 | foreach ($array as $key => $value) { |
|
| 63 | 10 | if (isset($value['@attributes'])) { |
|
| 64 | 6 | foreach ($value['@attributes'] as $key2 => $value2) { |
|
| 65 | 6 | $array[$key]['@'.$key2] = $value2; |
|
| 66 | 3 | } |
|
| 67 | 6 | unset($array[$key]['@attributes']); |
|
| 68 | 3 | } |
|
| 69 | |||
| 70 | 10 | if (is_array($array[$key])) { |
|
| 71 | 10 | $array[$key] = self::convertAttributes($array[$key]); |
|
| 72 | 5 | } |
|
| 73 | 5 | } |
|
| 74 | 5 | } |
|
| 75 | |||
| 76 | 10 | return $array; |
|
| 77 | } |
||
| 78 | } |
||
| 79 |