-
Notifications
You must be signed in to change notification settings - Fork 0
/
Json.php
100 lines (94 loc) · 3.76 KB
/
Json.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\json;
use JsonException;
use phootwork\collection\ArrayList;
use phootwork\collection\Collection;
use phootwork\collection\CollectionUtils;
use phootwork\collection\Map;
class Json {
/**
* Returns the JSON representation of a value
*
* @param mixed $data The value being encoded. Can be any type except a resource. All string data must be UTF-8 encoded.
* @param int $options Bitmask consisting of JSON_FORCE_OBJECT, JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP,
* JSON_HEX_APOS, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_NUMERIC_CHECK,
* JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_PRESERVE_ZERO_FRACTION, JSON_PRETTY_PRINT,
* JSON_UNESCAPED_LINE_TERMINATORS, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE
* @param int $depth Set the maximum depth. Must be greater than zero.
*
* @throws JsonException if something gone wrong
*
* @return string Returns a JSON encoded string
*
* @see https://www.php.net/manual/en/json.constants.php for constant details
*/
public static function encode(mixed $data, int $options = 0, int $depth = 512): string {
return json_encode($data, $options | JSON_THROW_ON_ERROR, $depth);
}
/**
* Decodes a JSON string to an array.
*
* @param string|\Stringable $json The json string being decoded. This only works with UTF-8 encoded strings.
* @param int $options Bitmask consisting of JSON_FORCE_OBJECT, JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP,
* JSON_HEX_APOS, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_NUMERIC_CHECK,
* JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_PRESERVE_ZERO_FRACTION, JSON_PRETTY_PRINT,
* JSON_UNESCAPED_LINE_TERMINATORS, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE
* @param int $depth User specified recursion depth.
*
* @throws JsonException if something gone wrong
*
* @return array Returns the value encoded in json in appropriate PHP type. Values true, false and null
* are returned as TRUE, FALSE and NULL respectively.
*
* @psalm-suppress MixedReturnStatement if `json_decode` doesn't return an array, a `TypeError` exception
* is thrown, which fits for us
* @psalm-suppress MixedInferredReturnType
*/
public static function decode(string|\Stringable $json, int $options = 0, int $depth = 512): array {
return json_decode((string) $json, true, $depth, $options | JSON_THROW_ON_ERROR);
}
/**
* Returns a map collection of the provided json
*
* @param string|\Stringable $json
*
* @return Map
*
* @psalm-suppress MixedArgument `json_decode($json, true)` returns an array
*/
public static function toMap(string|\Stringable $json): Map {
return CollectionUtils::toMap(json_decode((string) $json, true));
}
/**
* Returns a list collection of the provided json
*
* @param string|\Stringable $json
*
* @return ArrayList
*
* @psalm-suppress MixedArgument `json_decode($json, true)` returns an array
*/
public static function toList(string|\Stringable $json): ArrayList {
return CollectionUtils::toList(json_decode((string) $json, true));
}
/**
* Returns a collection (list or map) of the provided json
*
* @param string|\Stringable $json
*
* @return Collection
*
* @psalm-suppress MixedArgument `json_decode($json, true)` returns an array
*/
public static function toCollection(string|\Stringable $json): Collection {
return CollectionUtils::fromCollection(json_decode((string) $json, true));
}
}