This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathResource.php
592 lines (545 loc) · 16.9 KB
/
Resource.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
<?php
/**
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright Copyright (c) 2014 Zend Technologies USA Inc. (http://www.zend.com)
*/
namespace ZF\Rest;
use ArrayObject;
use InvalidArgumentException;
use Traversable;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerInterface;
use Zend\Http\Response;
use Zend\InputFilter\InputFilterInterface;
use Zend\Mvc\Router\RouteMatch as V2RouteMatch;
use Zend\Router\RouteMatch;
use Zend\Stdlib\Parameters;
use ZF\ApiProblem\ApiProblem;
use ZF\ApiProblem\ApiProblemResponse;
use ZF\Hal\Collection as HalCollection;
use ZF\MvcAuth\Identity\IdentityInterface;
/**
* Base resource class
*
* Essentially, simply marshalls arguments and triggers events; it is useless
* without listeners to do the actual work.
*/
class Resource implements ResourceInterface
{
/**
* @var EventManagerInterface
*/
protected $events;
/**
* @var null|IdentityInterface
*/
protected $identity;
/**
* @var null|InputFilterInterface
*/
protected $inputFilter;
/**
* @var array
*/
protected $params = [];
/**
* @var null|Parameters
*/
protected $queryParams;
/**
* @var null|RouteMatch|V2RouteMatch
*/
protected $routeMatch;
/**
* @param array $params
* @return self
*/
public function setEventParams(array $params)
{
$this->params = $params;
return $this;
}
/**
* @return array
*/
public function getEventParams()
{
return $this->params;
}
/**
* @param null|IdentityInterface $identity
* @return self
*/
public function setIdentity(IdentityInterface $identity = null)
{
$this->identity = $identity;
return $this;
}
/**
* @return null|IdentityInterface
*/
public function getIdentity()
{
return $this->identity;
}
/**
* @param null|InputFilterInterface $inputFilter
* @return self
*/
public function setInputFilter(InputFilterInterface $inputFilter = null)
{
$this->inputFilter = $inputFilter;
return $this;
}
/**
* @return null|InputFilterInterface
*/
public function getInputFilter()
{
return $this->inputFilter;
}
/**
* @param Parameters $params
* @return self
*/
public function setQueryParams(Parameters $params)
{
$this->queryParams = $params;
return $this;
}
/**
* @return null|Parameters
*/
public function getQueryParams()
{
return $this->queryParams;
}
/**
* @param RouteMatch|V2RouteMatch $matches
* @return self
*/
public function setRouteMatch($matches)
{
if (! ($matches instanceof RouteMatch || $matches instanceof V2RouteMatch)) {
throw new InvalidArgumentException(sprintf(
'%s expects a %s or %s instance; received %s',
__METHOD__,
RouteMatch::class,
V2RouteMatch::class,
(is_object($matches) ? get_class($matches) : gettype($matches))
));
}
$this->routeMatch = $matches;
return $this;
}
/**
* @return null|RouteMatch|V2RouteMatch
*/
public function getRouteMatch()
{
return $this->routeMatch;
}
/**
* @param string $name
* @param mixed $value
* @return self
*/
public function setEventParam($name, $value)
{
$this->params[$name] = $value;
return $this;
}
/**
* @param mixed $name
* @param mixed $default
* @return mixed
*/
public function getEventParam($name, $default = null)
{
if (isset($this->params[$name])) {
return $this->params[$name];
}
return $default;
}
/**
* Set event manager instance
*
* Sets the event manager identifiers to the current class, this class, and
* the resource interface.
*
* @param EventManagerInterface $events
* @return self
*/
public function setEventManager(EventManagerInterface $events)
{
$events->addIdentifiers([
get_class($this),
__CLASS__,
'ZF\Rest\ResourceInterface',
]);
$this->events = $events;
return $this;
}
/**
* Retrieve event manager
*
* Lazy-instantiates an EM instance if none provided.
*
* @return EventManagerInterface
*/
public function getEventManager()
{
if (! $this->events) {
$this->setEventManager(new EventManager());
}
return $this->events;
}
/**
* Create a record in the resource
*
* Expects either an array or object representing the item to create. If
* a non-array, non-object is provided, raises an exception.
*
* The value returned by the last listener to the "create" event will be
* returned as long as it is an array or object; otherwise, the original
* $data is returned. If you wish to indicate failure to create, raise a
* ZF\Rest\Exception\CreationException from a listener.
*
* @param array|object $data
* @return array|object
* @throws Exception\InvalidArgumentException
*/
public function create($data)
{
if (is_array($data)) {
$data = (object) $data;
}
if (! is_object($data)) {
throw new Exception\InvalidArgumentException(sprintf(
'Data provided to create must be either an array or object; received "%s"',
gettype($data)
));
}
$results = $this->triggerEvent(__FUNCTION__, ['data' => $data]);
$last = $results->last();
if (! is_array($last) && ! is_object($last)) {
return $data;
}
return $last;
}
/**
* Update (replace) an existing item
*
* Updates the item indicated by $id, replacing it with the information
* in $data. $data should be a full representation of the item, and should
* be an array or object; if otherwise, an exception will be raised.
*
* Like create(), the return value of the last executed listener will be
* returned, as long as it is an array or object; otherwise, $data is
* returned. If you wish to indicate failure to update, raise a
* ZF\Rest\Exception\UpdateException.
*
* @param string|int $id
* @param array|object $data
* @return array|object
* @throws Exception\InvalidArgumentException
*/
public function update($id, $data)
{
if (is_array($data)) {
$data = (object) $data;
}
if (! is_object($data)) {
throw new Exception\InvalidArgumentException(sprintf(
'Data provided to update must be either an array or object; received "%s"',
gettype($data)
));
}
$results = $this->triggerEvent(__FUNCTION__, compact('id', 'data'));
$last = $results->last();
if (! is_array($last) && ! is_object($last)) {
return $data;
}
return $last;
}
/**
* Update (replace) an existing collection of items
*
* Replaces the collection with the items contained in $data.
* $data should be a multidimensional array or array of objects; if
* otherwise, an exception will be raised.
*
* Like update(), the return value of the last executed listener will be
* returned, as long as it is an array or object; otherwise, $data is
* returned. If you wish to indicate failure to update, raise a
* ZF\Rest\Exception\UpdateException.
*
* @param array $data
* @return array|object
* @throws Exception\InvalidArgumentException
*/
public function replaceList($data)
{
if (! is_array($data)) {
throw new Exception\InvalidArgumentException(sprintf(
'Data provided to replaceList must be either a multi-dimensional array '
. 'or array of objects; received "%s"',
gettype($data)
), 400);
}
array_walk($data, function ($value, $key) use (&$data) {
if (is_array($value)) {
$data[$key] = (object) $value;
return;
}
if (! is_object($value)) {
throw new Exception\InvalidArgumentException(sprintf(
'Data provided to replaceList must contain only arrays or objects; received "%s"',
gettype($value)
), 400);
}
});
$results = $this->triggerEvent(__FUNCTION__, ['data' => $data]);
$last = $results->last();
if (! is_array($last) && ! is_object($last)) {
return $data;
}
return $last;
}
/**
* Partial update of an existing item
*
* Update the item indicated by $id, using the information from $data;
* $data should be merged with the existing item in order to provide a
* partial update. Additionally, $data should be an array or object; any
* other value will raise an exception.
*
* Like create(), the return value of the last executed listener will be
* returned, as long as it is an array or object; otherwise, $data is
* returned. If you wish to indicate failure to update, raise a
* ZF\Rest\Exception\PatchException.
*
* @param string|int $id
* @param array|object $data
* @return array|object
* @throws Exception\InvalidArgumentException
*/
public function patch($id, $data)
{
if (is_array($data)) {
$data = (object) $data;
}
if (! is_object($data)) {
throw new Exception\InvalidArgumentException(sprintf(
'Data provided to patch must be either an array or object; received "%s"',
gettype($data)
));
}
$results = $this->triggerEvent(__FUNCTION__, compact('id', 'data'));
$last = $results->last();
if (! is_array($last) && ! is_object($last)) {
return $data;
}
return $last;
}
/**
* Patches the collection with the items contained in $data.
* $data should be a multidimensional array or array of objects; if
* otherwise, an exception will be raised.
*
* Like update(), the return value of the last executed listener will be
* returned, as long as it is an array or object; otherwise, $data is
* returned.
*
* As this method can create and update resources, if you wish to indicate
* failure to update, raise a PhlyRestfully\Exception\UpdateException and
* if you wish to indicate a failure to create, raise a
* PhlyRestfully\Exception\CreationException.
*
* @param array $data
* @return array|object
* @throws Exception\InvalidArgumentException
*/
public function patchList($data)
{
if (! is_array($data)) {
throw new Exception\InvalidArgumentException(sprintf(
'Data provided to patchList must be either a multidimensional array or array of objects; received "%s"',
gettype($data)
), 400);
}
$original = $data;
array_walk($data, function ($value, $key) use (&$data) {
if (is_array($value)) {
$data[$key] = new ArrayObject($value);
return;
}
if (! is_object($value)) {
throw new Exception\InvalidArgumentException(sprintf(
'Data provided to patchList must contain only arrays or objects; received "%s"',
gettype($value)
), 400);
}
});
$data = new ArrayObject($data);
$results = $this->triggerEvent(__FUNCTION__, ['data' => $data]);
$last = $results->last();
if (! is_array($last) && ! is_object($last)) {
return $original;
}
return $last;
}
/**
* Delete an existing item
*
* Use to delete the item indicated by $id. The value returned by the last
* listener will be used, as long as it is a boolean; otherwise, a boolean
* false will be returned, indicating failure to delete.
*
* @param string|int $id
* @return bool
*/
public function delete($id)
{
$results = $this->triggerEvent(__FUNCTION__, ['id' => $id]);
$last = $results->last();
if (! is_bool($last)
&& ! $last instanceof ApiProblem
&& ! $last instanceof ApiProblemResponse
&& ! $last instanceof Response
) {
return false;
}
return $last;
}
/**
* Delete an existing collection of records
*
* @param null|array $data
* @return bool
*/
public function deleteList($data = null)
{
if ($data
&& (! is_array($data) && ! $data instanceof Traversable)
) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects a null argument, or an array/Traversable of items and/or ids; received %s',
__METHOD__,
gettype($data)
));
}
$results = $this->triggerEvent(__FUNCTION__, ['data' => $data]);
$last = $results->last();
if (! is_bool($last)
&& ! $last instanceof ApiProblem
&& ! $last instanceof ApiProblemResponse
&& ! $last instanceof Response
) {
return false;
}
return $last;
}
/**
* Fetch an existing item
*
* Retrieve an existing item indicated by $id. The value of the last
* listener will be returned, as long as it is an array or object;
* otherwise, a boolean false value will be returned, indicating a
* lookup failure.
*
* @param string|int $id
* @return false|array|object
*/
public function fetch($id)
{
$results = $this->triggerEvent(__FUNCTION__, ['id' => $id]);
$last = $results->last();
if (! is_array($last) && ! is_object($last)) {
return false;
}
return $last;
}
/**
* Fetch a collection of items
*
* Use to retrieve a collection of items. The value of the last
* listener will be returned, as long as it is an array or Traversable;
* otherwise, an empty array will be returned.
*
* The recommendation is to return a \Zend\Paginator\Paginator instance,
* which will allow performing paginated sets, and thus allow the view
* layer to select the current page based on the query string or route.
*
* @return array|Traversable
*/
public function fetchAll()
{
$params = func_get_args();
$results = $this->triggerEvent(__FUNCTION__, $params);
$last = $results->last();
if (! is_array($last)
&& ! $last instanceof HalCollection
&& ! $last instanceof ApiProblem
&& ! $last instanceof ApiProblemResponse
&& ! is_object($last)
) {
return [];
}
return $last;
}
/**
* @param string $name
* @param array $args
* @return \Zend\EventManager\ResponseCollection
*/
protected function triggerEvent($name, array $args)
{
return $this->getEventManager()->triggerEventUntil(function ($result) {
return ($result instanceof ApiProblem
|| $result instanceof ApiProblemResponse
|| $result instanceof Response
);
}, $this->prepareEvent($name, $args));
}
/**
* Prepare event parameters
*
* Merges any event parameters set in the resources with arguments passed
* to a resource method, and passes them to the `prepareArgs` method of the
* event manager.
*
* If an input filter is composed, this, too, is injected into the event.
*
* @param string $name
* @param array $args
* @return ResourceEvent
*/
protected function prepareEvent($name, array $args)
{
$event = new ResourceEvent($name, $this, $this->prepareEventParams($args));
$event->setIdentity($this->getIdentity());
$event->setInputFilter($this->getInputFilter());
$event->setQueryParams($this->getQueryParams());
$event->setRouteMatch($this->getRouteMatch());
return $event;
}
/**
* Prepare event parameters
*
* Ensures event parameters are created as an array object, allowing them to be modified
* by listeners and retrieved.
*
* @param array $args
* @return ArrayObject
*/
protected function prepareEventParams(array $args)
{
$defaultParams = $this->getEventParams();
$params = array_merge($defaultParams, $args);
if (empty($params)) {
return $params;
}
return $this->getEventManager()->prepareArgs($params);
}
}