1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: batanov.pavel |
5
|
|
|
* Date: 29.12.2015 |
6
|
|
|
* Time: 11:11 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Bankiru\Api\Doctrine; |
10
|
|
|
|
11
|
|
|
use Bankiru\Api\Doctrine\Mapping\ApiMetadata; |
12
|
|
|
use Bankiru\Api\Doctrine\Proxy\ApiCollection; |
13
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
14
|
|
|
use ScayTrase\Api\Rpc\RpcClientInterface; |
15
|
|
|
|
16
|
|
|
class EntityRepository implements ObjectRepository |
17
|
|
|
{ |
18
|
|
|
/** @var ApiMetadata */ |
19
|
|
|
private $metadata; |
20
|
|
|
/** @var EntityManager */ |
21
|
|
|
private $manager; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* EntityRepository constructor. |
25
|
|
|
* |
26
|
|
|
* @param EntityManager $manager |
27
|
|
|
* @param string $className |
28
|
|
|
*/ |
29
|
12 |
|
public function __construct(EntityManager $manager, $className) |
30
|
|
|
{ |
31
|
12 |
|
$this->manager = $manager; |
32
|
12 |
|
$this->metadata = $this->manager->getClassMetadata($className); |
33
|
12 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Finds an object by its primary key / identifier. |
37
|
|
|
* |
38
|
|
|
* @param mixed $id The identifier. |
39
|
|
|
* |
40
|
|
|
* @return object The object. |
41
|
|
|
*/ |
42
|
9 |
|
public function find($id) |
43
|
|
|
{ |
44
|
9 |
|
return $this->manager->find($this->getClassName(), $id); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns the class name of the object managed by the repository. |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
9 |
|
public function getClassName() |
53
|
|
|
{ |
54
|
9 |
|
return $this->metadata->getReflectionClass()->getName(); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Finds all objects in the repository. |
59
|
|
|
* |
60
|
|
|
* @return array The objects. |
61
|
|
|
*/ |
62
|
|
|
public function findAll() |
63
|
|
|
{ |
64
|
|
|
return $this->findBy([]); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Finds objects by a set of criteria. |
69
|
|
|
* |
70
|
|
|
* Optionally sorting and limiting details can be passed. An implementation may throw |
71
|
|
|
* an UnexpectedValueException if certain values of the sorting or limiting details are |
72
|
|
|
* not supported. |
73
|
|
|
* |
74
|
|
|
* @param array $criteria |
75
|
|
|
* @param array|null $orderBy |
76
|
|
|
* @param int|null $limit |
77
|
|
|
* @param int|null $offset |
78
|
|
|
* |
79
|
|
|
* @return array The objects. |
80
|
|
|
* |
81
|
|
|
* @throws \UnexpectedValueException |
82
|
|
|
*/ |
83
|
3 |
|
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
84
|
|
|
{ |
85
|
3 |
|
return new ApiCollection($this->manager, $this->metadata, [$criteria, $orderBy, $limit, $offset]); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Finds a single object by a set of criteria. |
90
|
|
|
* |
91
|
|
|
* @param array $criteria The criteria. |
92
|
|
|
* |
93
|
|
|
* @return object The object. |
94
|
|
|
*/ |
95
|
|
|
public function findOneBy(array $criteria) |
96
|
|
|
{ |
97
|
|
|
$objects = $this->findBy($criteria, [], 1); |
98
|
|
|
|
99
|
|
|
return array_shift($objects); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return RpcClientInterface |
104
|
|
|
*/ |
105
|
1 |
|
protected function getClient() |
106
|
|
|
{ |
107
|
1 |
|
return $this->manager->getConfiguration()->getRegistry()->get($this->metadata->getClientName()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return ApiMetadata |
112
|
|
|
*/ |
113
|
1 |
|
protected function getMetadata() |
114
|
|
|
{ |
115
|
1 |
|
return $this->metadata; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return EntityManager |
120
|
|
|
*/ |
121
|
|
|
public function getManager() |
122
|
|
|
{ |
123
|
|
|
return $this->manager; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|