Completed
Push — develop ( 930556...77256d )
by Hassan
9s
created

Xml   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 40
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 23 2
A getSupportedExtensions() 0 4 1
1
<?php
2
3
namespace Noodlehaus\Parser;
4
5
use Noodlehaus\Exception\ParseException;
6
7
/**
8
 * XML parser
9
 *
10
 * @package    Config
11
 * @author     Jesus A. Domingo <[email protected]>
12
 * @author     Hassan Khan <[email protected]>
13
 * @author     Filip Š <[email protected]>
14
 * @link       https://github.com/noodlehaus/config
15
 * @license    MIT
16
 */
17
class Xml implements ParserInterface
18
{
19
    /**
20
     * {@inheritDoc}
21
     * Parses an XML string as an array
22
     *
23
     * @throws ParseException If there is an error parsing the XML string
24
     */
25 6
    public function parse($config, $filename = null)
26
    {
27 6
        libxml_use_internal_errors(true);
28
29 6
        $data = simplexml_load_string($config, null, LIBXML_NOERROR);
30
31 6
        if ($data === false) {
32 3
            $errors      = libxml_get_errors();
33 3
            $latestError = array_pop($errors);
34
            $error       = [
35 3
                'message' => $latestError->message,
36 3
                'type'    => $latestError->level,
37 3
                'code'    => $latestError->code,
38 3
                'file'    => $filename,
39 3
                'line'    => $latestError->line,
40 1
            ];
41 3
            throw new ParseException($error);
42
        }
43
44 3
        $data = json_decode(json_encode($data), true);
45
46 3
        return $data;
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52 3
    public static function getSupportedExtensions()
53
    {
54 3
        return ['xml'];
55
    }
56
}
57