@@ -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 | $plugin->beforeRemove($instance, $this->space); |
@@ -215,8 +215,8 @@ discard block |
||
| 215 | 215 | $size = count(get_object_vars($instance)); |
| 216 | 216 | $skipped = 0; |
| 217 | 217 | |
| 218 | - foreach($this->space->getFormat() as $index => $info) { |
|
| 219 | - if(!property_exists($instance, $info['name'])) { |
|
| 218 | + foreach ($this->space->getFormat() as $index => $info) { |
|
| 219 | + if (!property_exists($instance, $info['name'])) { |
|
| 220 | 220 | $skipped++; |
| 221 | 221 | $instance->{$info['name']} = null; |
| 222 | 222 | } |
@@ -225,7 +225,7 @@ discard block |
||
| 225 | 225 | ->formatValue($info['type'], $instance->{$info['name']}); |
| 226 | 226 | $tuple[$index] = $instance->{$info['name']}; |
| 227 | 227 | |
| 228 | - if(count($tuple) == $size + $skipped) { |
|
| 228 | + if (count($tuple) == $size + $skipped) { |
|
| 229 | 229 | break; |
| 230 | 230 | } |
| 231 | 231 | } |
@@ -233,20 +233,20 @@ discard block |
||
| 233 | 233 | $key = $this->space->getInstanceKey($instance); |
| 234 | 234 | $client = $this->space->getMapper()->getClient(); |
| 235 | 235 | |
| 236 | - if(array_key_exists($key, $this->persisted)) { |
|
| 236 | + if (array_key_exists($key, $this->persisted)) { |
|
| 237 | 237 | // update |
| 238 | 238 | $update = array_diff_assoc($tuple, $this->original[$key]); |
| 239 | - if(!count($update)) { |
|
| 239 | + if (!count($update)) { |
|
| 240 | 240 | return $instance; |
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | $operations = []; |
| 244 | - foreach($update as $index => $value) { |
|
| 244 | + foreach ($update as $index => $value) { |
|
| 245 | 245 | $operations[] = ['=', $index, $value]; |
| 246 | 246 | } |
| 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 | 252 | $plugin->beforeCreate($instance, $this->space); |