JsonDecodeFile::call()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Schnittstabil\JsonDecodeFile;
4
5
use KHerGe\File\File;
6
use KHerGe\File\Exception\ResourceException;
7
use Seld\JsonLint\JsonParser;
8
use Seld\JsonLint\ParsingException;
9
use duncan3dc\Bom\Util as BomUtil;
10
11
/**
12
 * Read and decode JSON files.
13
 */
14
class JsonDecodeFile
15
{
16
    /**
17
     * Reads entire file into a string.
18
     *
19
     * @param string $path the file path
20
     *
21
     * @return string the read data
22
     *
23
     * @throws ResourceException
24
     */
25
    protected static function readFile($path)
26
    {
27
        // Workaround for https://github.com/kherge-php/file-manager/pull/2
28
        $error = null;
29
        set_error_handler(function ($severity, $message, $filename, $lineno) use (&$error) {
30
            $error = new \ErrorException($message, 0, $severity, $filename, $lineno);
31
        }, E_WARNING);
32
33
        try {
34
            $file = new File($path, 'r');
35
        } finally {
36
            restore_error_handler();
37
        }
38
39
        if ($error) {
40
            // @codeCoverageIgnoreStart
41
            throw new ResourceException(
42
                "The file \"$path\" could not be opened.",
43
                $error
44
            );
45
            // @codeCoverageIgnoreEnd
46
        }
47
48
        return $file->read();
49
    }
50
51
    /**
52
     * Converts JSON encoded string into a PHP variable.
53
     *
54
     * @param string $json  JSON string
55
     * @param bool   $assoc returned objects will be converted into associative arrays
56
     *
57
     * @return mixed the value encoded in json in appropriate PHP type
58
     *
59
     * @throws ParsingException
60
     *
61
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
62
     */
63
    protected static function parse($json, $assoc = false)
64
    {
65
        $falgs = JsonParser::DETECT_KEY_CONFLICTS;
66
67
        if ($assoc) {
68
            $falgs |= JsonParser::PARSE_TO_ASSOC;
69
        }
70
71
        $parser = new JsonParser();
72
73
        return $parser->parse($json, $falgs);
74
    }
75
76
    /**
77
     * Read and decode a JSON file.
78
     *
79
     * @see http://php.net/manual/en/function.json-decode.php json_decode documentation
80
     *
81
     * @param string $path  the file path
82
     * @param bool   $assoc returned objects will be converted into associative arrays
83
     *
84
     * @return mixed the value encoded in json in appropriate PHP type
85
     *
86
     * @throws ResourceException
87
     * @throws ParsingException
88
     *
89
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
90
     * @SuppressWarnings(PHPMD.StaticAccess)
91
     */
92
    public static function call($path, $assoc = false)
93
    {
94
        return self::parse(BomUtil::removeBom(self::readFile($path)), $assoc);
95
    }
96
}
97