Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
XmlParser
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 parse
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
3
 parseValue
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework Config Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\Config\Parsers;
11
12use Framework\Config\Parsers\Extra\JsonXMLElement;
13
14/**
15 * Class XmlParser.
16 *
17 * @package config
18 */
19class XmlParser extends Parser
20{
21    /**
22     * Parses an XML file.
23     *
24     * @param mixed $config path to the XML file
25     *
26     * @throws ParserException
27     *
28     * @return array<mixed> The XML parsed data
29     */
30    public static function parse(mixed $config) : array
31    {
32        static::checkConfig($config);
33        return static::parseOrThrow(static function () use ($config) : array {
34            $config = \file_get_contents($config);
35            $config = \simplexml_load_string($config, JsonXMLElement::class); // @phpstan-ignore-line
36            $config = \json_encode($config);
37            $config = \json_decode($config, true); // @phpstan-ignore-line
38            $data = [];
39            foreach ($config as $instance => $values) {
40                foreach ($values as &$value) {
41                    $value = static::parseValue($value);
42                }
43                unset($value);
44                $data[$instance] = $values;
45            }
46            return static::ksortRecursive($data);
47        });
48    }
49
50    /**
51     * @param array<int|string,mixed>|string $value
52     *
53     * @return array<int|string,mixed>|bool|float|int|string|null
54     */
55    protected static function parseValue(array|string $value) : array|bool|int|float|string|null
56    {
57        if (\is_array($value)) {
58            foreach ($value as &$val) {
59                $val = static::parseValue($val);
60            }
61            return $value;
62        }
63        return static::getValue($value);
64    }
65}