@@ -28,6 +28,9 @@ discard block |
||
28 | 28 | return $plugin; |
29 | 29 | } |
30 | 30 | |
31 | + /** |
|
32 | + * @param string $space |
|
33 | + */ |
|
31 | 34 | public function create($space, $data) |
32 | 35 | { |
33 | 36 | $instance = $this->getRepository($space)->create($data); |
@@ -35,11 +38,17 @@ discard block |
||
35 | 38 | return $instance; |
36 | 39 | } |
37 | 40 | |
41 | + /** |
|
42 | + * @param string $space |
|
43 | + */ |
|
38 | 44 | public function findOne($space, $params = []) |
39 | 45 | { |
40 | 46 | return $this->getRepository($space)->findOne($params); |
41 | 47 | } |
42 | 48 | |
49 | + /** |
|
50 | + * @param string $space |
|
51 | + */ |
|
43 | 52 | public function find($space, $params = []) |
44 | 53 | { |
45 | 54 | return $this->getRepository($space)->find($params); |
@@ -18,8 +18,8 @@ discard block |
||
18 | 18 | |
19 | 19 | public function addPlugin($class) |
20 | 20 | { |
21 | - if(!is_subclass_of($class, Plugin::class)) { |
|
22 | - throw new Exception("Plugin should extend " . Plugin::class . " class"); |
|
21 | + if (!is_subclass_of($class, Plugin::class)) { |
|
22 | + throw new Exception("Plugin should extend ".Plugin::class." class"); |
|
23 | 23 | } |
24 | 24 | |
25 | 25 | $plugin = new $class($this); |
@@ -47,8 +47,8 @@ discard block |
||
47 | 47 | |
48 | 48 | public function findRepository(Entity $instance) |
49 | 49 | { |
50 | - foreach($this->getSchema()->getSpaces() as $space) { |
|
51 | - if($space->getRepository()->knows($instance)) { |
|
50 | + foreach ($this->getSchema()->getSpaces() as $space) { |
|
51 | + if ($space->getRepository()->knows($instance)) { |
|
52 | 52 | return $space->getRepository(); |
53 | 53 | } |
54 | 54 | } |
@@ -19,6 +19,9 @@ |
||
19 | 19 | } |
20 | 20 | } |
21 | 21 | |
22 | + /** |
|
23 | + * @param Space $space |
|
24 | + */ |
|
22 | 25 | private function generateValue($space) |
23 | 26 | { |
24 | 27 | $spaceId = $space->getId(); |
@@ -11,9 +11,9 @@ discard block |
||
11 | 11 | public function beforeCreate(Entity $instance, Space $space) |
12 | 12 | { |
13 | 13 | $primary = $space->getPrimaryIndex(); |
14 | - if(count($primary->parts) == 1) { |
|
14 | + if (count($primary->parts) == 1) { |
|
15 | 15 | $key = $space->getFormat()[$primary->parts[0][0]]['name']; |
16 | - if(!property_exists($instance, $key)) { |
|
16 | + if (!property_exists($instance, $key)) { |
|
17 | 17 | $instance->$key = $this->generateValue($space); |
18 | 18 | } |
19 | 19 | } |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | { |
24 | 24 | $spaceId = $space->getId(); |
25 | 25 | |
26 | - if(!$this->mapper->getSchema()->hasSpace('sequence')) { |
|
26 | + if (!$this->mapper->getSchema()->hasSpace('sequence')) { |
|
27 | 27 | |
28 | 28 | $sequence = $this->mapper->getSchema()->createSpace('sequence'); |
29 | 29 | $sequence->addProperty('space', 'unsigned'); |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | } |
39 | 39 | |
40 | 40 | $entity = $this->mapper->findOne('sequence', $space->getId()); |
41 | - if(!$entity) { |
|
41 | + if (!$entity) { |
|
42 | 42 | $entity = $this->mapper->create('sequence', [ |
43 | 43 | 'space' => $space->getId(), |
44 | 44 | 'counter' => 0, |
@@ -15,14 +15,14 @@ discard block |
||
15 | 15 | |
16 | 16 | public function getRepositoryClass(Space $space) |
17 | 17 | { |
18 | - if(array_key_exists($space->getName(), $this->repositories)) { |
|
18 | + if (array_key_exists($space->getName(), $this->repositories)) { |
|
19 | 19 | return $this->repositories[$space->getName()]; |
20 | 20 | } |
21 | 21 | } |
22 | 22 | |
23 | 23 | public function getEntityClass(Space $space) |
24 | 24 | { |
25 | - if(array_key_exists($space->getName(), $this->entities)) { |
|
25 | + if (array_key_exists($space->getName(), $this->entities)) { |
|
26 | 26 | return $this->entities[$space->getName()]; |
27 | 27 | } |
28 | 28 | } |
@@ -31,12 +31,12 @@ discard block |
||
31 | 31 | { |
32 | 32 | $this->validateSpace($space); |
33 | 33 | |
34 | - if(!class_exists($class)) { |
|
34 | + if (!class_exists($class)) { |
|
35 | 35 | throw new Exception("No class $class"); |
36 | 36 | } |
37 | 37 | |
38 | - if(!is_subclass_of($class, Entity::class)) { |
|
39 | - throw new Exception("Entity should extend " . Entity::class . " class"); |
|
38 | + if (!is_subclass_of($class, Entity::class)) { |
|
39 | + throw new Exception("Entity should extend ".Entity::class." class"); |
|
40 | 40 | } |
41 | 41 | |
42 | 42 | $this->entities[$space] = $class; |
@@ -46,12 +46,12 @@ discard block |
||
46 | 46 | { |
47 | 47 | $this->validateSpace($space); |
48 | 48 | |
49 | - if(!class_exists($class)) { |
|
49 | + if (!class_exists($class)) { |
|
50 | 50 | throw new Exception("No class $class"); |
51 | 51 | } |
52 | 52 | |
53 | - if(!is_subclass_of($class, Repository::class)) { |
|
54 | - throw new Exception("Repository should extend " . Repository::class . " class"); |
|
53 | + if (!is_subclass_of($class, Repository::class)) { |
|
54 | + throw new Exception("Repository should extend ".Repository::class." class"); |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | $this->repositories[$space] = $class; |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | |
60 | 60 | public function validateSpace($space) |
61 | 61 | { |
62 | - if(!$this->mapper->getSchema()->hasSpace($space)) { |
|
62 | + if (!$this->mapper->getSchema()->hasSpace($space)) { |
|
63 | 63 | throw new Exception("No space $space"); |
64 | 64 | } |
65 | 65 | } |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | |
32 | 32 | public function formatValue($type, $value) |
33 | 33 | { |
34 | - switch($type) { |
|
34 | + switch ($type) { |
|
35 | 35 | case 'str': return (string) $value; |
36 | 36 | case 'unsigned': return (int) $value; |
37 | 37 | default: return $value; |
@@ -40,15 +40,15 @@ discard block |
||
40 | 40 | |
41 | 41 | public function getSpace($id) |
42 | 42 | { |
43 | - if(is_string($id)) { |
|
43 | + if (is_string($id)) { |
|
44 | 44 | return $this->getSpace($this->getSpaceId($id)); |
45 | 45 | } |
46 | 46 | |
47 | - if(!$id) { |
|
47 | + if (!$id) { |
|
48 | 48 | throw new Exception("Space id or name not defined"); |
49 | 49 | } |
50 | 50 | |
51 | - if(!array_key_exists($id, $this->spaces)) { |
|
51 | + if (!array_key_exists($id, $this->spaces)) { |
|
52 | 52 | $this->spaces[$id] = new Space($this->mapper, $id, array_search($id, $this->names)); |
53 | 53 | } |
54 | 54 | return $this->spaces[$id]; |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | |
57 | 57 | public function getSpaceId($name) |
58 | 58 | { |
59 | - if(!$this->hasSpace($name)) { |
|
59 | + if (!$this->hasSpace($name)) { |
|
60 | 60 | throw new Exception("No space $id"); |
61 | 61 | } |
62 | 62 | return $this->names[$name]; |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | |
65 | 65 | public function getSpaces() |
66 | 66 | { |
67 | - foreach($this->names as $id) { |
|
67 | + foreach ($this->names as $id) { |
|
68 | 68 | $this->getSpace($id); |
69 | 69 | } |
70 | 70 | return $this->spaces; |
@@ -77,10 +77,10 @@ discard block |
||
77 | 77 | |
78 | 78 | public function once($name, $callback) |
79 | 79 | { |
80 | - $key = 'once' . $name; |
|
80 | + $key = 'once'.$name; |
|
81 | 81 | |
82 | 82 | $rows = $this->mapper->find('_schema', ['key' => $key]); |
83 | - if(!count($rows)) { |
|
83 | + if (!count($rows)) { |
|
84 | 84 | $this->mapper->create('_schema', ['key' => $key]); |
85 | 85 | return $callback($this->mapper); |
86 | 86 | } |
@@ -28,8 +28,8 @@ discard block |
||
28 | 28 | public function addProperty($name, $type) |
29 | 29 | { |
30 | 30 | $format = $this->getFormat(); |
31 | - foreach($format as $field) { |
|
32 | - if($field['name'] == $name) { |
|
31 | + foreach ($format as $field) { |
|
32 | + if ($field['name'] == $name) { |
|
33 | 33 | throw new Exception("Property $name exists"); |
34 | 34 | } |
35 | 35 | } |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | { |
45 | 45 | $format = $this->getFormat(); |
46 | 46 | $last = array_pop($format); |
47 | - if($last['name'] != $name) { |
|
47 | + if ($last['name'] != $name) { |
|
48 | 48 | throw new Exception("Remove only last property"); |
49 | 49 | } |
50 | 50 | $this->mapper->getClient()->evaluate("box.space[$this->id]:format(...)", [$format]); |
@@ -63,13 +63,13 @@ discard block |
||
63 | 63 | public function createIndex($config) |
64 | 64 | { |
65 | 65 | |
66 | - if(!is_array($config)) { |
|
66 | + if (!is_array($config)) { |
|
67 | 67 | $config = ['fields' => $config]; |
68 | 68 | } |
69 | 69 | |
70 | 70 | |
71 | - if(!array_key_exists('fields', $config)) { |
|
72 | - if(array_values($config) != $config) { |
|
71 | + if (!array_key_exists('fields', $config)) { |
|
72 | + if (array_values($config) != $config) { |
|
73 | 73 | throw new Exception("Invalid index configuration"); |
74 | 74 | } |
75 | 75 | $config = [ |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | ]; |
78 | 78 | } |
79 | 79 | |
80 | - if(!is_array($config['fields'])) { |
|
80 | + if (!is_array($config['fields'])) { |
|
81 | 81 | $config['fields'] = [$config['fields']]; |
82 | 82 | } |
83 | 83 | |
@@ -85,17 +85,17 @@ discard block |
||
85 | 85 | 'parts' => [] |
86 | 86 | ]; |
87 | 87 | |
88 | - foreach($config as $k => $v) { |
|
89 | - if($k != 'name' && $k != 'fields') { |
|
88 | + foreach ($config as $k => $v) { |
|
89 | + if ($k != 'name' && $k != 'fields') { |
|
90 | 90 | $options[$k] = $v; |
91 | 91 | } |
92 | 92 | } |
93 | 93 | |
94 | - foreach($config['fields'] as $property) { |
|
95 | - if(!$this->getPropertyType($property)) { |
|
94 | + foreach ($config['fields'] as $property) { |
|
95 | + if (!$this->getPropertyType($property)) { |
|
96 | 96 | throw new Exception("Unknown property $property", 1); |
97 | 97 | } |
98 | - $options['parts'][] = $this->getPropertyIndex($property)+1; |
|
98 | + $options['parts'][] = $this->getPropertyIndex($property) + 1; |
|
99 | 99 | $options['parts'][] = $this->getPropertyType($property); |
100 | 100 | } |
101 | 101 | |
@@ -118,15 +118,15 @@ discard block |
||
118 | 118 | |
119 | 119 | public function getFormat() |
120 | 120 | { |
121 | - if(!$this->format) { |
|
122 | - if($this->isSpecial()) { |
|
121 | + if (!$this->format) { |
|
122 | + if ($this->isSpecial()) { |
|
123 | 123 | $this->format = $this->mapper->getClient() |
124 | 124 | ->getSpace(280)->select([$this->id])->getData()[0][6]; |
125 | 125 | |
126 | 126 | } else { |
127 | 127 | $this->format = $this->mapper->findOne('_space', ['id' => $this->id])->format; |
128 | 128 | } |
129 | - if(!$this->format) { |
|
129 | + if (!$this->format) { |
|
130 | 130 | $this->format = []; |
131 | 131 | } |
132 | 132 | $this->parseFormat(); |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | { |
150 | 150 | $this->formatTypesHash = []; |
151 | 151 | $this->formatNamesHash = []; |
152 | - foreach($this->format as $key => $row) { |
|
152 | + foreach ($this->format as $key => $row) { |
|
153 | 153 | $this->formatTypesHash[$row['name']] = $row['type']; |
154 | 154 | $this->formatNamesHash[$row['name']] = $key; |
155 | 155 | } |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | |
164 | 164 | public function getPropertyType($name) |
165 | 165 | { |
166 | - if(!$this->hasProperty($name)) { |
|
166 | + if (!$this->hasProperty($name)) { |
|
167 | 167 | throw new Exception("No property $name"); |
168 | 168 | } |
169 | 169 | return $this->formatTypesHash[$name]; |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | |
172 | 172 | public function getPropertyIndex($name) |
173 | 173 | { |
174 | - if(!$this->hasProperty($name)) { |
|
174 | + if (!$this->hasProperty($name)) { |
|
175 | 175 | throw new Exception("No property $name"); |
176 | 176 | } |
177 | 177 | return $this->formatNamesHash[$name]; |
@@ -179,14 +179,14 @@ discard block |
||
179 | 179 | |
180 | 180 | public function getIndexes() |
181 | 181 | { |
182 | - if(!$this->indexes) { |
|
183 | - if($this->isSpecial()) { |
|
182 | + if (!$this->indexes) { |
|
183 | + if ($this->isSpecial()) { |
|
184 | 184 | $this->indexes = []; |
185 | 185 | $indexTuples = $this->mapper->getClient()->getSpace(288)->select([$this->id])->getData(); |
186 | 186 | $indexFormat = $this->mapper->getSchema()->getSpace(288)->getFormat(); |
187 | - foreach($indexTuples as $tuple) { |
|
187 | + foreach ($indexTuples as $tuple) { |
|
188 | 188 | $instance = (object) []; |
189 | - foreach($indexFormat as $index => $format) { |
|
189 | + foreach ($indexFormat as $index => $format) { |
|
190 | 190 | $instance->{$format['name']} = $tuple[$index]; |
191 | 191 | } |
192 | 192 | $this->indexes[] = $instance; |
@@ -204,44 +204,44 @@ discard block |
||
204 | 204 | $keys = array_keys($params); |
205 | 205 | |
206 | 206 | $keys = []; |
207 | - foreach($params as $name => $value) { |
|
207 | + foreach ($params as $name => $value) { |
|
208 | 208 | $keys[] = $this->getPropertyIndex($name); |
209 | 209 | } |
210 | - if($keys == [0]) { |
|
210 | + if ($keys == [0]) { |
|
211 | 211 | // primary index |
212 | 212 | return 0; |
213 | 213 | } |
214 | 214 | |
215 | 215 | // equals |
216 | - foreach($this->getIndexes() as $index) { |
|
216 | + foreach ($this->getIndexes() as $index) { |
|
217 | 217 | $equals = false; |
218 | - if(count($keys) == count($index->parts)) { |
|
218 | + if (count($keys) == count($index->parts)) { |
|
219 | 219 | // same length |
220 | 220 | $equals = true; |
221 | - foreach($index->parts as $part) { |
|
221 | + foreach ($index->parts as $part) { |
|
222 | 222 | $equals = $equals && in_array($part[0], $keys); |
223 | 223 | } |
224 | 224 | } |
225 | 225 | |
226 | - if($equals) { |
|
226 | + if ($equals) { |
|
227 | 227 | return $index->iid; |
228 | 228 | } |
229 | 229 | } |
230 | 230 | |
231 | 231 | // index part |
232 | - foreach($this->getIndexes() as $index) { |
|
232 | + foreach ($this->getIndexes() as $index) { |
|
233 | 233 | $partial = []; |
234 | - foreach($index->parts as $n => $part) { |
|
235 | - if(!array_key_exists($n, $keys)) { |
|
234 | + foreach ($index->parts as $n => $part) { |
|
235 | + if (!array_key_exists($n, $keys)) { |
|
236 | 236 | break; |
237 | 237 | } |
238 | - if($keys[$n] != $part[0]) { |
|
238 | + if ($keys[$n] != $part[0]) { |
|
239 | 239 | break; |
240 | 240 | } |
241 | 241 | $partial[] = $keys[$n]; |
242 | 242 | } |
243 | 243 | |
244 | - if(count($partial) == count($keys)) { |
|
244 | + if (count($partial) == count($keys)) { |
|
245 | 245 | return $index->iid; |
246 | 246 | } |
247 | 247 | } |
@@ -255,9 +255,9 @@ discard block |
||
255 | 255 | $format = $this->getFormat(); |
256 | 256 | |
257 | 257 | $values = []; |
258 | - foreach($index->parts as $part) { |
|
258 | + foreach ($index->parts as $part) { |
|
259 | 259 | $name = $format[$part[0]]['name']; |
260 | - if(!array_key_exists($name, $params)) { |
|
260 | + if (!array_key_exists($name, $params)) { |
|
261 | 261 | break; |
262 | 262 | } |
263 | 263 | $values[] = $this->mapper->getSchema()->formatValue($part[1], $params[$name]); |
@@ -268,7 +268,7 @@ discard block |
||
268 | 268 | public function getPrimaryIndex() |
269 | 269 | { |
270 | 270 | $indexes = $this->getIndexes(); |
271 | - if(!count($indexes)) { |
|
271 | + if (!count($indexes)) { |
|
272 | 272 | throw new Exception("No primary index"); |
273 | 273 | } |
274 | 274 | return $indexes[0]; |
@@ -277,7 +277,7 @@ discard block |
||
277 | 277 | public function getTupleKey($tuple) |
278 | 278 | { |
279 | 279 | $key = []; |
280 | - foreach($this->getPrimaryIndex()->parts as $part) { |
|
280 | + foreach ($this->getPrimaryIndex()->parts as $part) { |
|
281 | 281 | $key[] = $tuple[$part[0]]; |
282 | 282 | } |
283 | 283 | return count($key) == 1 ? $key[0] : implode(':', $key); |
@@ -288,9 +288,9 @@ discard block |
||
288 | 288 | |
289 | 289 | $key = []; |
290 | 290 | |
291 | - foreach($this->getPrimaryIndex()->parts as $part) { |
|
291 | + foreach ($this->getPrimaryIndex()->parts as $part) { |
|
292 | 292 | $name = $this->getFormat()[$part[0]]['name']; |
293 | - if(!property_exists($instance, $name)) { |
|
293 | + if (!property_exists($instance, $name)) { |
|
294 | 294 | throw new Exception("Field $name is undefined", 1); |
295 | 295 | } |
296 | 296 | $key[] = $instance->$name; |
@@ -302,10 +302,10 @@ discard block |
||
302 | 302 | public function getRepository() |
303 | 303 | { |
304 | 304 | $class = Repository::class; |
305 | - foreach($this->mapper->getPlugins() as $plugin) { |
|
305 | + foreach ($this->mapper->getPlugins() as $plugin) { |
|
306 | 306 | $repositoryClass = $plugin->getRepositoryClass($this); |
307 | - if($repositoryClass) { |
|
308 | - if($class != Repository::class) { |
|
307 | + if ($repositoryClass) { |
|
308 | + if ($class != Repository::class) { |
|
309 | 309 | throw new Exception('Repository class override'); |
310 | 310 | } |
311 | 311 | $class = $repositoryClass; |
@@ -24,23 +24,23 @@ 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(); |
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 | 40 | } |
41 | 41 | } |
42 | 42 | |
43 | - foreach($this->space->getMapper()->getPlugins() as $plugin) { |
|
43 | + foreach ($this->space->getMapper()->getPlugins() as $plugin) { |
|
44 | 44 | $plugin->beforeCreate($instance, $this->space); |
45 | 45 | } |
46 | 46 | |
@@ -59,18 +59,18 @@ discard block |
||
59 | 59 | public function find($params = [], $one = false) |
60 | 60 | { |
61 | 61 | $cacheIndex = array_search([$params, $one], $this->cache); |
62 | - if($cacheIndex !== false) { |
|
62 | + if ($cacheIndex !== false) { |
|
63 | 63 | return $this->results[$cacheIndex]; |
64 | 64 | } |
65 | 65 | |
66 | - if(!is_array($params)) { |
|
66 | + if (!is_array($params)) { |
|
67 | 67 | $params = [$params]; |
68 | 68 | } |
69 | - if(count($params) == 1 && array_key_exists(0, $params)) { |
|
69 | + if (count($params) == 1 && array_key_exists(0, $params)) { |
|
70 | 70 | $primary = $this->space->getPrimaryIndex(); |
71 | - if(count($primary->parts) == 1) { |
|
71 | + if (count($primary->parts) == 1) { |
|
72 | 72 | $formatted = $this->space->getMapper()->getSchema()->formatValue($primary->parts[0][1], $params[0]); |
73 | - if($params[0] == $formatted) { |
|
73 | + if ($params[0] == $formatted) { |
|
74 | 74 | $params = [ |
75 | 75 | $this->space->getFormat()[$primary->parts[0][0]]['name'] => $params[0] |
76 | 76 | ]; |
@@ -78,8 +78,8 @@ discard block |
||
78 | 78 | } |
79 | 79 | } |
80 | 80 | |
81 | - if(array_key_exists('id', $params)) { |
|
82 | - if(array_key_exists($params['id'], $this->persisted)) { |
|
81 | + if (array_key_exists('id', $params)) { |
|
82 | + if (array_key_exists($params['id'], $this->persisted)) { |
|
83 | 83 | $instance = $this->persisted[$params['id']]; |
84 | 84 | return $one ? $instance : [$instance]; |
85 | 85 | } |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | |
88 | 88 | |
89 | 89 | $index = $this->space->castIndex($params); |
90 | - if(is_null($index)) { |
|
90 | + if (is_null($index)) { |
|
91 | 91 | throw new Exception("No index for params ".json_encode($params)); |
92 | 92 | } |
93 | 93 | |
@@ -100,15 +100,15 @@ discard block |
||
100 | 100 | $data = $client->getSpace($this->space->getId())->select($values, $index)->getData(); |
101 | 101 | |
102 | 102 | $result = []; |
103 | - foreach($data as $tuple) { |
|
103 | + foreach ($data as $tuple) { |
|
104 | 104 | $instance = $this->getInstance($tuple); |
105 | - if($one) { |
|
105 | + if ($one) { |
|
106 | 106 | return $this->results[$cacheIndex] = $instance; |
107 | 107 | } |
108 | 108 | $result[] = $instance; |
109 | 109 | } |
110 | 110 | |
111 | - if($one) { |
|
111 | + if ($one) { |
|
112 | 112 | return $this->results[$cacheIndex] = null; |
113 | 113 | } |
114 | 114 | |
@@ -119,15 +119,15 @@ discard block |
||
119 | 119 | { |
120 | 120 | $key = $this->space->getTupleKey($tuple); |
121 | 121 | |
122 | - if(array_key_exists($key, $this->persisted)) { |
|
122 | + if (array_key_exists($key, $this->persisted)) { |
|
123 | 123 | return $this->persisted[$key]; |
124 | 124 | } |
125 | 125 | |
126 | 126 | $class = Entity::class; |
127 | - foreach($this->space->getMapper()->getPlugins() as $plugin) { |
|
127 | + foreach ($this->space->getMapper()->getPlugins() as $plugin) { |
|
128 | 128 | $entityClass = $plugin->getEntityClass($this->space); |
129 | - if($entityClass) { |
|
130 | - if($class != Entity::class) { |
|
129 | + if ($entityClass) { |
|
130 | + if ($class != Entity::class) { |
|
131 | 131 | throw new Exception('Entity class override'); |
132 | 132 | } |
133 | 133 | $class = $entityClass; |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | |
138 | 138 | $this->original[$key] = $tuple; |
139 | 139 | |
140 | - foreach($this->space->getFormat() as $index => $info) { |
|
140 | + foreach ($this->space->getFormat() as $index => $info) { |
|
141 | 141 | $instance->{$info['name']} = array_key_exists($index, $tuple) ? $tuple[$index] : null; |
142 | 142 | } |
143 | 143 | |
@@ -153,26 +153,26 @@ discard block |
||
153 | 153 | |
154 | 154 | public function update(Entity $instance, $operations) |
155 | 155 | { |
156 | - if(!count($operations)) { |
|
156 | + if (!count($operations)) { |
|
157 | 157 | return; |
158 | 158 | } |
159 | 159 | |
160 | 160 | $tupleOperations = []; |
161 | - foreach($operations as $operation) { |
|
161 | + foreach ($operations as $operation) { |
|
162 | 162 | $tupleIndex = $this->space->getPropertyIndex($operation[1]); |
163 | 163 | $tupleOperations[] = [$operation[0], $tupleIndex, $operation[2]]; |
164 | 164 | } |
165 | 165 | |
166 | 166 | $pk = []; |
167 | - foreach($this->space->getPrimaryIndex()->parts as $part) { |
|
167 | + foreach ($this->space->getPrimaryIndex()->parts as $part) { |
|
168 | 168 | $pk[] = $instance->{$this->space->getFormat()[$part[0]]['name']}; |
169 | 169 | } |
170 | 170 | |
171 | 171 | $client = $this->space->getMapper()->getClient(); |
172 | 172 | $result = $client->getSpace($this->space->getId())->update($pk, $tupleOperations); |
173 | - foreach($result->getData() as $tuple) { |
|
174 | - foreach($this->space->getFormat() as $index => $info) { |
|
175 | - if(array_key_exists($index, $tuple)) { |
|
173 | + foreach ($result->getData() as $tuple) { |
|
174 | + foreach ($this->space->getFormat() as $index => $info) { |
|
175 | + if (array_key_exists($index, $tuple)) { |
|
176 | 176 | $instance->{$info['name']} = $tuple[$index]; |
177 | 177 | } |
178 | 178 | } |
@@ -183,16 +183,16 @@ discard block |
||
183 | 183 | { |
184 | 184 | $key = $this->space->getInstanceKey($instance); |
185 | 185 | |
186 | - if(!array_key_exists($key, $this->original)) { |
|
186 | + if (!array_key_exists($key, $this->original)) { |
|
187 | 187 | return; |
188 | 188 | } |
189 | 189 | |
190 | - if(array_key_exists($key, $this->persisted)) { |
|
190 | + if (array_key_exists($key, $this->persisted)) { |
|
191 | 191 | |
192 | 192 | unset($this->persisted[$key]); |
193 | 193 | |
194 | 194 | $pk = []; |
195 | - foreach($this->space->getPrimaryIndex()->parts as $part) { |
|
195 | + foreach ($this->space->getPrimaryIndex()->parts as $part) { |
|
196 | 196 | $pk[] = $this->original[$key][$part[0]]; |
197 | 197 | } |
198 | 198 | |
@@ -213,8 +213,8 @@ discard block |
||
213 | 213 | |
214 | 214 | $size = count(get_object_vars($instance)); |
215 | 215 | |
216 | - foreach($this->space->getFormat() as $index => $info) { |
|
217 | - if(!property_exists($instance, $info['name'])) { |
|
216 | + foreach ($this->space->getFormat() as $index => $info) { |
|
217 | + if (!property_exists($instance, $info['name'])) { |
|
218 | 218 | $instance->{$info['name']} = null; |
219 | 219 | } |
220 | 220 | |
@@ -222,7 +222,7 @@ discard block |
||
222 | 222 | ->formatValue($info['type'], $instance->{$info['name']}); |
223 | 223 | $tuple[$index] = $instance->{$info['name']}; |
224 | 224 | |
225 | - if(count($tuple) == $size) { |
|
225 | + if (count($tuple) == $size) { |
|
226 | 226 | break; |
227 | 227 | } |
228 | 228 | } |
@@ -230,20 +230,20 @@ discard block |
||
230 | 230 | $key = $this->space->getInstanceKey($instance); |
231 | 231 | $client = $this->space->getMapper()->getClient(); |
232 | 232 | |
233 | - if(array_key_exists($key, $this->persisted)) { |
|
233 | + if (array_key_exists($key, $this->persisted)) { |
|
234 | 234 | // update |
235 | 235 | $update = array_diff_assoc($tuple, $this->original[$key]); |
236 | - if(!count($update)) { |
|
236 | + if (!count($update)) { |
|
237 | 237 | return $instance; |
238 | 238 | } |
239 | 239 | |
240 | 240 | $operations = []; |
241 | - foreach($update as $index => $value) { |
|
241 | + foreach ($update as $index => $value) { |
|
242 | 242 | $operations[] = ['=', $index, $value]; |
243 | 243 | } |
244 | 244 | |
245 | 245 | $pk = []; |
246 | - foreach($this->space->getPrimaryIndex()->parts as $part) { |
|
246 | + foreach ($this->space->getPrimaryIndex()->parts as $part) { |
|
247 | 247 | $pk[] = $this->original[$key][$part[0]]; |
248 | 248 | } |
249 | 249 |