1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Nate Brunette. |
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
5
|
|
|
*/ |
6
|
|
|
declare(strict_types=1); |
7
|
|
|
|
8
|
|
|
namespace Tebru\Gson\Context; |
9
|
|
|
|
10
|
|
|
use DateTime; |
11
|
|
|
use Tebru\Gson\Internal\Excluder; |
12
|
|
|
use Tebru\Gson\Internal\TypeAdapterProvider; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Context |
16
|
|
|
* |
17
|
|
|
* @author Nate Brunette <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
abstract class Context |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* User defined attributes |
23
|
|
|
* |
24
|
|
|
* @var mixed[] |
25
|
|
|
*/ |
26
|
|
|
public $attributes = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* If the default scalar type adapters should be enabled |
30
|
|
|
* |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
public $enableScalarAdapters = true; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Excluder |
37
|
|
|
*/ |
38
|
|
|
public $excluder; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var TypeAdapterProvider |
42
|
|
|
*/ |
43
|
|
|
public $typeAdapterProvider; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
public $dateFormat = DateTime::ATOM; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get an array of user defined attributes |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function getAttributes(): array |
56
|
|
|
{ |
57
|
|
|
return $this->attributes; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns the attribute value for a given key or null if it's missing |
62
|
|
|
* |
63
|
|
|
* @param string $key |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
public function getAttribute(string $key) |
67
|
|
|
{ |
68
|
|
|
return $this->attributes[$key] ?? null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Add a custom attribute |
73
|
|
|
* |
74
|
|
|
* @param string $key |
75
|
|
|
* @param $value |
76
|
|
|
* @return Context |
77
|
|
|
*/ |
78
|
|
|
public function setAttribute(string $key, $value): Context |
79
|
|
|
{ |
80
|
|
|
$this->attributes[$key] = $value; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* If scalar values should get sent through the type adapter |
87
|
|
|
* system or if they should just be read/written in place. |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function enableScalarAdapters(): bool |
92
|
|
|
{ |
93
|
|
|
return $this->enableScalarAdapters; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Defaults to true. Set to false if scalar values should be read/written |
98
|
|
|
* through a type adapter. |
99
|
|
|
* |
100
|
|
|
* @param bool $enable |
101
|
|
|
* @return Context |
102
|
|
|
*/ |
103
|
|
|
public function setEnableScalarAdapters(bool $enable): Context |
104
|
|
|
{ |
105
|
|
|
$this->enableScalarAdapters = $enable; |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return TypeAdapterProvider |
112
|
|
|
*/ |
113
|
|
|
public function getTypeAdapterProvider(): TypeAdapterProvider |
114
|
|
|
{ |
115
|
|
|
return $this->typeAdapterProvider; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param TypeAdapterProvider $typeAdapterProvider |
120
|
|
|
* @return Context |
121
|
|
|
*/ |
122
|
|
|
public function setTypeAdapterProvider(TypeAdapterProvider $typeAdapterProvider): Context |
123
|
|
|
{ |
124
|
|
|
$this->typeAdapterProvider = $typeAdapterProvider; |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return Excluder |
131
|
|
|
*/ |
132
|
|
|
public function getExcluder(): Excluder |
133
|
|
|
{ |
134
|
|
|
return $this->excluder; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param Excluder $excluder |
139
|
|
|
* @return Context |
140
|
|
|
*/ |
141
|
|
|
public function setExcluder(Excluder $excluder): Context |
142
|
|
|
{ |
143
|
|
|
$this->excluder = $excluder; |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public function getDateFormat(): string |
152
|
|
|
{ |
153
|
|
|
return $this->dateFormat; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $dateFormat |
158
|
|
|
* @return Context |
159
|
|
|
*/ |
160
|
|
|
public function setDateFormat(string $dateFormat): Context |
161
|
|
|
{ |
162
|
|
|
$this->dateFormat = $dateFormat; |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|