@@ -25,20 +25,20 @@ discard block |
||
| 25 | 25 | { |
| 26 | 26 | $data = (array) $data; |
| 27 | 27 | $class = Entity::class; |
| 28 | - foreach($this->space->getMapper()->getPlugins() as $plugin) { |
|
| 28 | + foreach ($this->space->getMapper()->getPlugins() as $plugin) { |
|
| 29 | 29 | $entityClass = $plugin->getEntityClass($this->space); |
| 30 | - if($entityClass) { |
|
| 31 | - if($class != Entity::class) { |
|
| 30 | + if ($entityClass) { |
|
| 31 | + if ($class != Entity::class) { |
|
| 32 | 32 | throw new Exception('Entity class override'); |
| 33 | 33 | } |
| 34 | 34 | $class = $entityClass; |
| 35 | 35 | } |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - if(array_key_exists(0, $data)) { |
|
| 38 | + if (array_key_exists(0, $data)) { |
|
| 39 | 39 | $byType = []; |
| 40 | - foreach($this->space->getFormat() as $row) { |
|
| 41 | - if(!array_key_exists($row['type'], $byType)) { |
|
| 40 | + foreach ($this->space->getFormat() as $row) { |
|
| 41 | + if (!array_key_exists($row['type'], $byType)) { |
|
| 42 | 42 | $byType[$row['type']] = [$row['name']]; |
| 43 | 43 | } else { |
| 44 | 44 | $byType[$row['type']][] = $row['name']; |
@@ -49,10 +49,10 @@ discard block |
||
| 49 | 49 | 'is_string' => 'str', |
| 50 | 50 | 'is_array' => '*', |
| 51 | 51 | ]; |
| 52 | - foreach($data as $k => $v) { |
|
| 53 | - foreach($mapping as $function => $type) { |
|
| 54 | - if(call_user_func($function, $v)) { |
|
| 55 | - if(count($byType[$type]) == 1) { |
|
| 52 | + foreach ($data as $k => $v) { |
|
| 53 | + foreach ($mapping as $function => $type) { |
|
| 54 | + if (call_user_func($function, $v)) { |
|
| 55 | + if (count($byType[$type]) == 1) { |
|
| 56 | 56 | $data[$byType[$type][0]] = $v; |
| 57 | 57 | unset($data[$k]); |
| 58 | 58 | } |
@@ -62,16 +62,16 @@ discard block |
||
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | $instance = new $class($this); |
| 65 | - foreach($this->space->getFormat() as $row) { |
|
| 66 | - if(array_key_exists($row['name'], $data)) { |
|
| 65 | + foreach ($this->space->getFormat() as $row) { |
|
| 66 | + if (array_key_exists($row['name'], $data)) { |
|
| 67 | 67 | $instance->{$row['name']} = $data[$row['name']]; |
| 68 | - if($data[$row['name']] instanceof Entity) { |
|
| 68 | + if ($data[$row['name']] instanceof Entity) { |
|
| 69 | 69 | $instance->{$row['name']} = $instance->{$row['name']}->id; |
| 70 | 70 | } |
| 71 | 71 | } |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - foreach($this->space->getMapper()->getPlugins() as $plugin) { |
|
| 74 | + foreach ($this->space->getMapper()->getPlugins() as $plugin) { |
|
| 75 | 75 | $plugin->generateKey($instance, $this->space); |
| 76 | 76 | } |
| 77 | 77 | |
@@ -90,18 +90,18 @@ discard block |
||
| 90 | 90 | public function find($params = [], $one = false) |
| 91 | 91 | { |
| 92 | 92 | $cacheIndex = array_search([$params, $one], $this->cache); |
| 93 | - if($cacheIndex !== false) { |
|
| 93 | + if ($cacheIndex !== false) { |
|
| 94 | 94 | return $this->results[$cacheIndex]; |
| 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 |
||
| 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 |
||
| 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 | |
@@ -131,15 +131,15 @@ discard block |
||
| 131 | 131 | $data = $client->getSpace($this->space->getId())->select($values, $index)->getData(); |
| 132 | 132 | |
| 133 | 133 | $result = []; |
| 134 | - foreach($data as $tuple) { |
|
| 134 | + foreach ($data as $tuple) { |
|
| 135 | 135 | $instance = $this->getInstance($tuple); |
| 136 | - if($one) { |
|
| 136 | + if ($one) { |
|
| 137 | 137 | return $this->results[$cacheIndex] = $instance; |
| 138 | 138 | } |
| 139 | 139 | $result[] = $instance; |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | - if($one) { |
|
| 142 | + if ($one) { |
|
| 143 | 143 | return $this->results[$cacheIndex] = null; |
| 144 | 144 | } |
| 145 | 145 | |
@@ -150,15 +150,15 @@ discard block |
||
| 150 | 150 | { |
| 151 | 151 | $key = $this->space->getTupleKey($tuple); |
| 152 | 152 | |
| 153 | - if(array_key_exists($key, $this->persisted)) { |
|
| 153 | + if (array_key_exists($key, $this->persisted)) { |
|
| 154 | 154 | return $this->persisted[$key]; |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | $class = Entity::class; |
| 158 | - foreach($this->space->getMapper()->getPlugins() as $plugin) { |
|
| 158 | + foreach ($this->space->getMapper()->getPlugins() as $plugin) { |
|
| 159 | 159 | $entityClass = $plugin->getEntityClass($this->space); |
| 160 | - if($entityClass) { |
|
| 161 | - if($class != Entity::class) { |
|
| 160 | + if ($entityClass) { |
|
| 161 | + if ($class != Entity::class) { |
|
| 162 | 162 | throw new Exception('Entity class override'); |
| 163 | 163 | } |
| 164 | 164 | $class = $entityClass; |
@@ -168,7 +168,7 @@ discard block |
||
| 168 | 168 | |
| 169 | 169 | $this->original[$key] = $tuple; |
| 170 | 170 | |
| 171 | - foreach($this->space->getFormat() as $index => $info) { |
|
| 171 | + foreach ($this->space->getFormat() as $index => $info) { |
|
| 172 | 172 | $instance->{$info['name']} = array_key_exists($index, $tuple) ? $tuple[$index] : null; |
| 173 | 173 | } |
| 174 | 174 | |
@@ -184,26 +184,26 @@ discard block |
||
| 184 | 184 | |
| 185 | 185 | public function update(Entity $instance, $operations) |
| 186 | 186 | { |
| 187 | - if(!count($operations)) { |
|
| 187 | + if (!count($operations)) { |
|
| 188 | 188 | return; |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | $tupleOperations = []; |
| 192 | - foreach($operations as $operation) { |
|
| 192 | + foreach ($operations as $operation) { |
|
| 193 | 193 | $tupleIndex = $this->space->getPropertyIndex($operation[1]); |
| 194 | 194 | $tupleOperations[] = [$operation[0], $tupleIndex, $operation[2]]; |
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | $pk = []; |
| 198 | - foreach($this->space->getPrimaryIndex()->parts as $part) { |
|
| 198 | + foreach ($this->space->getPrimaryIndex()->parts as $part) { |
|
| 199 | 199 | $pk[] = $instance->{$this->space->getFormat()[$part[0]]['name']}; |
| 200 | 200 | } |
| 201 | 201 | |
| 202 | 202 | $client = $this->space->getMapper()->getClient(); |
| 203 | 203 | $result = $client->getSpace($this->space->getId())->update($pk, $tupleOperations); |
| 204 | - foreach($result->getData() as $tuple) { |
|
| 205 | - foreach($this->space->getFormat() as $index => $info) { |
|
| 206 | - if(array_key_exists($index, $tuple)) { |
|
| 204 | + foreach ($result->getData() as $tuple) { |
|
| 205 | + foreach ($this->space->getFormat() as $index => $info) { |
|
| 206 | + if (array_key_exists($index, $tuple)) { |
|
| 207 | 207 | $instance->{$info['name']} = $tuple[$index]; |
| 208 | 208 | } |
| 209 | 209 | } |
@@ -220,15 +220,15 @@ discard block |
||
| 220 | 220 | |
| 221 | 221 | public function remove($params = []) |
| 222 | 222 | { |
| 223 | - if($params instanceof Entity) { |
|
| 223 | + if ($params instanceof Entity) { |
|
| 224 | 224 | return $this->removeEntity($params); |
| 225 | 225 | } |
| 226 | 226 | |
| 227 | - if(!count($params)) { |
|
| 227 | + if (!count($params)) { |
|
| 228 | 228 | throw new Exception("Use truncate to flush space"); |
| 229 | 229 | } |
| 230 | 230 | |
| 231 | - foreach($this->find($params) as $entity) { |
|
| 231 | + foreach ($this->find($params) as $entity) { |
|
| 232 | 232 | $this->removeEntity($entity); |
| 233 | 233 | } |
| 234 | 234 | } |
@@ -237,19 +237,19 @@ discard block |
||
| 237 | 237 | { |
| 238 | 238 | $key = $this->space->getInstanceKey($instance); |
| 239 | 239 | |
| 240 | - if(!array_key_exists($key, $this->original)) { |
|
| 240 | + if (!array_key_exists($key, $this->original)) { |
|
| 241 | 241 | return; |
| 242 | 242 | } |
| 243 | 243 | |
| 244 | - if(array_key_exists($key, $this->persisted)) { |
|
| 244 | + if (array_key_exists($key, $this->persisted)) { |
|
| 245 | 245 | |
| 246 | 246 | unset($this->persisted[$key]); |
| 247 | 247 | |
| 248 | 248 | $pk = []; |
| 249 | - foreach($this->space->getPrimaryIndex()->parts as $part) { |
|
| 249 | + foreach ($this->space->getPrimaryIndex()->parts as $part) { |
|
| 250 | 250 | $pk[] = $this->original[$key][$part[0]]; |
| 251 | 251 | } |
| 252 | - foreach($this->space->getMapper()->getPlugins() as $plugin) { |
|
| 252 | + foreach ($this->space->getMapper()->getPlugins() as $plugin) { |
|
| 253 | 253 | $plugin->beforeRemove($instance, $this->space); |
| 254 | 254 | } |
| 255 | 255 | |
@@ -271,8 +271,8 @@ discard block |
||
| 271 | 271 | $size = count(get_object_vars($instance)); |
| 272 | 272 | $skipped = 0; |
| 273 | 273 | |
| 274 | - foreach($this->space->getFormat() as $index => $info) { |
|
| 275 | - if(!property_exists($instance, $info['name'])) { |
|
| 274 | + foreach ($this->space->getFormat() as $index => $info) { |
|
| 275 | + if (!property_exists($instance, $info['name'])) { |
|
| 276 | 276 | $skipped++; |
| 277 | 277 | $instance->{$info['name']} = null; |
| 278 | 278 | } |
@@ -281,7 +281,7 @@ discard block |
||
| 281 | 281 | ->formatValue($info['type'], $instance->{$info['name']}); |
| 282 | 282 | $tuple[$index] = $instance->{$info['name']}; |
| 283 | 283 | |
| 284 | - if(count($tuple) == $size + $skipped) { |
|
| 284 | + if (count($tuple) == $size + $skipped) { |
|
| 285 | 285 | break; |
| 286 | 286 | } |
| 287 | 287 | } |
@@ -289,24 +289,24 @@ discard block |
||
| 289 | 289 | $key = $this->space->getInstanceKey($instance); |
| 290 | 290 | $client = $this->space->getMapper()->getClient(); |
| 291 | 291 | |
| 292 | - if(array_key_exists($key, $this->persisted)) { |
|
| 292 | + if (array_key_exists($key, $this->persisted)) { |
|
| 293 | 293 | // update |
| 294 | 294 | $update = array_diff_assoc($tuple, $this->original[$key]); |
| 295 | - if(!count($update)) { |
|
| 295 | + if (!count($update)) { |
|
| 296 | 296 | return $instance; |
| 297 | 297 | } |
| 298 | 298 | |
| 299 | 299 | $operations = []; |
| 300 | - foreach($update as $index => $value) { |
|
| 300 | + foreach ($update as $index => $value) { |
|
| 301 | 301 | $operations[] = ['=', $index, $value]; |
| 302 | 302 | } |
| 303 | 303 | |
| 304 | 304 | $pk = []; |
| 305 | - foreach($this->space->getPrimaryIndex()->parts as $part) { |
|
| 305 | + foreach ($this->space->getPrimaryIndex()->parts as $part) { |
|
| 306 | 306 | $pk[] = $this->original[$key][$part[0]]; |
| 307 | 307 | } |
| 308 | 308 | |
| 309 | - foreach($this->space->getMapper()->getPlugins() as $plugin) { |
|
| 309 | + foreach ($this->space->getMapper()->getPlugins() as $plugin) { |
|
| 310 | 310 | $plugin->beforeUpdate($instance, $this->space); |
| 311 | 311 | } |
| 312 | 312 | |
@@ -315,7 +315,7 @@ discard block |
||
| 315 | 315 | |
| 316 | 316 | } else { |
| 317 | 317 | |
| 318 | - foreach($this->space->getMapper()->getPlugins() as $plugin) { |
|
| 318 | + foreach ($this->space->getMapper()->getPlugins() as $plugin) { |
|
| 319 | 319 | $plugin->beforeCreate($instance, $this->space); |
| 320 | 320 | } |
| 321 | 321 | |