|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bankiru\Api\Doctrine; |
|
4
|
|
|
|
|
5
|
|
|
use Bankiru\Api\Doctrine\Mapping\ApiMetadata; |
|
6
|
|
|
use Bankiru\Api\Doctrine\Persister\CollectionMatcher; |
|
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
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
|
12
|
|
|
use ScayTrase\Api\Rpc\RpcClientInterface; |
|
13
|
|
|
|
|
14
|
|
|
class EntityRepository implements ObjectRepository, Selectable |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var ApiMetadata */ |
|
17
|
|
|
private $metadata; |
|
18
|
|
|
/** @var EntityManager */ |
|
19
|
|
|
private $manager; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
18 |
|
* EntityRepository constructor. |
|
23
|
|
|
* |
|
24
|
18 |
|
* @param EntityManager $manager |
|
25
|
18 |
|
* @param string $className |
|
26
|
18 |
|
*/ |
|
27
|
|
|
public function __construct(EntityManager $manager, $className) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->manager = $manager; |
|
30
|
|
|
$this->metadata = $this->manager->getClassMetadata($className); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** {@inheritdoc} */ |
|
34
|
|
|
public function find($id) |
|
35
|
13 |
|
{ |
|
36
|
|
|
return $this->manager->find($this->getClassName(), $id); |
|
37
|
13 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** {@inheritdoc} */ |
|
40
|
|
|
public function getClassName() |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->metadata->getReflectionClass()->getName(); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
17 |
|
/** {@inheritdoc} */ |
|
46
|
|
|
public function findAll() |
|
47
|
17 |
|
{ |
|
48
|
|
|
return $this->findBy([]); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** {@inheritdoc} */ |
|
52
|
|
|
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
|
53
|
|
|
{ |
|
54
|
|
|
$persister = $this->manager->getUnitOfWork()->getEntityPersister($this->getClassName()); |
|
55
|
2 |
|
|
|
56
|
|
|
return $persister->loadAll($criteria, $orderBy, $limit, $offset); |
|
57
|
2 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** {@inheritdoc} */ |
|
60
|
|
|
public function findOneBy(array $criteria) |
|
61
|
|
|
{ |
|
62
|
|
|
$objects = $this->findBy($criteria, [], 1); |
|
63
|
|
|
|
|
64
|
|
|
return array_shift($objects); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return EntityManager |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getManager() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->manager; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** {@inheritdoc} */ |
|
76
|
5 |
|
public function matching(Criteria $criteria) |
|
77
|
|
|
{ |
|
78
|
5 |
|
return new LazyCriteriaCollection(new CollectionMatcher($this->getManager(), $this->getApi()), $criteria); |
|
79
|
|
|
} |
|
80
|
5 |
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return RpcClientInterface |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function getClient() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->manager->getConfiguration()->getClientRegistry()->get($this->metadata->getClientName()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return CrudsApiInterface |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function getApi() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->manager->getUnitOfWork()->getEntityPersister($this->metadata->getName())->getCrudsApi(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return ApiMetadata |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function getMetadata() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->metadata; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Hydrates object from given data or merges it to already fetched object |
|
107
|
|
|
* |
|
108
|
1 |
|
* @param mixed $data |
|
109
|
|
|
* |
|
110
|
1 |
|
* @return object |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function hydrateObject($data) |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->getManager()->getUnitOfWork()->getOrCreateEntity($this->getClassName(), $data); |
|
115
|
|
|
} |
|
116
|
1 |
|
|
|
117
|
|
|
/** |
|
118
|
1 |
|
* @param string $alias |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
* @throws \OutOfBoundsException if no method exist |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function getClientMethod($alias) |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->getMetadata()->getMethodContainer()->getMethod($alias); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|