-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractEncoderDecoder.php
76 lines (62 loc) · 1.86 KB
/
AbstractEncoderDecoder.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
<?php
declare(strict_types=1);
namespace SonsOfPHP\Component\Json;
/**
* This class is used to reduce some of the code duplication that appears
* in the JsonEncoder and JsonDecoder classes.
*
* @author Joshua Estes <joshua@sonsofphp.com>
*/
abstract class AbstractEncoderDecoder
{
protected int $flags = 0;
protected int $depth = 512;
public function __construct(int $flags = null, int $depth = null)
{
$this->flags = $flags ?? $this->flags;
$this->depth = $depth ?? $this->depth;
}
public function withFlags(int $flag): static
{
$that = clone $this;
$that->flags = $this->flags | $flag;
return $that;
}
public function withoutFlags(int $flag): static
{
$that = clone $this;
$that->flags = $this->flags & ~$flag;
return $that;
}
public function withDepth(int $depth): static
{
$that = clone $this;
$that->depth = $depth;
return $that;
}
/**
* Ignore invalid UTF-8 characters. Available as of PHP 7.2.0.
*/
public function invalidUtf8Ignore(): static
{
return $this->withFlags(\JSON_INVALID_UTF8_IGNORE);
}
/**
* Convert invalid UTF-8 characters to \0xfffd (Unicode Character
* 'REPLACEMENT CHARACTER') Available as of PHP 7.2.0.
*/
public function invalidUtf8Substitute(): static
{
return $this->withFlags(\JSON_INVALID_UTF8_SUBSTITUTE);
}
/**
* Throws JsonException if an error occurs instead of setting the global
* error state that is retrieved with json_last_error() and
* json_last_error_msg(). JSON_PARTIAL_OUTPUT_ON_ERROR takes precedence
* over JSON_THROW_ON_ERROR. Available as of PHP 7.3.0.
*/
public function throwOnError(): static
{
return $this->withFlags(\JSON_THROW_ON_ERROR);
}
}