-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonDecoder.php
54 lines (44 loc) · 1.22 KB
/
JsonDecoder.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
<?php
declare(strict_types=1);
namespace SonsOfPHP\Component\Json;
/**
* JSON Decoder will covert json to stdClass or array.
*
* @author Joshua Estes <[email protected]>
*/
class JsonDecoder extends AbstractEncoderDecoder
{
public function __construct(bool $associative = null, int $depth = null, int $flags = null)
{
parent::__construct($flags, $depth);
if (true === $associative) {
$this->flags |= \JSON_OBJECT_AS_ARRAY;
}
}
public function decode(string $json)
{
$return = json_decode($json, null, $this->depth, $this->flags);
if (\JSON_ERROR_NONE !== json_last_error()) {
throw new JsonException(json_last_error_msg(), json_last_error());
}
return $return;
}
public function asArray(): static
{
return $this->withFlags(\JSON_OBJECT_AS_ARRAY);
}
/**
* Decodes large integers as their original string value.
*/
public function bigintAsString(): static
{
return $this->withFlags(\JSON_BIGINT_AS_STRING);
}
/**
* Decodes JSON objects as PHP array.
*/
public function objectAsArray(): static
{
return $this->withFlags(\JSON_OBJECT_AS_ARRAY);
}
}