You've already forked config
86 lines
2.2 KiB
PHP
86 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Siteworx\Config\Parser;
|
|
|
|
use Siteworx\Config\Exception\ParseException;
|
|
|
|
/**
|
|
* JSON parser
|
|
*
|
|
* @package Config
|
|
* @author Jesus A. Domingo <jesus.domingo@gmail.com>
|
|
* @author Hassan Khan <contact@hassankhan.me>
|
|
* @author Filip Š <projects@filips.si>
|
|
* @link https://github.com/noodlehaus/config
|
|
* @license MIT
|
|
*/
|
|
class Json implements ParserInterface
|
|
{
|
|
/**
|
|
* {@inheritDoc}
|
|
* Parses an JSON file as an array
|
|
*
|
|
* @throws ParseException If there is an error parsing the JSON file
|
|
*/
|
|
public function parseFile(string $filename): array
|
|
{
|
|
try {
|
|
$data = json_decode(file_get_contents($filename), true, 512, JSON_THROW_ON_ERROR);
|
|
} catch (\JsonException $exception) {
|
|
throw new ParseException(['message' => $exception->getMessage()]);
|
|
}
|
|
|
|
return (array) $this->parse($data, $filename);
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
* Parses an JSON string as an array
|
|
*
|
|
* @throws ParseException|\JsonException If there is an error parsing the JSON string
|
|
*/
|
|
public function parseString(string $config): array
|
|
{
|
|
$data = json_decode($config, true, 512, JSON_THROW_ON_ERROR);
|
|
|
|
return (array) $this->parse($data);
|
|
}
|
|
|
|
/**
|
|
* Completes parsing of JSON data
|
|
*
|
|
* @param array | null $data
|
|
* @param string | null $filename
|
|
* @return array | null
|
|
*
|
|
* @throws ParseException If there is an error parsing the JSON data
|
|
*/
|
|
protected function parse(array $data = null, string $filename = null): ?array
|
|
{
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
$error_message = 'Syntax error';
|
|
if (function_exists('json_last_error_msg')) {
|
|
$error_message = json_last_error_msg();
|
|
}
|
|
|
|
$error = [
|
|
'message' => $error_message,
|
|
'type' => json_last_error(),
|
|
'file' => $filename,
|
|
];
|
|
|
|
throw new ParseException($error);
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public static function getSupportedExtensions(): array
|
|
{
|
|
return ['json'];
|
|
}
|
|
}
|