Completed
Pull Request — master (#91)
by
unknown
04:12
created

Json::parse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 13
cts 13
cp 1
rs 9.4285
cc 3
eloc 12
nc 3
nop 1
crap 3
1
<?php
2
3
namespace mhndev\config\FileParser;
4
5
use mhndev\config\Exception\ParseException;
6
7
/**
8
 * JSON 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 Json implements FileParserInterface
17
{
18
    /**
19
     * {@inheritDoc}
20
     * Loads a JSON file as an array
21
     *
22
     * @throws ParseException If there is an error parsing the JSON file
23
     */
24 6
    public function parse($path)
25
    {
26 6
        $data = json_decode(file_get_contents($path), true);
27
28 6
        if (json_last_error() !== JSON_ERROR_NONE) {
29 2
            $error_message  = 'Syntax error';
30 2
            if (function_exists('json_last_error_msg')) {
31 4
                $error_message = json_last_error_msg();
32
            }
33
34 6
            $error = array(
35
                'message' => $error_message,
36 3
                'type'    => json_last_error(),
37 3
                'file'    => $path,
38 3
            );
39 3
            throw new ParseException($error);
40 3
        }
41
42
        return $data;
43 3
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    public static function getSupportedExtensions()
49 3
    {
50
        return array('json');
51 3
    }
52
}
53