Assets.php
2 weeks ago
Cache.php
2 weeks ago
Date.php
2 weeks ago
Debug.php
2 weeks ago
FeedReader.php
2 weeks ago
HTML.php
2 weeks ago
Helper.php
2 weeks ago
JSON.php
2 weeks ago
Nonce.php
2 weeks ago
Notice.php
2 weeks ago
Number.php
2 weeks ago
NumberConverter.php
2 weeks ago
Param.php
2 weeks ago
Sanitizing.php
2 weeks ago
Strip.php
2 weeks ago
Templates.php
2 weeks ago
User.php
2 weeks ago
Validating.php
2 weeks ago
WooCommerce.php
2 weeks ago
WordPress.php
2 weeks ago
JSON.php
115 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPParsidate\Helper; |
| 4 | |
| 5 | use Throwable; |
| 6 | |
| 7 | class JSON { |
| 8 | /** |
| 9 | * Decodes a JSON string |
| 10 | * @link https://php.net/manual/en/function.json-decode.php |
| 11 | * |
| 12 | * @param string $json <p> |
| 13 | * The <i>json</i> string being decoded. |
| 14 | * </p> |
| 15 | * <p> |
| 16 | * This function only works with UTF-8 encoded strings. |
| 17 | * </p> |
| 18 | * <p>PHP implements a superset of |
| 19 | * JSON - it will also encode and decode scalar types and <b>NULL</b>. The JSON standard |
| 20 | * only supports these values when they are nested inside an array or an object. |
| 21 | * </p> |
| 22 | * @param bool|null $associative <p> |
| 23 | * When <b>TRUE</b>, returned objects will be converted into |
| 24 | * associative arrays. |
| 25 | * </p> |
| 26 | * @param int $depth [optional] <p> |
| 27 | * User specified recursion depth. |
| 28 | * </p> |
| 29 | * @param int $flags [optional] <p> |
| 30 | * Bitmask of JSON decode options:<br/> |
| 31 | * {@see JSON_BIGINT_AS_STRING} decodes large integers as their original string value.<br/> |
| 32 | * {@see JSON_INVALID_UTF8_IGNORE} ignores invalid UTF-8 characters,<br/> |
| 33 | * {@see JSON_INVALID_UTF8_SUBSTITUTE} converts invalid UTF-8 characters to \0xfffd,<br/> |
| 34 | * {@see JSON_OBJECT_AS_ARRAY} decodes JSON objects as PHP array, since 7.2.0 used by default if $assoc parameter is true,<br/> |
| 35 | * {@see JSON_THROW_ON_ERROR} when passed this flag, the error behaviour of these functions is changed. The global error state is left untouched, and if an error occurs that would otherwise set it, these functions instead throw a JsonException<br/> |
| 36 | * </p> |
| 37 | * |
| 38 | * @return mixed the value encoded in <i>json</i> in appropriate |
| 39 | * PHP type. Values true, false and |
| 40 | * null (case-insensitive) are returned as <b>TRUE</b>, <b>FALSE</b> |
| 41 | * and <b>NULL</b> respectively. <b>NULL</b> is returned if the |
| 42 | * <i>json</i> cannot be decoded or if the encoded |
| 43 | * data is deeper than the recursion limit. |
| 44 | */ |
| 45 | public static function decode( string $json, ?bool $associative = null, int $depth = 512, int $flags = 0 ) { |
| 46 | try { |
| 47 | return json_decode( $json, $associative, $depth, JSON_THROW_ON_ERROR | $flags ); |
| 48 | } catch ( Throwable $e ) { |
| 49 | return null; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | |
| 54 | /** |
| 55 | * Returns the JSON representation of a value |
| 56 | * @link https://php.net/manual/en/function.json-encode.php |
| 57 | * |
| 58 | * @param mixed $value <p> |
| 59 | * The <i>value</i> being encoded. Can be any type except |
| 60 | * a resource. |
| 61 | * </p> |
| 62 | * <p> |
| 63 | * All string data must be UTF-8 encoded. |
| 64 | * </p> |
| 65 | * <p>PHP implements a superset of |
| 66 | * JSON - it will also encode and decode scalar types and <b>NULL</b>. The JSON standard |
| 67 | * only supports these values when they are nested inside an array or an object. |
| 68 | * </p> |
| 69 | * @param int $flags [optional] <p> |
| 70 | * Bitmask consisting of <b>JSON_HEX_QUOT</b>, |
| 71 | * <b>JSON_HEX_TAG</b>, |
| 72 | * <b>JSON_HEX_AMP</b>, |
| 73 | * <b>JSON_HEX_APOS</b>, |
| 74 | * <b>JSON_NUMERIC_CHECK</b>, |
| 75 | * <b>JSON_PRETTY_PRINT</b>, |
| 76 | * <b>JSON_UNESCAPED_SLASHES</b>, |
| 77 | * <b>JSON_FORCE_OBJECT</b>, |
| 78 | * <b>JSON_UNESCAPED_UNICODE</b>. |
| 79 | * <b>JSON_THROW_ON_ERROR</b> The behaviour of these |
| 80 | * constants is described on |
| 81 | * the JSON constants page. |
| 82 | * </p> |
| 83 | * @param int $depth [optional] <p> |
| 84 | * Set the maximum depth. Must be greater than zero. |
| 85 | * </p> |
| 86 | * |
| 87 | * @return string|false a JSON encoded string on success or <b>FALSE</b> on failure. |
| 88 | */ |
| 89 | public static function encode( $value, int $flags = 0, int $depth = 512 ) { |
| 90 | try { |
| 91 | return json_encode( $value, JSON_THROW_ON_ERROR | $flags, $depth ); |
| 92 | } catch ( Throwable $e ) { |
| 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Validate JSON value |
| 99 | * |
| 100 | * @param string $value JSON string |
| 101 | * |
| 102 | * @return bool True, if it hasn't error on decode JSON string |
| 103 | * @throws \JsonException |
| 104 | */ |
| 105 | public static function validate( $value ): bool { |
| 106 | if ( function_exists( 'json_validate' ) ) { |
| 107 | return json_validate( $value ); |
| 108 | } |
| 109 | |
| 110 | json_decode( $value ); |
| 111 | |
| 112 | return json_last_error() === JSON_ERROR_NONE; |
| 113 | } |
| 114 | } |
| 115 |