|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BEAR\Resource; |
|
6
|
|
|
|
|
7
|
|
|
use ArrayAccess; |
|
8
|
|
|
use Countable; |
|
9
|
|
|
use Exception; |
|
10
|
|
|
use IteratorAggregate; |
|
11
|
|
|
use JsonSerializable; |
|
12
|
|
|
|
|
13
|
|
|
abstract class ResourceObject implements AcceptTransferInterface, ArrayAccess, Countable, IteratorAggregate, JsonSerializable, ToStringInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Uri |
|
17
|
|
|
* |
|
18
|
|
|
* @var AbstractUri |
|
19
|
|
|
*/ |
|
20
|
|
|
public $uri; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Status code |
|
24
|
|
|
* |
|
25
|
|
|
* @var int |
|
26
|
|
|
*/ |
|
27
|
|
|
public $code = 200; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Resource header |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
public $headers = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Resource representation |
|
38
|
|
|
* |
|
39
|
|
|
* @var null|string |
|
40
|
|
|
*/ |
|
41
|
|
|
public $view; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Body |
|
45
|
|
|
* |
|
46
|
|
|
* @var mixed |
|
47
|
|
|
*/ |
|
48
|
|
|
public $body; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Renderer |
|
52
|
|
|
* |
|
53
|
|
|
* @var \BEAR\Resource\RenderInterface |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $renderer; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Return representational string |
|
59
|
|
|
* |
|
60
|
|
|
* Return object hash if representation renderer is not set. |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
15 |
|
public function __toString() |
|
65
|
|
|
{ |
|
66
|
|
|
try { |
|
67
|
15 |
|
$view = $this->toString(); |
|
68
|
1 |
|
} catch (Exception $e) { |
|
69
|
1 |
|
$msg = sprintf("%s(%s)\n%s", get_class($e), $e->getMessage(), $e->getTraceAsString()); |
|
70
|
1 |
|
trigger_error($msg, E_USER_WARNING); |
|
71
|
|
|
|
|
72
|
1 |
|
return ''; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
14 |
|
return $view; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
public function __sleep() |
|
79
|
|
|
{ |
|
80
|
1 |
|
if (is_array($this->body)) { |
|
81
|
1 |
|
foreach ($this->body as &$item) { |
|
82
|
1 |
|
if ($item instanceof RequestInterface) { |
|
83
|
|
|
$item = ($item)(); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
return ['uri', 'code', 'headers', 'body', 'view']; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Returns the body value at the specified index |
|
93
|
|
|
* |
|
94
|
|
|
* @param mixed $offset offset |
|
95
|
|
|
*/ |
|
96
|
12 |
|
public function offsetGet($offset) |
|
97
|
|
|
{ |
|
98
|
12 |
|
return $this->body[$offset]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Sets the body value at the specified index to renew |
|
103
|
|
|
* |
|
104
|
|
|
* @param mixed $offset offset |
|
105
|
|
|
* @param mixed $value value |
|
106
|
|
|
*/ |
|
107
|
37 |
|
public function offsetSet($offset, $value) |
|
108
|
|
|
{ |
|
109
|
37 |
|
$this->body[$offset] = $value; |
|
110
|
37 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Returns whether the requested index in body exists |
|
114
|
|
|
* |
|
115
|
|
|
* @param mixed $offset offset |
|
116
|
|
|
* |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
3 |
|
public function offsetExists($offset) |
|
120
|
|
|
{ |
|
121
|
3 |
|
return isset($this->body[$offset]); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Set the value at the specified index |
|
126
|
|
|
* |
|
127
|
|
|
* @param mixed $offset offset |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function offsetUnset($offset) |
|
130
|
|
|
{ |
|
131
|
1 |
|
unset($this->body[$offset]); |
|
132
|
1 |
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get the number of public properties in the ArrayObject |
|
136
|
|
|
* |
|
137
|
|
|
* @return int |
|
138
|
|
|
*/ |
|
139
|
1 |
|
public function count() |
|
140
|
|
|
{ |
|
141
|
1 |
|
return count($this->body); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Sort the entries by key |
|
146
|
|
|
*/ |
|
147
|
2 |
|
public function ksort() |
|
148
|
|
|
{ |
|
149
|
2 |
|
if (! is_array($this->body)) { |
|
150
|
1 |
|
return; |
|
151
|
|
|
} |
|
152
|
1 |
|
ksort($this->body); |
|
153
|
1 |
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Sort the entries by key |
|
157
|
|
|
*/ |
|
158
|
2 |
|
public function asort() |
|
159
|
|
|
{ |
|
160
|
2 |
|
if (! is_array($this->body)) { |
|
161
|
1 |
|
return; |
|
162
|
|
|
} |
|
163
|
1 |
|
asort($this->body); |
|
164
|
1 |
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Get array iterator |
|
168
|
|
|
* |
|
169
|
|
|
* @return \ArrayIterator |
|
170
|
|
|
*/ |
|
171
|
2 |
|
public function getIterator() |
|
172
|
|
|
{ |
|
173
|
2 |
|
$isTraversal = (is_array($this->body) || $this->body instanceof \Traversable); |
|
174
|
|
|
|
|
175
|
2 |
|
return $isTraversal ? new \ArrayIterator($this->body) : new \ArrayIterator([]); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Set renderer |
|
180
|
|
|
* |
|
181
|
|
|
* @return $this |
|
182
|
|
|
* @Ray\Di\Di\Inject(optional=true) |
|
183
|
|
|
*/ |
|
184
|
50 |
|
public function setRenderer(RenderInterface $renderer) |
|
185
|
|
|
{ |
|
186
|
50 |
|
$this->renderer = $renderer; |
|
187
|
|
|
|
|
188
|
50 |
|
return $this; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* {@inheritdoc} |
|
193
|
|
|
*/ |
|
194
|
16 |
|
public function toString() |
|
195
|
|
|
{ |
|
196
|
16 |
|
if ($this->view !== null) { |
|
197
|
1 |
|
return $this->view; |
|
198
|
|
|
} |
|
199
|
15 |
|
if (! $this->renderer instanceof RenderInterface) { |
|
200
|
4 |
|
$this->renderer = new JsonRenderer; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
15 |
|
return $this->renderer->render($this); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
12 |
|
public function jsonSerialize() |
|
207
|
|
|
{ |
|
208
|
12 |
|
$body = $this->evaluate($this->body); |
|
209
|
12 |
|
$isTraversable = is_array($body) || $body instanceof \Traversable; |
|
210
|
12 |
|
if (! $isTraversable) { |
|
211
|
2 |
|
return ['value' => $body]; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
10 |
|
return $body; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* {@inheritdoc} |
|
219
|
|
|
*/ |
|
220
|
1 |
|
public function transfer(TransferInterface $responder, array $server) |
|
221
|
|
|
{ |
|
222
|
1 |
|
$responder($this, $server); |
|
223
|
1 |
|
} |
|
224
|
|
|
|
|
225
|
12 |
|
private function evaluate($body) |
|
226
|
|
|
{ |
|
227
|
12 |
|
if (is_array($body)) { |
|
228
|
|
|
/* @noinspection ForeachSourceInspection */ |
|
229
|
10 |
|
foreach ($body as &$value) { |
|
230
|
10 |
|
if ($value instanceof RequestInterface) { |
|
231
|
5 |
|
$result = $value(); |
|
232
|
5 |
|
$value = $result->body; |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
12 |
|
return $body; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|