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