1
|
|
|
<?php namespace Pz\Doctrine\Rest\Action\Relationships; |
2
|
|
|
|
3
|
|
|
use League\Fractal\TransformerAbstract; |
4
|
|
|
use Pz\Doctrine\Rest\Action\Related\RelatedCollectionAction; |
5
|
|
|
use Pz\Doctrine\Rest\Contracts\RestRequestContract; |
6
|
|
|
use Pz\Doctrine\Rest\RestAction; |
7
|
|
|
use Pz\Doctrine\Rest\RestRepository; |
8
|
|
|
use Pz\Doctrine\Rest\RestResponse; |
9
|
|
|
use Pz\Doctrine\Rest\Traits\CanHydrate; |
10
|
|
|
use Pz\Doctrine\Rest\Traits\RelatedAction; |
11
|
|
|
|
12
|
|
|
class RelationshipsCollectionUpdateAction extends RestAction |
13
|
|
|
{ |
14
|
|
|
use RelatedAction; |
15
|
|
|
use CanHydrate; |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* RelatedRestAction constructor. |
19
|
|
|
* |
20
|
|
|
* @param RestRepository $repository |
21
|
|
|
* @param string $field |
22
|
|
|
* @param string $mappedBy |
23
|
|
|
* @param RestRepository $related |
24
|
|
|
* @param TransformerAbstract $transformer |
25
|
|
|
*/ |
26
|
1 |
|
public function __construct(RestRepository $repository, $field, $mappedBy, RestRepository $related, $transformer) |
27
|
|
|
{ |
28
|
1 |
|
parent::__construct($repository, $transformer); |
29
|
1 |
|
$this->mappedBy = $mappedBy; |
30
|
1 |
|
$this->related = $related; |
31
|
1 |
|
$this->field = $field; |
32
|
1 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param RestRequestContract $request |
36
|
|
|
* |
37
|
|
|
* @return RestResponse |
38
|
|
|
*/ |
39
|
1 |
|
public function handle($request) |
40
|
|
|
{ |
41
|
1 |
|
$entity = $this->repository()->findById($request->getId()); |
42
|
|
|
|
43
|
1 |
|
$this->authorize($request, $entity); |
44
|
|
|
|
45
|
1 |
|
$items = array_map( |
46
|
1 |
|
function($raw) use ($entity) { |
|
|
|
|
47
|
1 |
|
return $this->getRelatedEntity($raw); |
48
|
1 |
|
}, |
49
|
|
|
$request->getData() |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$this->setProperty($entity, $this->field(), $items); |
53
|
|
|
|
54
|
|
|
$this->repository()->getEntityManager()->flush($entity); |
55
|
|
|
|
56
|
|
|
return ( |
57
|
|
|
new RelatedCollectionAction( |
58
|
|
|
$this->repository(), |
59
|
|
|
$this->mappedBy(), |
60
|
|
|
$this->related(), |
61
|
|
|
$this->transformer() |
|
|
|
|
62
|
|
|
) |
63
|
|
|
)->dispatch($request); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|