Completed
Push — master ( 4d4575...2b8886 )
by Xeriab
02:40
created

Json::parse()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

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