@@ -236,6 +236,9 @@ |
||
236 | 236 | $this->cache = []; |
237 | 237 | } |
238 | 238 | |
239 | + /** |
|
240 | + * @param Entity $instance |
|
241 | + */ |
|
239 | 242 | public function save($instance) |
240 | 243 | { |
241 | 244 | $tuple = []; |
@@ -24,26 +24,26 @@ discard block |
||
24 | 24 | public function create($data) |
25 | 25 | { |
26 | 26 | $class = Entity::class; |
27 | - foreach($this->space->getMapper()->getPlugins() as $plugin) { |
|
27 | + foreach ($this->space->getMapper()->getPlugins() as $plugin) { |
|
28 | 28 | $entityClass = $plugin->getEntityClass($this->space); |
29 | - if($entityClass) { |
|
30 | - if($class != Entity::class) { |
|
29 | + if ($entityClass) { |
|
30 | + if ($class != Entity::class) { |
|
31 | 31 | throw new Exception('Entity class override'); |
32 | 32 | } |
33 | 33 | $class = $entityClass; |
34 | 34 | } |
35 | 35 | } |
36 | 36 | $instance = new $class($this); |
37 | - foreach($this->space->getFormat() as $row) { |
|
38 | - if(array_key_exists($row['name'], $data)) { |
|
37 | + foreach ($this->space->getFormat() as $row) { |
|
38 | + if (array_key_exists($row['name'], $data)) { |
|
39 | 39 | $instance->{$row['name']} = $data[$row['name']]; |
40 | - if($data[$row['name']] instanceof Entity) { |
|
40 | + if ($data[$row['name']] instanceof Entity) { |
|
41 | 41 | $instance->{$row['name']} = $instance->{$row['name']}->id; |
42 | 42 | } |
43 | 43 | } |
44 | 44 | } |
45 | 45 | |
46 | - foreach($this->space->getMapper()->getPlugins() as $plugin) { |
|
46 | + foreach ($this->space->getMapper()->getPlugins() as $plugin) { |
|
47 | 47 | $plugin->generateKey($instance, $this->space); |
48 | 48 | } |
49 | 49 | |
@@ -62,18 +62,18 @@ discard block |
||
62 | 62 | public function find($params = [], $one = false) |
63 | 63 | { |
64 | 64 | $cacheIndex = array_search([$params, $one], $this->cache); |
65 | - if($cacheIndex !== false) { |
|
65 | + if ($cacheIndex !== false) { |
|
66 | 66 | return $this->results[$cacheIndex]; |
67 | 67 | } |
68 | 68 | |
69 | - if(!is_array($params)) { |
|
69 | + if (!is_array($params)) { |
|
70 | 70 | $params = [$params]; |
71 | 71 | } |
72 | - if(count($params) == 1 && array_key_exists(0, $params)) { |
|
72 | + if (count($params) == 1 && array_key_exists(0, $params)) { |
|
73 | 73 | $primary = $this->space->getPrimaryIndex(); |
74 | - if(count($primary->parts) == 1) { |
|
74 | + if (count($primary->parts) == 1) { |
|
75 | 75 | $formatted = $this->space->getMapper()->getSchema()->formatValue($primary->parts[0][1], $params[0]); |
76 | - if($params[0] == $formatted) { |
|
76 | + if ($params[0] == $formatted) { |
|
77 | 77 | $params = [ |
78 | 78 | $this->space->getFormat()[$primary->parts[0][0]]['name'] => $params[0] |
79 | 79 | ]; |
@@ -81,8 +81,8 @@ discard block |
||
81 | 81 | } |
82 | 82 | } |
83 | 83 | |
84 | - if(array_key_exists('id', $params)) { |
|
85 | - if(array_key_exists($params['id'], $this->persisted)) { |
|
84 | + if (array_key_exists('id', $params)) { |
|
85 | + if (array_key_exists($params['id'], $this->persisted)) { |
|
86 | 86 | $instance = $this->persisted[$params['id']]; |
87 | 87 | return $one ? $instance : [$instance]; |
88 | 88 | } |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | |
91 | 91 | |
92 | 92 | $index = $this->space->castIndex($params); |
93 | - if(is_null($index)) { |
|
93 | + if (is_null($index)) { |
|
94 | 94 | throw new Exception("No index for params ".json_encode($params)); |
95 | 95 | } |
96 | 96 | |
@@ -103,15 +103,15 @@ discard block |
||
103 | 103 | $data = $client->getSpace($this->space->getId())->select($values, $index)->getData(); |
104 | 104 | |
105 | 105 | $result = []; |
106 | - foreach($data as $tuple) { |
|
106 | + foreach ($data as $tuple) { |
|
107 | 107 | $instance = $this->getInstance($tuple); |
108 | - if($one) { |
|
108 | + if ($one) { |
|
109 | 109 | return $this->results[$cacheIndex] = $instance; |
110 | 110 | } |
111 | 111 | $result[] = $instance; |
112 | 112 | } |
113 | 113 | |
114 | - if($one) { |
|
114 | + if ($one) { |
|
115 | 115 | return $this->results[$cacheIndex] = null; |
116 | 116 | } |
117 | 117 | |
@@ -122,15 +122,15 @@ discard block |
||
122 | 122 | { |
123 | 123 | $key = $this->space->getTupleKey($tuple); |
124 | 124 | |
125 | - if(array_key_exists($key, $this->persisted)) { |
|
125 | + if (array_key_exists($key, $this->persisted)) { |
|
126 | 126 | return $this->persisted[$key]; |
127 | 127 | } |
128 | 128 | |
129 | 129 | $class = Entity::class; |
130 | - foreach($this->space->getMapper()->getPlugins() as $plugin) { |
|
130 | + foreach ($this->space->getMapper()->getPlugins() as $plugin) { |
|
131 | 131 | $entityClass = $plugin->getEntityClass($this->space); |
132 | - if($entityClass) { |
|
133 | - if($class != Entity::class) { |
|
132 | + if ($entityClass) { |
|
133 | + if ($class != Entity::class) { |
|
134 | 134 | throw new Exception('Entity class override'); |
135 | 135 | } |
136 | 136 | $class = $entityClass; |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | |
141 | 141 | $this->original[$key] = $tuple; |
142 | 142 | |
143 | - foreach($this->space->getFormat() as $index => $info) { |
|
143 | + foreach ($this->space->getFormat() as $index => $info) { |
|
144 | 144 | $instance->{$info['name']} = array_key_exists($index, $tuple) ? $tuple[$index] : null; |
145 | 145 | } |
146 | 146 | |
@@ -156,26 +156,26 @@ discard block |
||
156 | 156 | |
157 | 157 | public function update(Entity $instance, $operations) |
158 | 158 | { |
159 | - if(!count($operations)) { |
|
159 | + if (!count($operations)) { |
|
160 | 160 | return; |
161 | 161 | } |
162 | 162 | |
163 | 163 | $tupleOperations = []; |
164 | - foreach($operations as $operation) { |
|
164 | + foreach ($operations as $operation) { |
|
165 | 165 | $tupleIndex = $this->space->getPropertyIndex($operation[1]); |
166 | 166 | $tupleOperations[] = [$operation[0], $tupleIndex, $operation[2]]; |
167 | 167 | } |
168 | 168 | |
169 | 169 | $pk = []; |
170 | - foreach($this->space->getPrimaryIndex()->parts as $part) { |
|
170 | + foreach ($this->space->getPrimaryIndex()->parts as $part) { |
|
171 | 171 | $pk[] = $instance->{$this->space->getFormat()[$part[0]]['name']}; |
172 | 172 | } |
173 | 173 | |
174 | 174 | $client = $this->space->getMapper()->getClient(); |
175 | 175 | $result = $client->getSpace($this->space->getId())->update($pk, $tupleOperations); |
176 | - foreach($result->getData() as $tuple) { |
|
177 | - foreach($this->space->getFormat() as $index => $info) { |
|
178 | - if(array_key_exists($index, $tuple)) { |
|
176 | + foreach ($result->getData() as $tuple) { |
|
177 | + foreach ($this->space->getFormat() as $index => $info) { |
|
178 | + if (array_key_exists($index, $tuple)) { |
|
179 | 179 | $instance->{$info['name']} = $tuple[$index]; |
180 | 180 | } |
181 | 181 | } |
@@ -192,15 +192,15 @@ discard block |
||
192 | 192 | |
193 | 193 | public function remove($params = []) |
194 | 194 | { |
195 | - if($params instanceof Entity) { |
|
195 | + if ($params instanceof Entity) { |
|
196 | 196 | return $this->removeEntity($params); |
197 | 197 | } |
198 | 198 | |
199 | - if(!count($params)) { |
|
199 | + if (!count($params)) { |
|
200 | 200 | throw new Exception("Use truncate to flush space"); |
201 | 201 | } |
202 | 202 | |
203 | - foreach($this->find($params) as $entity) { |
|
203 | + foreach ($this->find($params) as $entity) { |
|
204 | 204 | $this->removeEntity($entity); |
205 | 205 | } |
206 | 206 | } |
@@ -209,19 +209,19 @@ discard block |
||
209 | 209 | { |
210 | 210 | $key = $this->space->getInstanceKey($instance); |
211 | 211 | |
212 | - if(!array_key_exists($key, $this->original)) { |
|
212 | + if (!array_key_exists($key, $this->original)) { |
|
213 | 213 | return; |
214 | 214 | } |
215 | 215 | |
216 | - if(array_key_exists($key, $this->persisted)) { |
|
216 | + if (array_key_exists($key, $this->persisted)) { |
|
217 | 217 | |
218 | 218 | unset($this->persisted[$key]); |
219 | 219 | |
220 | 220 | $pk = []; |
221 | - foreach($this->space->getPrimaryIndex()->parts as $part) { |
|
221 | + foreach ($this->space->getPrimaryIndex()->parts as $part) { |
|
222 | 222 | $pk[] = $this->original[$key][$part[0]]; |
223 | 223 | } |
224 | - foreach($this->space->getMapper()->getPlugins() as $plugin) { |
|
224 | + foreach ($this->space->getMapper()->getPlugins() as $plugin) { |
|
225 | 225 | $plugin->beforeRemove($instance, $this->space); |
226 | 226 | } |
227 | 227 | |
@@ -243,8 +243,8 @@ discard block |
||
243 | 243 | $size = count(get_object_vars($instance)); |
244 | 244 | $skipped = 0; |
245 | 245 | |
246 | - foreach($this->space->getFormat() as $index => $info) { |
|
247 | - if(!property_exists($instance, $info['name'])) { |
|
246 | + foreach ($this->space->getFormat() as $index => $info) { |
|
247 | + if (!property_exists($instance, $info['name'])) { |
|
248 | 248 | $skipped++; |
249 | 249 | $instance->{$info['name']} = null; |
250 | 250 | } |
@@ -253,7 +253,7 @@ discard block |
||
253 | 253 | ->formatValue($info['type'], $instance->{$info['name']}); |
254 | 254 | $tuple[$index] = $instance->{$info['name']}; |
255 | 255 | |
256 | - if(count($tuple) == $size + $skipped) { |
|
256 | + if (count($tuple) == $size + $skipped) { |
|
257 | 257 | break; |
258 | 258 | } |
259 | 259 | } |
@@ -261,24 +261,24 @@ discard block |
||
261 | 261 | $key = $this->space->getInstanceKey($instance); |
262 | 262 | $client = $this->space->getMapper()->getClient(); |
263 | 263 | |
264 | - if(array_key_exists($key, $this->persisted)) { |
|
264 | + if (array_key_exists($key, $this->persisted)) { |
|
265 | 265 | // update |
266 | 266 | $update = array_diff_assoc($tuple, $this->original[$key]); |
267 | - if(!count($update)) { |
|
267 | + if (!count($update)) { |
|
268 | 268 | return $instance; |
269 | 269 | } |
270 | 270 | |
271 | 271 | $operations = []; |
272 | - foreach($update as $index => $value) { |
|
272 | + foreach ($update as $index => $value) { |
|
273 | 273 | $operations[] = ['=', $index, $value]; |
274 | 274 | } |
275 | 275 | |
276 | 276 | $pk = []; |
277 | - foreach($this->space->getPrimaryIndex()->parts as $part) { |
|
277 | + foreach ($this->space->getPrimaryIndex()->parts as $part) { |
|
278 | 278 | $pk[] = $this->original[$key][$part[0]]; |
279 | 279 | } |
280 | 280 | |
281 | - foreach($this->space->getMapper()->getPlugins() as $plugin) { |
|
281 | + foreach ($this->space->getMapper()->getPlugins() as $plugin) { |
|
282 | 282 | $plugin->beforeUpdate($instance, $this->space); |
283 | 283 | } |
284 | 284 | |
@@ -287,7 +287,7 @@ discard block |
||
287 | 287 | |
288 | 288 | } else { |
289 | 289 | |
290 | - foreach($this->space->getMapper()->getPlugins() as $plugin) { |
|
290 | + foreach ($this->space->getMapper()->getPlugins() as $plugin) { |
|
291 | 291 | $plugin->beforeCreate($instance, $this->space); |
292 | 292 | } |
293 | 293 |