1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bankiru\Api\Doctrine\Persister; |
4
|
|
|
|
5
|
|
|
use Bankiru\Api\Doctrine\ApiEntityManager; |
6
|
|
|
use Bankiru\Api\Doctrine\Proxy\LazyCriteriaCollection; |
7
|
|
|
use Bankiru\Api\Doctrine\Rpc\CrudsApiInterface; |
8
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
9
|
|
|
use Doctrine\Common\Collections\Criteria; |
10
|
|
|
use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor; |
11
|
|
|
use Doctrine\Common\Collections\Selectable; |
12
|
|
|
|
13
|
|
|
final class CollectionPersister implements Selectable |
14
|
|
|
{ |
15
|
|
|
/** @var ApiEntityManager */ |
16
|
|
|
private $manager; |
17
|
|
|
/** @var CrudsApiInterface */ |
18
|
|
|
private $api; |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $association; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* CollectionPersister constructor. |
26
|
|
|
* |
27
|
|
|
* @param ApiEntityManager $manager |
28
|
|
|
* @param CrudsApiInterface $api |
29
|
|
|
* @param array $association |
30
|
|
|
*/ |
31
|
1 |
|
public function __construct(ApiEntityManager $manager, CrudsApiInterface $api, array $association) |
32
|
|
|
{ |
33
|
1 |
|
$this->manager = $manager; |
34
|
1 |
|
$this->api = $api; |
35
|
1 |
|
$this->association = $association; |
36
|
1 |
|
} |
37
|
|
|
|
38
|
1 |
|
public function getManyToManyCollection(array $identifiers) |
39
|
|
|
{ |
40
|
1 |
|
$metadata = $this->api->getMetadata(); |
41
|
1 |
|
if ($metadata->isIdentifierComposite()) { |
42
|
|
|
throw new \LogicException('Lazy loading entities with composite key is not supported'); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
$identifierFields = $metadata->getIdentifierFieldNames(); |
46
|
1 |
|
$identifierField = array_shift($identifierFields); |
47
|
|
|
|
48
|
1 |
|
$criteria = new Criteria(); |
49
|
1 |
|
$criteria->andWhere(Criteria::expr()->in($identifierField, array_values($identifiers))); |
50
|
|
|
|
51
|
1 |
|
if (null !== $this->association['orderBy']) { |
52
|
1 |
|
$criteria->orderBy($this->association['orderBy']); |
53
|
1 |
|
} |
54
|
|
|
|
55
|
1 |
|
return new LazyCriteriaCollection($this, $criteria); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public function matching(Criteria $criteria) |
59
|
|
|
{ |
60
|
1 |
|
if ($this->api instanceof Selectable) { |
61
|
|
|
return $this->api->matching($criteria); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
return $this->search($criteria); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
private function search(Criteria $criteria) |
68
|
|
|
{ |
69
|
1 |
|
$expr = $criteria->getWhereExpression(); |
70
|
|
|
|
71
|
1 |
|
$filter = []; |
72
|
1 |
|
if ($expr) { |
73
|
1 |
|
$visitor = new SimpleCriteriaVisitor(); |
74
|
1 |
|
$filter = $visitor->dispatch($expr); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
1 |
|
$orderings = $criteria->getOrderings(); |
78
|
1 |
|
$offset = $criteria->getFirstResult(); |
79
|
1 |
|
$length = $criteria->getMaxResults(); |
80
|
|
|
|
81
|
1 |
|
$data = $this->api->search($filter, $orderings, $offset, $length); |
82
|
|
|
|
83
|
1 |
|
$entities = []; |
84
|
1 |
|
foreach ($data as $object) { |
85
|
1 |
|
$entities[] = $this->manager->getUnitOfWork()->getOrCreateEntity( |
86
|
1 |
|
$this->api->getMetadata()->getName(), |
87
|
|
|
$object |
88
|
1 |
|
); |
89
|
1 |
|
} |
90
|
|
|
|
91
|
1 |
|
return new ArrayCollection($entities); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|