XML.php 2.71 KB
Newer Older
1
<?php
Tino Goratsch's avatar
Tino Goratsch committed
2

Tino Goratsch's avatar
Tino Goratsch committed
3
/**
4
 * Copyright (c) by the ACP3 Developers.
Tino Goratsch's avatar
Tino Goratsch committed
5
 * See the LICENSE file at the top-level module directory for licensing details.
Tino Goratsch's avatar
Tino Goratsch committed
6 7
 */

8
namespace ACP3\Core;
Tino Goratsch's avatar
Tino Goratsch committed
9

10
use ACP3\Core\Application\Bootstrap;
11 12

/**
13
 * Parses XML files.
14 15 16 17
 */
class XML
{
    /**
18
     * Cache for already parsed XML files.
19 20 21 22 23 24
     *
     * @var array
     */
    protected $info = [];

    /**
25
     * Parses the given XML file and returns it as an array.
26 27 28 29 30 31 32 33 34 35
     *
     * @param string $path
     * @param string $xpath
     *
     * @return array
     */
    public function parseXmlFile($path, $xpath)
    {
        if (!empty($this->info[$path][$xpath])) {
            return $this->info[$path][$xpath];
Tino Goratsch's avatar
Tino Goratsch committed
36
        } elseif (\is_file($path) === true) {
37
            /** @var \SimpleXMLElement $xml */
Tino Goratsch's avatar
Tino Goratsch committed
38
            $xml = \simplexml_load_file($path);
39 40 41 42 43
            $data = $xml->xpath($xpath);

            if (!empty($data)) {
                foreach ($data as $row) {
                    foreach ($row as $key => $value) {
44
                        /** @var \SimpleXMLElement $value */
45
                        if ($value->attributes()) {
46
                            $this->parseAttributes($value->attributes(), $path, $xpath, $key);
47 48 49 50 51 52 53
                        } elseif (isset($this->info[$path][$xpath][(string) $key]) && \is_array($this->info[$path][$xpath][(string) $key])) {
                            $this->info[$path][$xpath][(string) $key][] = (string) $value;
                        } elseif (isset($this->info[$path][$xpath][(string) $key])) {
                            $tmp = $this->info[$path][$xpath][(string) $key];
                            $this->info[$path][$xpath][(string) $key] = [];
                            $this->info[$path][$xpath][(string) $key][] = $tmp;
                            $this->info[$path][$xpath][(string) $key][] = (string) $value;
54
                        } else {
55
                            $this->info[$path][$xpath][(string) $key] = (string) $value;
56 57 58
                        }
                    }
                }
Tino Goratsch's avatar
Tino Goratsch committed
59

60 61 62
                return $this->info[$path][$xpath];
            }
        }
Tino Goratsch's avatar
Tino Goratsch committed
63

64 65
        return [];
    }
66 67 68 69 70 71 72 73 74 75

    /**
     * @param \SimpleXMLElement $attributes
     * @param string            $path
     * @param string            $xpath
     * @param string            $key
     */
    protected function parseAttributes(\SimpleXMLElement $attributes, $path, $xpath, $key)
    {
        foreach ($attributes as $attrKey => $attrValue) {
76
            if ($key === 'version' && $attrKey === 'core' && (string) $attrValue === 'true') {
77 78
                $this->info[$path][$xpath]['version'] = Bootstrap::VERSION;
            } else {
79
                $this->info[$path][$xpath][(string) $key][(string) $attrKey] = (string) $attrValue;
80 81 82
            }
        }
    }
83
}