@@ -31,6 +31,9 @@ |
||
| 31 | 31 | return $response; |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | + /** |
|
| 35 | + * @param double $start |
|
| 36 | + */ |
|
| 34 | 37 | private function log($start, $class, $request = [], $response = []) |
| 35 | 38 | { |
| 36 | 39 | if ($this->logging) { |
@@ -29,11 +29,17 @@ |
||
| 29 | 29 | return $this->getSchema()->getSpace($space)->getRepository(); |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | + /** |
|
| 33 | + * @param string $space |
|
| 34 | + */ |
|
| 32 | 35 | public function findOne($space, $params = []) |
| 33 | 36 | { |
| 34 | 37 | return $this->getRepository($space)->findOne($params); |
| 35 | 38 | } |
| 36 | 39 | |
| 40 | + /** |
|
| 41 | + * @param string $space |
|
| 42 | + */ |
|
| 37 | 43 | public function find($space, $params = []) |
| 38 | 44 | { |
| 39 | 45 | return $this->getRepository($space)->find($params); |
@@ -48,8 +48,8 @@ |
||
| 48 | 48 | |
| 49 | 49 | public function save($instance) |
| 50 | 50 | { |
| 51 | - foreach($this->getSchema()->getSpaces() as $space) { |
|
| 52 | - if($space->getRepository()->knows($instance)) { |
|
| 51 | + foreach ($this->getSchema()->getSpaces() as $space) { |
|
| 52 | + if ($space->getRepository()->knows($instance)) { |
|
| 53 | 53 | $space->getRepository()->save($instance); |
| 54 | 54 | } |
| 55 | 55 | } |
@@ -36,19 +36,19 @@ discard block |
||
| 36 | 36 | public function createIndex($config) |
| 37 | 37 | { |
| 38 | 38 | |
| 39 | - if(!is_array($config)) { |
|
| 39 | + if (!is_array($config)) { |
|
| 40 | 40 | $config = ['fields' => $config]; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | |
| 44 | - if(!array_key_exists('fields', $config)) { |
|
| 45 | - if(array_values($config) != $config) { |
|
| 44 | + if (!array_key_exists('fields', $config)) { |
|
| 45 | + if (array_values($config) != $config) { |
|
| 46 | 46 | throw new Exception("Invalid index configuration"); |
| 47 | 47 | } |
| 48 | 48 | $config['fields'] = $config; |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | - if(!is_array($config['fields'])) { |
|
| 51 | + if (!is_array($config['fields'])) { |
|
| 52 | 52 | $config['fields'] = [$config['fields']]; |
| 53 | 53 | } |
| 54 | 54 | |
@@ -56,16 +56,16 @@ discard block |
||
| 56 | 56 | 'parts' => [] |
| 57 | 57 | ]; |
| 58 | 58 | |
| 59 | - foreach($config as $k => $v) { |
|
| 60 | - if($k != 'name' && $k != 'fields') { |
|
| 59 | + foreach ($config as $k => $v) { |
|
| 60 | + if ($k != 'name' && $k != 'fields') { |
|
| 61 | 61 | $options[$k] = $v; |
| 62 | 62 | } |
| 63 | 63 | } |
| 64 | - foreach($config['fields'] as $property) { |
|
| 65 | - if(!$this->getPropertyType($property)) { |
|
| 64 | + foreach ($config['fields'] as $property) { |
|
| 65 | + if (!$this->getPropertyType($property)) { |
|
| 66 | 66 | throw new Exception("Unknown property $property", 1); |
| 67 | 67 | } |
| 68 | - $options['parts'][] = $this->getPropertyIndex($property)+1; |
|
| 68 | + $options['parts'][] = $this->getPropertyIndex($property) + 1; |
|
| 69 | 69 | $options['parts'][] = $this->getPropertyType($property); |
| 70 | 70 | } |
| 71 | 71 | |
@@ -93,8 +93,8 @@ discard block |
||
| 93 | 93 | |
| 94 | 94 | public function getFormat() |
| 95 | 95 | { |
| 96 | - if(!$this->format) { |
|
| 97 | - if($this->isSpecial()) { |
|
| 96 | + if (!$this->format) { |
|
| 97 | + if ($this->isSpecial()) { |
|
| 98 | 98 | $this->format = $this->mapper->getClient() |
| 99 | 99 | ->getSpace(280)->select([$this->id])->getData()[0][6]; |
| 100 | 100 | |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | { |
| 112 | 112 | $this->formatTypesHash = []; |
| 113 | 113 | $this->formatNamesHash = []; |
| 114 | - foreach($this->format as $key => $row) { |
|
| 114 | + foreach ($this->format as $key => $row) { |
|
| 115 | 115 | $this->formatTypesHash[$row['name']] = $row['type']; |
| 116 | 116 | $this->formatNamesHash[$row['name']] = $key; |
| 117 | 117 | } |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | |
| 126 | 126 | public function getPropertyType($name) |
| 127 | 127 | { |
| 128 | - if(!$this->hasProperty($name)) { |
|
| 128 | + if (!$this->hasProperty($name)) { |
|
| 129 | 129 | throw new Exception("No property $name"); |
| 130 | 130 | } |
| 131 | 131 | return $this->formatTypesHash[$name]; |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | |
| 134 | 134 | public function getPropertyIndex($name) |
| 135 | 135 | { |
| 136 | - if(!$this->hasProperty($name)) { |
|
| 136 | + if (!$this->hasProperty($name)) { |
|
| 137 | 137 | throw new Exception("No property $name"); |
| 138 | 138 | } |
| 139 | 139 | return $this->formatNamesHash[$name]; |
@@ -141,14 +141,14 @@ discard block |
||
| 141 | 141 | |
| 142 | 142 | public function getIndexes() |
| 143 | 143 | { |
| 144 | - if(!$this->indexes) { |
|
| 145 | - if($this->isSpecial()) { |
|
| 144 | + if (!$this->indexes) { |
|
| 145 | + if ($this->isSpecial()) { |
|
| 146 | 146 | $this->indexes = []; |
| 147 | 147 | $indexTuples = $this->mapper->getClient()->getSpace(288)->select([$this->id])->getData(); |
| 148 | 148 | $indexFormat = $this->mapper->getSchema()->getSpace(288)->getFormat(); |
| 149 | - foreach($indexTuples as $tuple) { |
|
| 149 | + foreach ($indexTuples as $tuple) { |
|
| 150 | 150 | $instance = (object) []; |
| 151 | - foreach($indexFormat as $index => $format) { |
|
| 151 | + foreach ($indexFormat as $index => $format) { |
|
| 152 | 152 | $instance->{$format['name']} = $tuple[$index]; |
| 153 | 153 | } |
| 154 | 154 | $this->indexes[] = $instance; |
@@ -166,44 +166,44 @@ discard block |
||
| 166 | 166 | $keys = array_keys($params); |
| 167 | 167 | |
| 168 | 168 | $keys = []; |
| 169 | - foreach($params as $name => $value) { |
|
| 169 | + foreach ($params as $name => $value) { |
|
| 170 | 170 | $keys[] = $this->getPropertyIndex($name); |
| 171 | 171 | } |
| 172 | - if($keys == [0]) { |
|
| 172 | + if ($keys == [0]) { |
|
| 173 | 173 | // primary index |
| 174 | 174 | return 0; |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | // equals |
| 178 | - foreach($this->getIndexes() as $index) { |
|
| 178 | + foreach ($this->getIndexes() as $index) { |
|
| 179 | 179 | $equals = false; |
| 180 | - if(count($keys) == count($index->parts)) { |
|
| 180 | + if (count($keys) == count($index->parts)) { |
|
| 181 | 181 | // same length |
| 182 | 182 | $equals = true; |
| 183 | - foreach($index->parts as $part) { |
|
| 183 | + foreach ($index->parts as $part) { |
|
| 184 | 184 | $equals = $equals && in_array($part[0], $keys); |
| 185 | 185 | } |
| 186 | 186 | } |
| 187 | 187 | |
| 188 | - if($equals) { |
|
| 188 | + if ($equals) { |
|
| 189 | 189 | return $index->iid; |
| 190 | 190 | } |
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | // index part |
| 194 | - foreach($this->getIndexes() as $index) { |
|
| 194 | + foreach ($this->getIndexes() as $index) { |
|
| 195 | 195 | $partial = []; |
| 196 | - foreach($index->parts as $n => $part) { |
|
| 197 | - if(!array_key_exists($n, $keys)) { |
|
| 196 | + foreach ($index->parts as $n => $part) { |
|
| 197 | + if (!array_key_exists($n, $keys)) { |
|
| 198 | 198 | break; |
| 199 | 199 | } |
| 200 | - if($keys[$n] != $part[0]) { |
|
| 200 | + if ($keys[$n] != $part[0]) { |
|
| 201 | 201 | break; |
| 202 | 202 | } |
| 203 | 203 | $partial[] = $keys[$n]; |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | - if(count($partial) == count($keys)) { |
|
| 206 | + if (count($partial) == count($keys)) { |
|
| 207 | 207 | return $index->iid; |
| 208 | 208 | } |
| 209 | 209 | } |
@@ -217,9 +217,9 @@ discard block |
||
| 217 | 217 | $format = $this->getFormat(); |
| 218 | 218 | |
| 219 | 219 | $values = []; |
| 220 | - foreach($index->parts as $part) { |
|
| 220 | + foreach ($index->parts as $part) { |
|
| 221 | 221 | $name = $format[$part[0]]['name']; |
| 222 | - if(!array_key_exists($name, $params)) { |
|
| 222 | + if (!array_key_exists($name, $params)) { |
|
| 223 | 223 | break; |
| 224 | 224 | } |
| 225 | 225 | $values[] = $this->mapper->getSchema()->formatValue($part[1], $params[$name]); |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | public function getPrimaryIndex() |
| 231 | 231 | { |
| 232 | 232 | $indexes = $this->getIndexes(); |
| 233 | - if(!count($indexes)) { |
|
| 233 | + if (!count($indexes)) { |
|
| 234 | 234 | throw new Exception("No primary index"); |
| 235 | 235 | } |
| 236 | 236 | return $indexes[0]; |
@@ -239,7 +239,7 @@ discard block |
||
| 239 | 239 | public function getTupleKey($tuple) |
| 240 | 240 | { |
| 241 | 241 | $key = []; |
| 242 | - foreach($this->getPrimaryIndex()->parts as $part) { |
|
| 242 | + foreach ($this->getPrimaryIndex()->parts as $part) { |
|
| 243 | 243 | $key[] = $tuple[$part[0]]; |
| 244 | 244 | } |
| 245 | 245 | return count($key) == 1 ? $key[0] : implode(':', $key); |
@@ -250,9 +250,9 @@ discard block |
||
| 250 | 250 | |
| 251 | 251 | $key = []; |
| 252 | 252 | |
| 253 | - foreach($this->getPrimaryIndex()->parts as $part) { |
|
| 253 | + foreach ($this->getPrimaryIndex()->parts as $part) { |
|
| 254 | 254 | $name = $this->getFormat()[$part[0]]['name']; |
| 255 | - if(!property_exists($instance, $name)) { |
|
| 255 | + if (!property_exists($instance, $name)) { |
|
| 256 | 256 | throw new Exception("Field $name is undefined", 1); |
| 257 | 257 | } |
| 258 | 258 | $key[] = $instance->$name; |
@@ -24,8 +24,8 @@ discard block |
||
| 24 | 24 | public function create($data) |
| 25 | 25 | { |
| 26 | 26 | $instance = (object) []; |
| 27 | - foreach($this->space->getFormat() as $row) { |
|
| 28 | - if(array_key_exists($row['name'], $data)) { |
|
| 27 | + foreach ($this->space->getFormat() as $row) { |
|
| 28 | + if (array_key_exists($row['name'], $data)) { |
|
| 29 | 29 | $instance->{$row['name']} = $data[$row['name']]; |
| 30 | 30 | } |
| 31 | 31 | } |
@@ -45,18 +45,18 @@ discard block |
||
| 45 | 45 | public function find($params = [], $one = false) |
| 46 | 46 | { |
| 47 | 47 | $cacheIndex = array_search([$params, $one], $this->cache); |
| 48 | - if($cacheIndex !== false) { |
|
| 48 | + if ($cacheIndex !== false) { |
|
| 49 | 49 | return $this->results[$cacheIndex]; |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | - if(!is_array($params)) { |
|
| 52 | + if (!is_array($params)) { |
|
| 53 | 53 | $params = [$params]; |
| 54 | 54 | } |
| 55 | - if(count($params) == 1 && array_key_exists(0, $params)) { |
|
| 55 | + if (count($params) == 1 && array_key_exists(0, $params)) { |
|
| 56 | 56 | $primary = $this->space->getPrimaryIndex(); |
| 57 | - if(count($primary->parts) == 1) { |
|
| 57 | + if (count($primary->parts) == 1) { |
|
| 58 | 58 | $formatted = $this->space->getMapper()->getSchema()->formatValue($primary->parts[0][1], $params[0]); |
| 59 | - if($params[0] == $formatted) { |
|
| 59 | + if ($params[0] == $formatted) { |
|
| 60 | 60 | $params = [ |
| 61 | 61 | $this->space->getFormat()[$primary->parts[0][0]]['name'] => $params[0] |
| 62 | 62 | ]; |
@@ -64,8 +64,8 @@ discard block |
||
| 64 | 64 | } |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - if(array_key_exists('id', $params)) { |
|
| 68 | - if(array_key_exists($params['id'], $this->persisted)) { |
|
| 67 | + if (array_key_exists('id', $params)) { |
|
| 68 | + if (array_key_exists($params['id'], $this->persisted)) { |
|
| 69 | 69 | $instance = $this->persisted[$params['id']]; |
| 70 | 70 | return $one ? $instance : [$instance]; |
| 71 | 71 | } |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | |
| 74 | 74 | |
| 75 | 75 | $index = $this->space->castIndex($params); |
| 76 | - if(is_null($index)) { |
|
| 76 | + if (is_null($index)) { |
|
| 77 | 77 | throw new Exception("No index for params ".json_encode($params)); |
| 78 | 78 | } |
| 79 | 79 | |
@@ -86,15 +86,15 @@ discard block |
||
| 86 | 86 | $data = $client->getSpace($this->space->getId())->select($values, $index)->getData(); |
| 87 | 87 | |
| 88 | 88 | $result = []; |
| 89 | - foreach($data as $tuple) { |
|
| 89 | + foreach ($data as $tuple) { |
|
| 90 | 90 | $instance = $this->getInstance($tuple); |
| 91 | - if($one) { |
|
| 91 | + if ($one) { |
|
| 92 | 92 | return $this->results[$cacheIndex] = $instance; |
| 93 | 93 | } |
| 94 | 94 | $result[] = $instance; |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - if($one) { |
|
| 97 | + if ($one) { |
|
| 98 | 98 | return $this->results[$cacheIndex] = null; |
| 99 | 99 | } |
| 100 | 100 | |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | { |
| 106 | 106 | $key = $this->space->getTupleKey($tuple); |
| 107 | 107 | |
| 108 | - if(array_key_exists($key, $this->persisted)) { |
|
| 108 | + if (array_key_exists($key, $this->persisted)) { |
|
| 109 | 109 | return $this->persisted[$key]; |
| 110 | 110 | } |
| 111 | 111 | |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | |
| 114 | 114 | $this->original[$key] = $tuple; |
| 115 | 115 | |
| 116 | - foreach($this->space->getFormat() as $index => $info) { |
|
| 116 | + foreach ($this->space->getFormat() as $index => $info) { |
|
| 117 | 117 | $instance->{$info['name']} = array_key_exists($index, $tuple) ? $tuple[$index] : null; |
| 118 | 118 | } |
| 119 | 119 | |
@@ -131,8 +131,8 @@ discard block |
||
| 131 | 131 | { |
| 132 | 132 | $tuple = []; |
| 133 | 133 | |
| 134 | - foreach($this->space->getFormat() as $index => $info) { |
|
| 135 | - if(property_exists($instance, $info['name'])) { |
|
| 134 | + foreach ($this->space->getFormat() as $index => $info) { |
|
| 135 | + if (property_exists($instance, $info['name'])) { |
|
| 136 | 136 | $instance->{$info['name']} = $this->space->getMapper()->getSchema() |
| 137 | 137 | ->formatValue($info['type'], $instance->{$info['name']}); |
| 138 | 138 | $tuple[$index] = $instance->{$info['name']}; |
@@ -142,20 +142,20 @@ discard block |
||
| 142 | 142 | $key = $this->space->getInstanceKey($instance); |
| 143 | 143 | $client = $this->space->getMapper()->getClient(); |
| 144 | 144 | |
| 145 | - if(array_key_exists($key, $this->persisted)) { |
|
| 145 | + if (array_key_exists($key, $this->persisted)) { |
|
| 146 | 146 | // update |
| 147 | 147 | $update = array_diff_assoc($tuple, $this->original[$key]); |
| 148 | - if(!count($update)) { |
|
| 148 | + if (!count($update)) { |
|
| 149 | 149 | return $instance; |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | $operations = []; |
| 153 | - foreach($update as $index => $value) { |
|
| 153 | + foreach ($update as $index => $value) { |
|
| 154 | 154 | $operations[] = ['=', $index, $value]; |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | $pk = []; |
| 158 | - foreach($this->space->getPrimaryIndex()->parts as $part) { |
|
| 158 | + foreach ($this->space->getPrimaryIndex()->parts as $part) { |
|
| 159 | 159 | $pk[] = $this->original[$key][$part[0]]; |
| 160 | 160 | } |
| 161 | 161 | |
@@ -26,14 +26,14 @@ discard block |
||
| 26 | 26 | |
| 27 | 27 | public function getSpace($id) |
| 28 | 28 | { |
| 29 | - if(is_string($id)) { |
|
| 30 | - if(array_key_exists($id, $this->names)) { |
|
| 29 | + if (is_string($id)) { |
|
| 30 | + if (array_key_exists($id, $this->names)) { |
|
| 31 | 31 | return $this->getSpace($this->names[$id]); |
| 32 | 32 | } |
| 33 | 33 | throw new Exception("No space $id"); |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | - if(!array_key_exists($id, $this->spaces)) { |
|
| 36 | + if (!array_key_exists($id, $this->spaces)) { |
|
| 37 | 37 | $this->spaces[$id] = new Space($this->mapper, $id); |
| 38 | 38 | } |
| 39 | 39 | return $this->spaces[$id]; |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | |
| 59 | 59 | public function formatValue($type, $value) |
| 60 | 60 | { |
| 61 | - switch($type) { |
|
| 61 | + switch ($type) { |
|
| 62 | 62 | case 'str': return (string) $value; |
| 63 | 63 | case 'unsigned': return (int) $value; |
| 64 | 64 | default: return $value; |