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\Exceptions\RestException; |
7
|
|
|
use Pz\Doctrine\Rest\RestAction; |
8
|
|
|
use Pz\Doctrine\Rest\RestRepository; |
9
|
|
|
use Pz\Doctrine\Rest\RestResponse; |
10
|
|
|
use Pz\Doctrine\Rest\Traits\CanHydrate; |
11
|
|
|
use Pz\Doctrine\Rest\Traits\RelatedAction; |
12
|
|
|
|
13
|
|
|
class RelationshipsCollectionCreateAction extends RestAction |
14
|
|
|
{ |
15
|
|
|
use RelatedAction; |
16
|
|
|
use CanHydrate; |
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* RelatedRestAction constructor. |
20
|
|
|
* |
21
|
|
|
* @param RestRepository $repository |
22
|
|
|
* @param string $field |
23
|
|
|
* @param string $mappedBy |
24
|
|
|
* @param RestRepository $related |
25
|
|
|
* @param TransformerAbstract $transformer |
26
|
|
|
*/ |
27
|
1 |
|
public function __construct(RestRepository $repository, $field, $mappedBy, RestRepository $related, $transformer) |
28
|
|
|
{ |
29
|
1 |
|
parent::__construct($repository, $transformer); |
30
|
1 |
|
$this->mappedBy = $mappedBy; |
31
|
1 |
|
$this->related = $related; |
32
|
1 |
|
$this->field = $field; |
33
|
1 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param RestRequestContract $request |
37
|
|
|
* |
38
|
|
|
* @return RestResponse |
39
|
|
|
* @throws RestException |
40
|
|
|
*/ |
41
|
1 |
|
public function handle($request) |
42
|
|
|
{ |
43
|
1 |
|
$entity = $this->repository()->findById($request->getId()); |
44
|
1 |
|
$this->authorize($request, $entity); |
45
|
|
|
|
46
|
1 |
|
foreach ($request->getData() as $raw) { |
47
|
1 |
|
$item = $this->getRelatedEntity($raw); |
48
|
1 |
|
$this->addRelationItem($entity, $this->field(), $item); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
$this->repository()->getEntityManager()->flush($entity); |
52
|
|
|
|
53
|
|
|
return ( |
54
|
1 |
|
new RelatedCollectionAction( |
55
|
1 |
|
$this->repository(), |
56
|
1 |
|
$this->mappedBy(), |
57
|
1 |
|
$this->related(), |
58
|
1 |
|
$this->transformer() |
|
|
|
|
59
|
|
|
) |
60
|
1 |
|
)->dispatch($request); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|