Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
JsonParser
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 parse
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
2
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework Config Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\Config\Parsers;
11
12/**
13 * Class JsonParser.
14 *
15 * @package config
16 */
17class JsonParser extends Parser
18{
19    /**
20     * Parses a JSON file.
21     *
22     * @param mixed $config path to the JSON file
23     *
24     * @throws ParserException
25     *
26     * @return array<mixed> The JSON parsed data
27     */
28    public static function parse(mixed $config) : array
29    {
30        static::checkConfig($config);
31        return static::parseOrThrow(static function () use ($config) : array {
32            $config = \file_get_contents($config);
33            try {
34                $data = \json_decode($config, true, 512, \JSON_THROW_ON_ERROR); // @phpstan-ignore-line
35            } catch (\Exception $exception) {
36                throw new ParserException(
37                    static::class . ': ' . $exception->getMessage(),
38                    $exception->getCode(),
39                    \E_ERROR,
40                    $exception->getFile(),
41                    $exception->getLine()
42                );
43            }
44            return static::ksortRecursive($data);
45        });
46    }
47}