1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bankiru\Api\Doctrine\Persister; |
4
|
|
|
|
5
|
|
|
use Bankiru\Api\Doctrine\ApiEntityManager; |
6
|
|
|
use Bankiru\Api\Doctrine\Mapping\ApiMetadata; |
7
|
|
|
use Bankiru\Api\Doctrine\Proxy\LazyCriteriaCollection; |
8
|
|
|
use Bankiru\Api\Doctrine\Rpc\CrudsApiInterface; |
9
|
|
|
use Doctrine\Common\Collections\Criteria; |
10
|
|
|
use Doctrine\Common\Collections\Selectable; |
11
|
|
|
|
12
|
|
|
final class CollectionPersister |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $association; |
18
|
|
|
/** |
19
|
|
|
* @var ApiMetadata |
20
|
|
|
*/ |
21
|
|
|
private $metadata; |
22
|
|
|
/** |
23
|
|
|
* @var Selectable |
24
|
|
|
*/ |
25
|
|
|
private $matcher; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* CollectionPersister constructor. |
29
|
|
|
* |
30
|
|
|
* @param ApiMetadata $metadata |
31
|
1 |
|
* @param Selectable $matcher |
32
|
|
|
* @param array $association |
33
|
1 |
|
* |
34
|
1 |
|
* @internal param CrudsApiInterface $api |
35
|
1 |
|
*/ |
36
|
1 |
|
public function __construct(ApiMetadata $metadata, Selectable $matcher, array $association) |
37
|
|
|
{ |
38
|
1 |
|
$this->association = $association; |
39
|
|
|
$this->metadata = $metadata; |
40
|
1 |
|
$this->matcher = $matcher; |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
public function getManyToManyCollection(array $identifiers) |
44
|
|
|
{ |
45
|
1 |
|
if ($this->metadata->isIdentifierComposite()) { |
46
|
1 |
|
throw new \LogicException('Lazy loading entities with composite key is not supported'); |
47
|
|
|
} |
48
|
1 |
|
|
49
|
1 |
|
$identifierFields = $this->metadata->getIdentifierFieldNames(); |
50
|
|
|
$identifierField = array_shift($identifierFields); |
51
|
1 |
|
|
52
|
1 |
|
$criteria = new Criteria(); |
53
|
1 |
|
$criteria->andWhere(Criteria::expr()->in($identifierField, array_values($identifiers))); |
54
|
|
|
|
55
|
1 |
|
if (null !== $this->association['orderBy']) { |
56
|
|
|
$criteria->orderBy($this->association['orderBy']); |
57
|
|
|
} |
58
|
1 |
|
|
59
|
|
|
return new LazyCriteriaCollection($this->matcher, $criteria); |
60
|
1 |
|
} |
61
|
|
|
} |
62
|
|
|
|