Completed
Pull Request — master (#111)
by Filip
07:42
created

Xml   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 31
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A parse() 0 22 2
1
<?php
2
3
namespace Noodlehaus\StringParser;
4
5
use Noodlehaus\Exception\ParseException;
6
7
/**
8
 * XML string 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 StringParserInterface
17
{
18
    /**
19
     * {@inheritDoc}
20
     * Parses an XML string as an array
21
     *
22
     * @throws ParseException If there is an error parsing the XML string
23
     */
24 6
    public function parse($configuration)
25
    {
26 6
        libxml_use_internal_errors(true);
27
28 6
        $data = simplexml_load_string($configuration, null, LIBXML_NOERROR);
29
30 6
        if ($data === false) {
31 3
            $errors      = libxml_get_errors();
32 3
            $latestError = array_pop($errors);
33
            $error       = [
34 3
                'message' => $latestError->message,
35 3
                'type'    => $latestError->level,
36 3
                'code'    => $latestError->code,
37 3
                'line'    => $latestError->line,
38 1
            ];
39 3
            throw new ParseException($error);
40
        }
41
42 3
        $data = json_decode(json_encode($data), true);
43
44 3
        return $data;
45
    }
46
}
47