Completed
Push — develop ( 3fad72...7847b0 )
by Davide
03:35
created

Xml::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2.0373

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
ccs 15
cts 19
cp 0.7895
rs 9.0856
cc 2
eloc 15
nc 2
nop 1
crap 2.0373
1
<?php
2
3
namespace Noodlehaus\FileParser;
4
5
use Noodlehaus\Exception\ParseException;
6
7
/**
8
 * XML file parser
9
 *
10
 * @package    Config
11
 * @author     Jesus A. Domingo <[email protected]>
12
 * @author     Hassan Khan <[email protected]>
13
 * @link       https://github.com/noodlehaus/config
14
 * @license    MIT
15
 */
16
class Xml implements FileParserInterface
17
{
18
    /**
19
     * {@inheritDoc}
20
     * Parses an XML file as an array
21
     *
22
     * @throws ParseException If there is an error parsing the XML file
23
     */
24 4
    public function parse($path)
25
    {
26 4
        libxml_use_internal_errors(true);
27
28 4
        $data = simplexml_load_file($path, null, LIBXML_NOERROR);
29
30 4
        if ($data === false) {
31 2
            $errors      = libxml_get_errors();
32 2
            $latestError = array_pop($errors);
33
            $error       = array(
34 2
                'message' => $latestError->message,
35 2
                'type'    => $latestError->level,
36 2
                'code'    => $latestError->code,
37 2
                'file'    => $latestError->file,
38 2
                'line'    => $latestError->line,
39 2
            );
40 2
            throw new ParseException($error);
41
        }
42
43 2
        $data = json_decode(json_encode($data), true);
44
45 2
        return $data;
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51 2
    public static function getSupportedExtensions()
52
    {
53 2
        return array('xml');
54
    }
55
}
56