1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tarantool\Mapper; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use LogicException; |
7
|
|
|
|
8
|
|
|
class Repository implements Contracts\Repository |
9
|
|
|
{ |
10
|
|
|
private $type; |
11
|
|
|
private $entities = []; |
12
|
|
|
private $keyMap = []; |
13
|
|
|
private $findCache = []; |
14
|
|
|
|
15
|
|
|
private $magicMethodRules = [ |
16
|
|
|
'by' => false, |
17
|
|
|
'firstBy' => true, |
18
|
|
|
'oneBy' => true, |
19
|
|
|
]; |
20
|
|
|
|
21
|
31 |
|
public function __construct(Contracts\Type $type) |
22
|
|
|
{ |
23
|
31 |
|
$this->type = $type; |
24
|
31 |
|
} |
25
|
|
|
|
26
|
31 |
|
public function create($params = null) |
27
|
|
|
{ |
28
|
31 |
|
if ($params && !is_array($params)) { |
29
|
31 |
|
$params = [$params]; |
30
|
|
|
} |
31
|
|
|
|
32
|
31 |
|
$data = []; |
33
|
31 |
|
foreach ($params as $k => $v) { |
34
|
31 |
|
if (is_numeric($k)) { |
35
|
31 |
|
if ($v instanceof Contracts\Entity) { |
36
|
4 |
|
$type = $this->type->getManager()->findRepository($v)->getType(); |
37
|
4 |
|
$k = $this->type->getReferenceProperty($type); |
38
|
|
|
} else { |
39
|
31 |
|
$primitive = []; |
40
|
31 |
|
foreach ($this->type->getProperties() as $property) { |
41
|
31 |
|
if (!$this->type->isReference($property)) { |
42
|
31 |
|
$primitive[] = $property; |
43
|
|
|
} |
44
|
|
|
} |
45
|
31 |
|
if (count($primitive) == 2) { |
46
|
31 |
|
$k = $primitive[1]; |
47
|
|
|
} else { |
48
|
1 |
|
throw new \Exception("Can't calculate key name"); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
31 |
|
if (!$this->type->hasProperty($k)) { |
53
|
1 |
|
throw new \Exception("Unknown property $k"); |
54
|
|
|
} |
55
|
31 |
|
$data[$k] = $this->type->encodeProperty($k, $v); |
56
|
|
|
} |
57
|
|
|
|
58
|
31 |
|
return $this->register(new Entity($data)); |
59
|
|
|
} |
60
|
|
|
|
61
|
31 |
|
public function __call($method, $arguments) |
62
|
|
|
{ |
63
|
31 |
|
foreach ($this->magicMethodRules as $prefix => $oneItem) { |
64
|
31 |
|
if (substr($method, 0, strlen($prefix)) == $prefix) { |
65
|
31 |
|
$tail = substr($method, strlen($prefix)); |
66
|
31 |
|
$fields = array_map('strtolower', explode('And', $tail)); |
67
|
|
|
|
68
|
31 |
|
return $this->find(array_combine($fields, $arguments), $oneItem); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
throw new BadMethodCallException("Method $method not found"); |
73
|
|
|
} |
74
|
|
|
|
75
|
7 |
|
public function findOne($params) |
76
|
|
|
{ |
77
|
7 |
|
return $this->find($params, true); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
31 |
|
public function find($params = [], $oneItem = false) |
81
|
|
|
{ |
82
|
31 |
|
$query = []; |
83
|
|
|
|
84
|
31 |
|
if (is_string($params) && 1 * $params == $params) { |
85
|
1 |
|
$params = 1 * $params; |
86
|
|
|
} |
87
|
|
|
|
88
|
31 |
|
if (is_int($params)) { |
89
|
8 |
|
if (isset($this->keyMap[$params])) { |
90
|
2 |
|
return $this->entities[$this->keyMap[$params]]; |
91
|
|
|
} |
92
|
|
|
$query = [ |
93
|
6 |
|
'id' => $params, |
94
|
|
|
]; |
95
|
6 |
|
$oneItem = true; |
96
|
|
|
} |
97
|
|
|
|
98
|
31 |
|
if ($params instanceof Contracts\Entity) { |
99
|
2 |
|
$params = [$params]; |
100
|
|
|
} |
101
|
|
|
|
102
|
31 |
|
if (is_array($params)) { |
103
|
31 |
|
foreach ($params as $key => $value) { |
104
|
31 |
|
if (is_numeric($key) && $value instanceof Contracts\Entity) { |
105
|
2 |
|
$type = $this->type->getManager()->findRepository($value)->getType(); |
106
|
2 |
|
$key = $this->type->getReferenceProperty($type); |
107
|
|
|
} |
108
|
31 |
|
if ($this->type->hasProperty($key)) { |
109
|
31 |
|
$query[$key] = $this->type->encodeProperty($key, $value); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
31 |
|
$findKey = md5(json_encode($query)); |
115
|
31 |
|
if (array_key_exists($findKey, $this->findCache)) { |
116
|
31 |
|
return $this->findCache[$findKey]; |
117
|
|
|
} |
118
|
|
|
|
119
|
31 |
|
$index = $this->type->findIndex(array_keys($query)); |
120
|
31 |
|
$values = count($query) ? $this->type->getIndexTuple($index, $query) : []; |
121
|
|
|
|
122
|
31 |
|
$data = $this->type->getSpace()->select($values, $index); |
123
|
|
|
|
124
|
31 |
|
$result = []; |
125
|
31 |
|
if (!empty($data->getData())) { |
126
|
31 |
|
foreach ($data->getData() as $tuple) { |
127
|
31 |
|
$data = $this->type->fromTuple($tuple); |
128
|
31 |
|
if (isset($data['id']) && array_key_exists($data['id'], $this->keyMap)) { |
129
|
31 |
|
$entity = $this->entities[$this->keyMap[$data['id']]]; |
130
|
31 |
|
$entity->update($data); |
131
|
|
|
} else { |
132
|
31 |
|
$entity = new Entity($data); |
133
|
31 |
|
$this->register($entity); |
134
|
|
|
} |
135
|
31 |
|
if ($oneItem) { |
136
|
31 |
|
return $this->findCache[$findKey] = $entity; |
137
|
|
|
} |
138
|
6 |
|
$result[] = $entity; |
139
|
|
|
} |
140
|
|
|
} |
141
|
31 |
|
if (!$oneItem) { |
142
|
6 |
|
return $this->findCache[$findKey] = $result; |
|
|
|
|
143
|
|
|
} |
144
|
31 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return Entity |
148
|
|
|
*/ |
149
|
31 |
|
public function knows(Contracts\Entity $entity) |
150
|
|
|
{ |
151
|
31 |
|
return in_array($entity, $this->entities); |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
public function remove(Contracts\Entity $entity) |
155
|
|
|
{ |
156
|
1 |
|
unset($this->entities[$this->keyMap[$entity->id]]); |
157
|
1 |
|
unset($this->keyMap[$entity->id]); |
158
|
|
|
|
159
|
1 |
|
$this->type->getSpace()->delete([$entity->id]); |
|
|
|
|
160
|
1 |
|
} |
161
|
|
|
|
162
|
31 |
|
public function save(Contracts\Entity $entity) |
163
|
|
|
{ |
164
|
31 |
|
if (!$this->knows($entity)) { |
165
|
1 |
|
throw new LogicException('Entity is not related with this repository'); |
166
|
|
|
} |
167
|
|
|
|
168
|
31 |
|
if (!$entity->getId()) { |
169
|
31 |
|
$this->generateId($entity); |
170
|
31 |
|
$tuple = $this->type->getCompleteTuple($entity->toArray()); |
171
|
31 |
|
$this->type->getSpace()->insert($tuple); |
172
|
|
|
} else { |
173
|
9 |
|
$changes = $entity->pullChanges(); |
174
|
9 |
|
if (count($changes)) { |
175
|
9 |
|
$operations = []; |
176
|
9 |
|
foreach ($this->type->getTuple($changes) as $key => $value) { |
177
|
9 |
|
$operations[] = ['=', $key, $value]; |
178
|
|
|
} |
179
|
|
|
try { |
180
|
9 |
|
$this->type->getSpace()->update($entity->getId(), $operations); |
181
|
1 |
|
} catch(\Exception $e) { |
182
|
1 |
|
$this->type->getSpace()->delete([$entity->getId()]); |
183
|
1 |
|
$tuple = $this->type->getCompleteTuple($entity->toArray()); |
184
|
1 |
|
$this->type->getSpace()->insert($tuple); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
31 |
|
return $entity; |
190
|
|
|
} |
191
|
|
|
|
192
|
31 |
|
private function register(Contracts\Entity $entity) |
193
|
|
|
{ |
194
|
31 |
|
if (!$this->knows($entity)) { |
195
|
31 |
|
$this->entities[] = $entity; |
196
|
|
|
} |
197
|
31 |
|
if ($entity->getId() && !array_key_exists($entity->getId(), $this->keyMap)) { |
198
|
31 |
|
$this->keyMap[$entity->getId()] = array_search($entity, $this->entities); |
199
|
|
|
} |
200
|
|
|
|
201
|
31 |
|
return $entity; |
202
|
|
|
} |
203
|
|
|
|
204
|
31 |
|
private function generateId(Contracts\Entity $entity) |
205
|
|
|
{ |
206
|
31 |
|
$manager = $this->type->getManager(); |
207
|
31 |
|
$name = $this->type->getName(); |
|
|
|
|
208
|
31 |
|
$spaceId = $this->type->getSpaceId(); |
209
|
|
|
|
210
|
31 |
|
$sequence = $manager->get('sequence')->oneBySpace($spaceId); |
211
|
31 |
|
if (!$sequence) { |
212
|
31 |
|
$sequence = $manager->get('sequence')->create([ |
213
|
31 |
|
'space' => $spaceId, |
214
|
31 |
|
'value' => 0, |
215
|
|
|
]); |
216
|
31 |
|
$manager->save($sequence); |
217
|
|
|
} |
218
|
|
|
|
219
|
31 |
|
$nextValue = +$manager->getMeta() |
220
|
31 |
|
->get('sequence') |
221
|
31 |
|
->getSpace() |
222
|
31 |
|
->update($sequence->id, [['+', 2, 1]]) |
223
|
31 |
|
->getData()[0][2]; |
224
|
|
|
|
225
|
31 |
|
$entity->setId($nextValue); |
226
|
|
|
|
227
|
31 |
|
$this->register($entity); |
228
|
|
|
|
229
|
31 |
|
return $entity; |
230
|
|
|
} |
231
|
|
|
|
232
|
4 |
|
public function getType() |
233
|
|
|
{ |
234
|
4 |
|
return $this->type; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|