2020-03-11 23:04:01 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
2020-03-12 11:45:11 -04:00
|
|
|
* Hummingbird Anime List Client
|
2020-03-11 23:04:01 -04:00
|
|
|
*
|
2020-03-12 11:45:11 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2020-03-11 23:04:01 -04:00
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
2020-03-11 23:04:01 -04:00
|
|
|
*
|
2022-03-04 15:50:35 -05:00
|
|
|
* @copyright 2015 - 2022 Timothy J. Warren <tim@timshome.page>
|
2020-03-11 23:04:01 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
2022-03-04 15:50:35 -05:00
|
|
|
* @link https://git.timshome.page/timw4mail/HummingBirdAnimeClient
|
2020-03-11 23:04:01 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\Ion;
|
|
|
|
|
|
|
|
use Aviat\Ion\Type\StringType;
|
2022-03-03 17:26:09 -05:00
|
|
|
use InvalidArgumentException;
|
2020-03-11 23:04:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper class for json convenience methods
|
|
|
|
*/
|
2022-03-03 17:26:09 -05:00
|
|
|
class Json
|
|
|
|
{
|
2020-03-11 23:04:01 -04:00
|
|
|
/**
|
|
|
|
* Encode data in json format
|
|
|
|
*
|
|
|
|
* @throws JsonException
|
|
|
|
*/
|
2021-02-10 17:17:51 -05:00
|
|
|
public static function encode(mixed $data, int $options = 0, int $depth = 512): string
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
|
|
|
$json = json_encode($data, $options, $depth);
|
|
|
|
self::check_json_error();
|
2021-02-10 17:17:51 -05:00
|
|
|
|
|
|
|
return ($json !== FALSE) ? $json : '';
|
2020-03-11 23:04:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode data in json format and save to a file
|
|
|
|
*
|
2022-03-03 17:26:09 -05:00
|
|
|
* @param int $jsonOptions - Options to pass to json_encode
|
|
|
|
* @param int $fileOptions - Options to pass to file_get_contents
|
2020-03-11 23:04:01 -04:00
|
|
|
* @throws JsonException
|
|
|
|
*/
|
2022-03-04 12:19:47 -05:00
|
|
|
public static function encodeFile(string $filename, mixed $data, int $jsonOptions = 0, int $fileOptions = 0): int
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
|
|
|
$json = self::encode($data, $jsonOptions);
|
2021-02-10 17:17:51 -05:00
|
|
|
|
|
|
|
$res = file_put_contents($filename, $json, $fileOptions);
|
2022-03-03 17:26:09 -05:00
|
|
|
|
2022-03-04 12:19:47 -05:00
|
|
|
return ($res !== FALSE) ? $res : 0;
|
2020-03-11 23:04:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode data from json
|
|
|
|
*
|
|
|
|
* @throws JsonException
|
|
|
|
*/
|
2021-02-10 17:17:51 -05:00
|
|
|
public static function decode(?string $json, bool $assoc = TRUE, int $depth = 512, int $options = 0): mixed
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
|
|
|
// Don't try to decode null
|
|
|
|
if ($json === NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = json_decode($json, $assoc, $depth, $options);
|
|
|
|
|
|
|
|
self::check_json_error();
|
2022-03-03 17:26:09 -05:00
|
|
|
|
2020-03-11 23:04:01 -04:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode json data loaded from the passed filename
|
|
|
|
*
|
|
|
|
* @throws JsonException
|
|
|
|
*/
|
2021-02-10 17:17:51 -05:00
|
|
|
public static function decodeFile(string $filename, bool $assoc = TRUE, int $depth = 512, int $options = 0): mixed
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
2021-02-10 17:17:51 -05:00
|
|
|
$rawJson = file_get_contents($filename);
|
|
|
|
$json = ($rawJson !== FALSE) ? $rawJson : '';
|
|
|
|
|
2020-03-11 23:04:01 -04:00
|
|
|
return self::decode($json, $assoc, $depth, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether a string is valid json
|
|
|
|
*
|
2022-03-03 17:26:09 -05:00
|
|
|
* @throws InvalidArgumentException
|
2020-03-11 23:04:01 -04:00
|
|
|
*/
|
|
|
|
public static function isJson(string $string): bool
|
|
|
|
{
|
|
|
|
return StringType::create($string)->isJson();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call the json error functions to check for errors encoding/decoding
|
|
|
|
*
|
|
|
|
* @throws JsonException
|
|
|
|
*/
|
|
|
|
protected static function check_json_error(): void
|
|
|
|
{
|
|
|
|
$constant_map = [
|
|
|
|
JSON_ERROR_NONE => 'JSON_ERROR_NONE',
|
|
|
|
JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH',
|
|
|
|
JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH',
|
|
|
|
JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR',
|
|
|
|
JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX',
|
|
|
|
JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8',
|
|
|
|
JSON_ERROR_RECURSION => 'JSON_ERROR_RECURSION',
|
|
|
|
JSON_ERROR_INF_OR_NAN => 'JSON_ERROR_INF_OR_NAN',
|
2022-03-03 17:26:09 -05:00
|
|
|
JSON_ERROR_UNSUPPORTED_TYPE => 'JSON_ERROR_UNSUPPORTED_TYPE',
|
2020-03-11 23:04:01 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
$error = json_last_error();
|
|
|
|
$message = json_last_error_msg();
|
|
|
|
|
|
|
|
if (JSON_ERROR_NONE !== $error)
|
|
|
|
{
|
|
|
|
throw new JsonException("{$constant_map[$error]} - {$message}", $error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-03 13:25:10 -05:00
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
// End of JSON.php
|