Completed
Push — master ( d621a8...b2ef5f )
by Hong
02:39
created

JsonReader::getError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Shared
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Shared\Reader;
16
17
/**
18
 * Read & parse json formatted file and return the result
19
 *
20
 * @package Phossa2\Shared
21
 * @author  Hong Zhang <[email protected]>
22
 * @version 2.0.1
23
 * @since   2.0.1 added
24
 */
25
class JsonReader extends ReaderAbstract
26
{
27
    /**
28
     * default json error
29
     *
30
     * @var    array
31
     * @access protected
32
     * @staticvar
33
     */
34
    protected static $error = [
35
        \JSON_ERROR_NONE => 'No error',
36
        \JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
37
        \JSON_ERROR_STATE_MISMATCH => 'State mismatch (invalid or malformed JSON)',
38
        \JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
39
        \JSON_ERROR_SYNTAX => 'Syntax error',
40
        \JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
41
        \JSON_ERROR_RECURSION => 'Recursion found',
42
        \JSON_ERROR_INF_OR_NAN => 'NaN found',
43
        \JSON_ERROR_UNSUPPORTED_TYPE => 'Unsupported type found',
44
    ];
45
46
    /**
47
     * {@inheritDoc}
48
     */
49
    protected static function readFromFile($path)
50
    {
51
        return @json_decode(file_get_contents($path), true);
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    protected static function getError(/*# string */ $path)/*#: string */
58
    {
59
        $error = json_last_error();
60
        return isset(static::$error[$error]) ?
61
            static::$error[$error] :
62
            'JSON parse error';
63
    }
64
}
65