Completed
Push — master ( 76163c...0c83ff )
by Nashwan
03:43
created

Xml::parse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 21
rs 9.3142
cc 3
eloc 13
nc 3
nop 1
1
<?php
2
/**
3
 * Konfig
4
 *
5
 * Yet another simple configuration file loader library.
6
 *
7
 * @author  Xeriab Nabil (aka KodeBurner) <[email protected]>
8
 * @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT
9
 * @link    https://xeriab.github.io/projects/konfig
10
 */
11
namespace Exen\Konfig\FileParser;
12
13
use Exen\Konfig\Exception\ParseException;
14
15
class Xml extends AbstractFileParser
16
{
17
    /**
18
     * {@inheritDoc}
19
     * Loads a XML file as an array
20
     *
21
     * @throws ParseException If there is an error parsing XML file
22
     * @since 0.1
23
     */
24
    public function parse($path)
25
    {
26
        $data = simplexml_load_file($path, 'SimpleXMLElement', LIBXML_NOWARNING | LIBXML_NOERROR);
27
28
        if ($data === false) {
29
            $lastError = libxml_get_last_error();
30
            if ($lastError !== false) {
31
                throw new ParseException([
32
                    'message' => $lastError->message,
33
                    'type'    => $lastError->level,
34
                    'code'    => $lastError->code,
35
                    'file'    => $lastError->file,
36
                    'line'    => $lastError->line,
37
                ]);
38
            }
39
        }
40
41
        $data = json_decode(json_encode($data), true);
42
43
        return $data;
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49
    public function getSupportedFileExtensions()
50
    {
51
        return ['xml'];
52
    }
53
}
54
55
#: END OF ./src/FileParser/Xml.php FILE
56