1
|
|
|
<?php namespace Pz\Doctrine\Rest\Action\Related; |
2
|
|
|
|
3
|
|
|
use Pz\Doctrine\Rest\Action\ItemAction as BaseItemAction; |
4
|
|
|
use Pz\Doctrine\Rest\Contracts\RestRequestContract; |
5
|
|
|
use Pz\Doctrine\Rest\Resource\Item; |
6
|
|
|
use Pz\Doctrine\Rest\Resource\NullResource; |
7
|
|
|
use Pz\Doctrine\Rest\RestResponse; |
8
|
|
|
use Pz\Doctrine\Rest\RestRepository; |
9
|
|
|
use Pz\Doctrine\Rest\Traits\RelatedAction; |
10
|
|
|
|
11
|
|
|
use League\Fractal\TransformerAbstract; |
12
|
|
|
|
13
|
|
|
class RelatedItemAction extends BaseItemAction |
14
|
|
|
{ |
15
|
|
|
use RelatedAction; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* RelatedRestAction constructor. |
19
|
|
|
* |
20
|
|
|
* @param RestRepository $repository |
21
|
|
|
* @param string $field |
22
|
|
|
* @param RestRepository $related |
23
|
|
|
* @param TransformerAbstract $transformer |
24
|
|
|
*/ |
25
|
2 |
|
public function __construct(RestRepository $repository, $field, RestRepository $related, $transformer) |
26
|
|
|
{ |
27
|
2 |
|
parent::__construct($repository, $transformer); |
28
|
2 |
|
$this->related = $related; |
29
|
2 |
|
$this->field = $field; |
30
|
2 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param RestRequestContract $request |
34
|
|
|
* |
35
|
|
|
* @return RestResponse |
36
|
|
|
*/ |
37
|
2 |
|
public function handle($request) |
38
|
|
|
{ |
39
|
2 |
|
$entity = $this->repository()->findById($request->getId()); |
40
|
|
|
|
41
|
2 |
|
$this->authorize($request, $entity); |
42
|
|
|
|
43
|
2 |
|
if ($relatedEntity = $this->getProperty($entity, $this->field())) { |
44
|
2 |
|
return $this->response()->resource($request, |
45
|
2 |
|
new Item($relatedEntity, $this->transformer()) |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
return $this->response()->resource($request, new NullResource()); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|