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

Json   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 4
c 4
b 1
f 0
lcom 0
cbo 2
dl 0
loc 38
ccs 15
cts 15
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 20 3
A getSupportedFileExtensions() 0 4 1
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