2016-10-13 13:11:22 -04:00
|
|
|
<?php declare(strict_types=1);
|
2016-08-04 14:55:37 -04:00
|
|
|
/**
|
|
|
|
* Ion
|
|
|
|
*
|
|
|
|
* Building blocks for web development
|
|
|
|
*
|
2016-10-13 13:11:22 -04:00
|
|
|
* PHP version 7
|
2016-08-26 23:10:20 -04:00
|
|
|
*
|
2016-08-04 14:55:37 -04:00
|
|
|
* @package Ion
|
2016-08-26 23:10:20 -04:00
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2017-02-22 15:42:09 -05:00
|
|
|
* @copyright 2015 - 2017 Timothy J. Warren
|
2016-08-26 23:10:20 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2017-09-13 16:54:09 -04:00
|
|
|
* @version 2.2.0
|
2016-08-26 23:10:20 -04:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/ion
|
2016-08-04 14:55:37 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\Ion;
|
|
|
|
|
|
|
|
use Aviat\Ion\Type\StringType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper class for json convenience methods
|
|
|
|
*/
|
|
|
|
class Json {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode data in json format
|
|
|
|
*
|
|
|
|
* @param mixed $data
|
2016-08-26 17:21:50 -04:00
|
|
|
* @param int $options
|
|
|
|
* @param int $depth
|
2016-08-04 14:55:37 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-01-10 15:49:14 -05:00
|
|
|
public static function encode($data, $options = 0, $depth = 512): string
|
2016-08-04 14:55:37 -04:00
|
|
|
{
|
|
|
|
$json = json_encode($data, $options, $depth);
|
|
|
|
self::check_json_error();
|
|
|
|
return $json;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode data in json format and save to a file
|
|
|
|
*
|
|
|
|
* @param string $filename
|
2016-08-26 17:21:50 -04:00
|
|
|
* @param mixed $data
|
2016-10-20 20:08:11 -04:00
|
|
|
* @param int $jsonOptions - Options to pass to json_encode
|
|
|
|
* @param int $fileOptions - Options to pass to file_get_contents
|
2016-08-04 14:55:37 -04:00
|
|
|
* @return int
|
|
|
|
*/
|
2016-10-20 20:08:11 -04:00
|
|
|
public static function encodeFile(string $filename, $data, int $jsonOptions = 0, int $fileOptions = 0): int
|
2016-08-04 14:55:37 -04:00
|
|
|
{
|
2016-10-20 20:08:11 -04:00
|
|
|
$json = self::encode($data, $jsonOptions);
|
|
|
|
return file_put_contents($filename, $json, $fileOptions);
|
2016-08-04 14:55:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode data from json
|
|
|
|
*
|
2016-10-20 20:08:11 -04:00
|
|
|
* @param string|null $json
|
2016-08-26 17:21:50 -04:00
|
|
|
* @param bool $assoc
|
|
|
|
* @param int $depth
|
|
|
|
* @param int $options
|
2016-08-04 14:55:37 -04:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-10-20 20:08:11 -04:00
|
|
|
public static function decode($json, bool $assoc = TRUE, int $depth = 512, int $options = 0)
|
2016-08-04 14:55:37 -04:00
|
|
|
{
|
|
|
|
// Don't try to decode null
|
|
|
|
if (empty($json))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cast json to string so that streams from guzzle are correctly decoded
|
|
|
|
$data = json_decode((string) $json, $assoc, $depth, $options);
|
|
|
|
|
|
|
|
self::check_json_error();
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode json data loaded from the passed filename
|
|
|
|
*
|
|
|
|
* @param string $filename
|
2016-08-26 17:21:50 -04:00
|
|
|
* @param bool $assoc
|
|
|
|
* @param int $depth
|
|
|
|
* @param int $options
|
2016-08-04 14:55:37 -04:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-10-20 20:08:11 -04:00
|
|
|
public static function decodeFile(string $filename, bool $assoc = TRUE, int $depth = 512, int $options = 0)
|
2016-08-04 14:55:37 -04:00
|
|
|
{
|
|
|
|
$json = file_get_contents($filename);
|
|
|
|
return self::decode($json, $assoc, $depth, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether a string is valid json
|
|
|
|
*
|
2016-08-26 17:21:50 -04:00
|
|
|
* @param string $string
|
2016-08-04 14:55:37 -04:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-10-20 20:08:11 -04:00
|
|
|
public static function isJson(string $string): bool
|
2016-08-04 14:55:37 -04:00
|
|
|
{
|
|
|
|
return StringType::create($string)->isJson();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call the json error functions to check for errors encoding/decoding
|
|
|
|
*
|
|
|
|
* @throws JsonException
|
2016-08-26 23:10:20 -04:00
|
|
|
* @return void
|
2016-08-04 14:55:37 -04:00
|
|
|
*/
|
|
|
|
protected static function check_json_error()
|
|
|
|
{
|
|
|
|
$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',
|
|
|
|
JSON_ERROR_UNSUPPORTED_TYPE => 'JSON_ERROR_UNSUPPORTED_TYPE'
|
|
|
|
];
|
|
|
|
|
|
|
|
$error = json_last_error();
|
|
|
|
$message = json_last_error_msg();
|
|
|
|
|
|
|
|
if (\JSON_ERROR_NONE !== $error)
|
|
|
|
{
|
|
|
|
throw new JsonException("{$constant_map[$error]} - {$message}", $error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// End of JSON.php
|