|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\SkeletonMapper\ObjectRepository; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\EventManager; |
|
8
|
|
|
use Doctrine\SkeletonMapper\DataRepository\ObjectDataRepositoryInterface; |
|
9
|
|
|
use Doctrine\SkeletonMapper\Hydrator\ObjectHydratorInterface; |
|
10
|
|
|
use Doctrine\SkeletonMapper\Mapping\ClassMetadataInterface; |
|
11
|
|
|
use Doctrine\SkeletonMapper\ObjectFactory; |
|
12
|
|
|
use Doctrine\SkeletonMapper\ObjectManagerInterface; |
|
13
|
|
|
use InvalidArgumentException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Base class for object repositories to extend from. |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class ObjectRepository implements ObjectRepositoryInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var ObjectManagerInterface */ |
|
21
|
|
|
protected $objectManager; |
|
22
|
|
|
|
|
23
|
|
|
/** @var ObjectDataRepositoryInterface */ |
|
24
|
|
|
protected $objectDataRepository; |
|
25
|
|
|
|
|
26
|
|
|
/** @var ObjectFactory */ |
|
27
|
|
|
protected $objectFactory; |
|
28
|
|
|
|
|
29
|
|
|
/** @var ObjectHydratorInterface */ |
|
30
|
|
|
protected $objectHydrator; |
|
31
|
|
|
|
|
32
|
|
|
/** @var EventManager */ |
|
33
|
|
|
protected $eventManager; |
|
34
|
|
|
|
|
35
|
|
|
/** @var string */ |
|
36
|
|
|
protected $className; |
|
37
|
|
|
|
|
38
|
|
|
/** @var ClassMetadataInterface */ |
|
39
|
|
|
protected $class; |
|
40
|
|
|
|
|
41
|
32 |
|
public function __construct( |
|
42
|
|
|
ObjectManagerInterface $objectManager, |
|
43
|
|
|
ObjectDataRepositoryInterface $objectDataRepository, |
|
44
|
|
|
ObjectFactory $objectFactory, |
|
45
|
|
|
ObjectHydratorInterface $objectHydrator, |
|
46
|
|
|
EventManager $eventManager, |
|
47
|
|
|
string $className |
|
48
|
|
|
) { |
|
49
|
32 |
|
$this->objectManager = $objectManager; |
|
50
|
32 |
|
$this->objectDataRepository = $objectDataRepository; |
|
51
|
32 |
|
$this->objectFactory = $objectFactory; |
|
52
|
32 |
|
$this->objectHydrator = $objectHydrator; |
|
53
|
32 |
|
$this->eventManager = $eventManager; |
|
54
|
32 |
|
$this->setClassName($className); |
|
55
|
32 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns the class name of the object managed by the repository. |
|
59
|
|
|
*/ |
|
60
|
23 |
|
public function getClassName() : string |
|
61
|
|
|
{ |
|
62
|
23 |
|
return $this->className; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
32 |
|
public function setClassName(string $className) : void |
|
66
|
|
|
{ |
|
67
|
32 |
|
$this->className = $className; |
|
68
|
32 |
|
$this->class = $this->objectManager->getClassMetadata($this->className); |
|
69
|
32 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Finds an object by its primary key / identifier. |
|
73
|
|
|
* |
|
74
|
|
|
* @param mixed $id The identifier. |
|
75
|
|
|
* |
|
76
|
|
|
* @return object|null The object. |
|
77
|
|
|
*/ |
|
78
|
19 |
|
public function find($id) |
|
79
|
|
|
{ |
|
80
|
19 |
|
return $this->getOrCreateObject( |
|
81
|
19 |
|
$this->objectDataRepository->find($id) |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Finds all objects in the repository. |
|
87
|
|
|
* |
|
88
|
|
|
* @return object[] The objects. |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function findAll() : array |
|
91
|
|
|
{ |
|
92
|
2 |
|
$objectsData = $this->objectDataRepository->findAll(); |
|
93
|
|
|
|
|
94
|
2 |
|
$objects = []; |
|
95
|
2 |
|
foreach ($objectsData as $objectData) { |
|
96
|
2 |
|
$object = $this->getOrCreateObject($objectData); |
|
97
|
|
|
|
|
98
|
2 |
|
if ($object === null) { |
|
99
|
|
|
throw new InvalidArgumentException('Could not create object.'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
2 |
|
$objects[] = $object; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
2 |
|
return $objects; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritDoc} |
|
110
|
|
|
*/ |
|
111
|
2 |
|
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null) : array |
|
112
|
|
|
{ |
|
113
|
2 |
|
$objectsData = $this->objectDataRepository->findBy( |
|
114
|
2 |
|
$criteria, |
|
115
|
2 |
|
$orderBy, |
|
116
|
2 |
|
$limit, |
|
117
|
2 |
|
$offset |
|
118
|
|
|
); |
|
119
|
|
|
|
|
120
|
2 |
|
$objects = []; |
|
121
|
2 |
|
foreach ($objectsData as $objectData) { |
|
122
|
2 |
|
$object = $this->getOrCreateObject($objectData); |
|
123
|
|
|
|
|
124
|
2 |
|
if ($object === null) { |
|
125
|
|
|
throw new InvalidArgumentException('Could not create object.'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
2 |
|
$objects[] = $object; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
2 |
|
return $objects; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* {@inheritDoc} |
|
136
|
|
|
*/ |
|
137
|
1 |
|
public function findOneBy(array $criteria) |
|
138
|
|
|
{ |
|
139
|
1 |
|
return $this->getOrCreateObject( |
|
140
|
1 |
|
$this->objectDataRepository->findOneBy($criteria) |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param object $object |
|
146
|
|
|
*/ |
|
147
|
2 |
|
public function refresh($object) : void |
|
148
|
|
|
{ |
|
149
|
2 |
|
$data = $this->objectDataRepository |
|
150
|
2 |
|
->find($this->getObjectIdentifier($object)); |
|
151
|
|
|
|
|
152
|
2 |
|
if ($data === null) { |
|
153
|
|
|
throw new InvalidArgumentException('Could not find object to refresh.'); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
2 |
|
$this->hydrate($object, $data); |
|
157
|
2 |
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param object $object |
|
161
|
|
|
* @param mixed[] $data |
|
162
|
|
|
*/ |
|
163
|
18 |
|
public function hydrate($object, array $data) : void |
|
164
|
|
|
{ |
|
165
|
18 |
|
$this->objectHydrator->hydrate($object, $data); |
|
166
|
18 |
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return object |
|
170
|
|
|
*/ |
|
171
|
18 |
|
public function create(string $className) |
|
172
|
|
|
{ |
|
173
|
18 |
|
return $this->objectFactory->create($className); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @param mixed[] $data |
|
178
|
|
|
* |
|
179
|
|
|
* @return object|null |
|
180
|
|
|
*/ |
|
181
|
22 |
|
protected function getOrCreateObject(?array $data = null) |
|
182
|
|
|
{ |
|
183
|
22 |
|
if ($data === null) { |
|
184
|
3 |
|
return null; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
22 |
|
return $this->objectManager->getOrCreateObject( |
|
188
|
22 |
|
$this->getClassName(), |
|
189
|
22 |
|
$data |
|
190
|
|
|
); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|