Completed
Push — master ( cf4ab6...f5b12b )
by Dmitry
03:04
created
src/Repository.php 1 patch
Spacing   +55 added lines, -55 removed lines patch added patch discarded remove patch
@@ -24,20 +24,20 @@  discard block
 block discarded – undo
24 24
     {
25 25
         $data = (array) $data;
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
 
37
-        if(array_key_exists(0, $data)) {
37
+        if (array_key_exists(0, $data)) {
38 38
             $byType = [];
39
-            foreach($this->space->getFormat() as $row) {
40
-                if(!array_key_exists($row['type'], $byType)) {
39
+            foreach ($this->space->getFormat() as $row) {
40
+                if (!array_key_exists($row['type'], $byType)) {
41 41
                     $byType[$row['type']] = [$row['name']];
42 42
                 } else {
43 43
                     $byType[$row['type']][] = $row['name'];
@@ -48,10 +48,10 @@  discard block
 block discarded – undo
48 48
                 'is_string' => 'str',
49 49
                 'is_array' => '*',
50 50
             ];
51
-            foreach($data as $k => $v) {
52
-                foreach($mapping as $function => $type) {
53
-                    if(call_user_func($function, $v)) {
54
-                        if(count($byType[$type]) == 1) {
51
+            foreach ($data as $k => $v) {
52
+                foreach ($mapping as $function => $type) {
53
+                    if (call_user_func($function, $v)) {
54
+                        if (count($byType[$type]) == 1) {
55 55
                             $data[$byType[$type][0]] = $v;
56 56
                             unset($data[$k]);
57 57
                         }
@@ -61,16 +61,16 @@  discard block
 block discarded – undo
61 61
         }
62 62
 
63 63
         $instance = new $class($this);
64
-        foreach($this->space->getFormat() as $row) {
65
-            if(array_key_exists($row['name'], $data)) {
64
+        foreach ($this->space->getFormat() as $row) {
65
+            if (array_key_exists($row['name'], $data)) {
66 66
                 $instance->{$row['name']} = $data[$row['name']];
67
-                if($data[$row['name']] instanceof Entity) {
67
+                if ($data[$row['name']] instanceof Entity) {
68 68
                     $instance->{$row['name']} = $instance->{$row['name']}->id;
69 69
                 }
70 70
             }
71 71
         }
72 72
 
73
-        foreach($this->space->getMapper()->getPlugins() as $plugin) {
73
+        foreach ($this->space->getMapper()->getPlugins() as $plugin) {
74 74
             $plugin->generateKey($instance, $this->space);
75 75
         }
76 76
 
@@ -90,18 +90,18 @@  discard block
 block discarded – undo
90 90
     {
91 91
         $cacheKey = json_encode(func_get_args());
92 92
 
93
-        if(array_key_exists($cacheKey, $this->results)) {
93
+        if (array_key_exists($cacheKey, $this->results)) {
94 94
             return $this->results[$cacheKey];
95 95
         }
96 96
 
97
-        if(!is_array($params)) {
97
+        if (!is_array($params)) {
98 98
             $params = [$params];
99 99
         }
100
-        if(count($params) == 1 && array_key_exists(0, $params)) {
100
+        if (count($params) == 1 && array_key_exists(0, $params)) {
101 101
             $primary = $this->space->getPrimaryIndex();
102
-            if(count($primary->parts) == 1) {
102
+            if (count($primary->parts) == 1) {
103 103
                 $formatted = $this->space->getMapper()->getSchema()->formatValue($primary->parts[0][1], $params[0]);
104
-                if($params[0] == $formatted) {
104
+                if ($params[0] == $formatted) {
105 105
                     $params = [
106 106
                         $this->space->getFormat()[$primary->parts[0][0]]['name'] => $params[0]
107 107
                     ];
@@ -109,8 +109,8 @@  discard block
 block discarded – undo
109 109
             }
110 110
         }
111 111
 
112
-        if(array_key_exists('id', $params)) {
113
-            if(array_key_exists($params['id'], $this->persisted)) {
112
+        if (array_key_exists('id', $params)) {
113
+            if (array_key_exists($params['id'], $this->persisted)) {
114 114
                 $instance = $this->persisted[$params['id']];
115 115
                 return $one ? $instance : [$instance];
116 116
             }
@@ -118,7 +118,7 @@  discard block
 block discarded – undo
118 118
 
119 119
 
120 120
         $index = $this->space->castIndex($params);
121
-        if(is_null($index)) {
121
+        if (is_null($index)) {
122 122
             throw new Exception("No index for params ".json_encode($params));
123 123
         }
124 124
 
@@ -128,15 +128,15 @@  discard block
 block discarded – undo
128 128
         $data = $client->getSpace($this->space->getId())->select($values, $index)->getData();
129 129
 
130 130
         $result = [];
131
-        foreach($data as $tuple) {
131
+        foreach ($data as $tuple) {
132 132
             $instance = $this->getInstance($tuple);
133
-            if($one) {
133
+            if ($one) {
134 134
                 return $this->results[$cacheKey] = $instance;
135 135
             }
136 136
             $result[] = $instance;
137 137
         }
138 138
 
139
-        if($one) {
139
+        if ($one) {
140 140
             return $this->results[$cacheKey] = null;
141 141
         }
142 142
 
@@ -147,15 +147,15 @@  discard block
 block discarded – undo
147 147
     {
148 148
         $key = $this->space->getTupleKey($tuple);
149 149
 
150
-        if(array_key_exists($key, $this->persisted)) {
150
+        if (array_key_exists($key, $this->persisted)) {
151 151
             return $this->persisted[$key];
152 152
         }
153 153
 
154 154
         $class = Entity::class;
155
-        foreach($this->space->getMapper()->getPlugins() as $plugin) {
155
+        foreach ($this->space->getMapper()->getPlugins() as $plugin) {
156 156
             $entityClass = $plugin->getEntityClass($this->space);
157
-            if($entityClass) {
158
-                if($class != Entity::class) {
157
+            if ($entityClass) {
158
+                if ($class != Entity::class) {
159 159
                     throw new Exception('Entity class override');
160 160
                 }
161 161
                 $class = $entityClass;
@@ -165,7 +165,7 @@  discard block
 block discarded – undo
165 165
 
166 166
         $this->original[$key] = $tuple;
167 167
 
168
-        foreach($this->space->getFormat() as $index => $info) {
168
+        foreach ($this->space->getFormat() as $index => $info) {
169 169
             $instance->{$info['name']} = array_key_exists($index, $tuple) ? $tuple[$index] : null;
170 170
         }
171 171
 
@@ -181,26 +181,26 @@  discard block
 block discarded – undo
181 181
 
182 182
     public function update(Entity $instance, $operations)
183 183
     {
184
-        if(!count($operations)) {
184
+        if (!count($operations)) {
185 185
             return;
186 186
         }
187 187
 
188 188
         $tupleOperations = [];
189
-        foreach($operations as $operation) {
189
+        foreach ($operations as $operation) {
190 190
             $tupleIndex = $this->space->getPropertyIndex($operation[1]);
191 191
             $tupleOperations[] = [$operation[0], $tupleIndex, $operation[2]];
192 192
         }
193 193
 
194 194
         $pk = [];
195
-        foreach($this->space->getPrimaryIndex()->parts as $part) {
195
+        foreach ($this->space->getPrimaryIndex()->parts as $part) {
196 196
             $pk[] = $instance->{$this->space->getFormat()[$part[0]]['name']};
197 197
         }
198 198
 
199 199
         $client = $this->space->getMapper()->getClient();
200 200
         $result = $client->getSpace($this->space->getId())->update($pk, $tupleOperations);
201
-        foreach($result->getData() as $tuple) {
202
-            foreach($this->space->getFormat() as $index => $info) {
203
-                if(array_key_exists($index, $tuple)) {
201
+        foreach ($result->getData() as $tuple) {
202
+            foreach ($this->space->getFormat() as $index => $info) {
203
+                if (array_key_exists($index, $tuple)) {
204 204
                     $instance->{$info['name']} = $tuple[$index];
205 205
                 }
206 206
             }
@@ -216,15 +216,15 @@  discard block
 block discarded – undo
216 216
 
217 217
     public function remove($params = [])
218 218
     {
219
-        if($params instanceof Entity) {
219
+        if ($params instanceof Entity) {
220 220
             return $this->removeEntity($params);
221 221
         }
222 222
 
223
-        if(!count($params)) {
223
+        if (!count($params)) {
224 224
             throw new Exception("Use truncate to flush space");
225 225
         }
226 226
 
227
-        foreach($this->find($params) as $entity) {
227
+        foreach ($this->find($params) as $entity) {
228 228
             $this->removeEntity($entity);
229 229
         }
230 230
     }
@@ -233,19 +233,19 @@  discard block
 block discarded – undo
233 233
     {
234 234
         $key = $this->space->getInstanceKey($instance);
235 235
 
236
-        if(!array_key_exists($key, $this->original)) {
236
+        if (!array_key_exists($key, $this->original)) {
237 237
             return;
238 238
         }
239 239
 
240
-        if(array_key_exists($key, $this->persisted)) {
240
+        if (array_key_exists($key, $this->persisted)) {
241 241
 
242 242
             unset($this->persisted[$key]);
243 243
 
244 244
             $pk = [];
245
-            foreach($this->space->getPrimaryIndex()->parts as $part) {
245
+            foreach ($this->space->getPrimaryIndex()->parts as $part) {
246 246
                 $pk[] = $this->original[$key][$part[0]];
247 247
             }
248
-            foreach($this->space->getMapper()->getPlugins() as $plugin) {
248
+            foreach ($this->space->getMapper()->getPlugins() as $plugin) {
249 249
                 $plugin->beforeRemove($instance, $this->space);
250 250
             }
251 251
 
@@ -266,10 +266,10 @@  discard block
 block discarded – undo
266 266
         $format = $this->space->getFormat();
267 267
 
268 268
         // complete indexes fields
269
-        foreach($this->space->getIndexes() as $index) {
270
-            foreach($index->parts as $part) {
269
+        foreach ($this->space->getIndexes() as $index) {
270
+            foreach ($index->parts as $part) {
271 271
                 $name = $format[$part[0]]['name'];
272
-                if(!property_exists($instance, $name)) {
272
+                if (!property_exists($instance, $name)) {
273 273
                     $instance->{$name} = null;
274 274
                 }
275 275
             }
@@ -278,8 +278,8 @@  discard block
 block discarded – undo
278 278
         $size = count(get_object_vars($instance));
279 279
         $skipped = 0;
280 280
 
281
-        foreach($format as $index => $info) {
282
-            if(!property_exists($instance, $info['name'])) {
281
+        foreach ($format as $index => $info) {
282
+            if (!property_exists($instance, $info['name'])) {
283 283
                 $skipped++;
284 284
                 $instance->{$info['name']} = null;
285 285
             }
@@ -288,7 +288,7 @@  discard block
 block discarded – undo
288 288
                 ->formatValue($info['type'], $instance->{$info['name']});
289 289
             $tuple[$index] = $instance->{$info['name']};
290 290
 
291
-            if(count($tuple) == $size + $skipped) {
291
+            if (count($tuple) == $size + $skipped) {
292 292
                 break;
293 293
             }
294 294
         }
@@ -296,24 +296,24 @@  discard block
 block discarded – undo
296 296
         $key = $this->space->getInstanceKey($instance);
297 297
         $client = $this->space->getMapper()->getClient();
298 298
 
299
-        if(array_key_exists($key, $this->persisted)) {
299
+        if (array_key_exists($key, $this->persisted)) {
300 300
             // update
301 301
             $update = array_diff_assoc($tuple, $this->original[$key]);
302
-            if(!count($update)) {
302
+            if (!count($update)) {
303 303
                 return $instance;
304 304
             }
305 305
 
306 306
             $operations = [];
307
-            foreach($update as $index => $value) {
307
+            foreach ($update as $index => $value) {
308 308
                 $operations[] = ['=', $index, $value];
309 309
             }
310 310
 
311 311
             $pk = [];
312
-            foreach($this->space->getPrimaryIndex()->parts as $part) {
312
+            foreach ($this->space->getPrimaryIndex()->parts as $part) {
313 313
                 $pk[] = $this->original[$key][$part[0]];
314 314
             }
315 315
 
316
-            foreach($this->space->getMapper()->getPlugins() as $plugin) {
316
+            foreach ($this->space->getMapper()->getPlugins() as $plugin) {
317 317
                 $plugin->beforeUpdate($instance, $this->space);
318 318
             }
319 319
 
@@ -322,7 +322,7 @@  discard block
 block discarded – undo
322 322
 
323 323
         } else {
324 324
 
325
-            foreach($this->space->getMapper()->getPlugins() as $plugin) {
325
+            foreach ($this->space->getMapper()->getPlugins() as $plugin) {
326 326
                 $plugin->beforeCreate($instance, $this->space);
327 327
             }
328 328
 
Please login to merge, or discard this patch.