Completed
Push — master ( 2b8886...d0ff07 )
by Xeriab
04:16
created

Json::parse()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.004

Importance

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