JsonReader::read()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 16
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * Json.php - Jaxon config reader
5
 *
6
 * Read the config data from a JSON formatted config file.
7
 *
8
 * @package jaxon-config
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2022 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
14
15
namespace Jaxon\Config\Reader;
16
17
use Jaxon\Config\Exception\FileAccess;
18
use Jaxon\Config\Exception\FileContent;
19
20
use function is_array;
21
use function realpath;
22
use function is_readable;
23
use function file_get_contents;
24
use function json_decode;
25
26
class JsonReader
27
{
28
    /**
29
     * Read options from a JSON formatted config file
30
     *
31
     * @param string $sConfigFile The full path to the config file
32
     *
33
     * @return array
34
     * @throws FileAccess
35
     * @throws FileContent
36
     */
37
    public static function read(string $sConfigFile): array
38
    {
39
        $sConfigFile = realpath($sConfigFile);
40
        if(!is_readable($sConfigFile))
41
        {
42
            throw new FileAccess($sConfigFile);
43
        }
44
45
        $sFileContent = file_get_contents($sConfigFile);
46
        $aConfigOptions = json_decode($sFileContent, true);
47
        if(!is_array($aConfigOptions))
48
        {
49
            throw new FileContent($sConfigFile);
50
        }
51
52
        return $aConfigOptions;
53
    }
54
}
55