| @@ -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) { | 
| @@ -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); | 
| @@ -19,8 +19,8 @@ discard block | ||
| 19 | 19 | |
| 20 | 20 | public function addPlugin($class) | 
| 21 | 21 |      { | 
| 22 | -        if(!is_subclass_of($class, Plugin::class)) { | |
| 23 | -            throw new Exception("Plugin should extend " . Plugin::class . " class"); | |
| 22 | +        if (!is_subclass_of($class, Plugin::class)) { | |
| 23 | +            throw new Exception("Plugin should extend ".Plugin::class." class"); | |
| 24 | 24 | } | 
| 25 | 25 | |
| 26 | 26 | $plugin = is_object($class) ? $class : new $class($this); | 
| @@ -48,8 +48,8 @@ discard block | ||
| 48 | 48 | |
| 49 | 49 | public function findRepository(Entity $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 | return $space->getRepository(); | 
| 54 | 54 | } | 
| 55 | 55 | } | 
| @@ -59,7 +59,7 @@ discard block | ||
| 59 | 59 | |
| 60 | 60 | public function getBootstrap() | 
| 61 | 61 |      { | 
| 62 | -        if(!$this->bootstrap) { | |
| 62 | +        if (!$this->bootstrap) { | |
| 63 | 63 | $this->bootstrap = new Bootstrap($this); | 
| 64 | 64 | } | 
| 65 | 65 | |
| @@ -73,7 +73,7 @@ discard block | ||
| 73 | 73 | |
| 74 | 74 | public function getPlugin($class) | 
| 75 | 75 |      { | 
| 76 | -        if(!array_key_exists($class, $this->plugins)) { | |
| 76 | +        if (!array_key_exists($class, $this->plugins)) { | |
| 77 | 77 |              throw new Exception("No plugin $class"); | 
| 78 | 78 | } | 
| 79 | 79 | return $this->plugins[$class]; | 
| @@ -92,8 +92,8 @@ discard block | ||
| 92 | 92 | public function getRepositories() | 
| 93 | 93 |      { | 
| 94 | 94 | $repositories = []; | 
| 95 | -        foreach($this->getSchema()->getSpaces() as $space) { | |
| 96 | -            if($space->repositoryExists()) { | |
| 95 | +        foreach ($this->getSchema()->getSpaces() as $space) { | |
| 96 | +            if ($space->repositoryExists()) { | |
| 97 | 97 | $repositories[] = $space->getRepository(); | 
| 98 | 98 | } | 
| 99 | 99 | } | 
| @@ -107,7 +107,7 @@ discard block | ||
| 107 | 107 | |
| 108 | 108 | public function remove($space, $params = []) | 
| 109 | 109 |      { | 
| 110 | -        if($space instanceof Entity) { | |
| 110 | +        if ($space instanceof Entity) { | |
| 111 | 111 | $this->findRepository($space)->removeEntity($space); | 
| 112 | 112 | |
| 113 | 113 |          } else { | 
| @@ -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 generateKey(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'); | 
| @@ -37,7 +37,7 @@ discard block | ||
| 37 | 37 | } | 
| 38 | 38 | |
| 39 | 39 |          $entity = $this->mapper->findOne('sequence', $space->getId()); | 
| 40 | -        if(!$entity) { | |
| 40 | +        if (!$entity) { | |
| 41 | 41 |              $entity = $this->mapper->create('sequence', [ | 
| 42 | 42 | 'space' => $space->getId(), | 
| 43 | 43 | '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 | } | 
| @@ -20,8 +20,8 @@ | ||
| 20 | 20 | public function migrate() | 
| 21 | 21 |      { | 
| 22 | 22 | $schema = $this->mapper->getSchema(); | 
| 23 | -        foreach($this->migrations as $migration) { | |
| 24 | -            if(!is_object($migration)) { | |
| 23 | +        foreach ($this->migrations as $migration) { | |
| 24 | +            if (!is_object($migration)) { | |
| 25 | 25 | $migration = new $migration; | 
| 26 | 26 | } | 
| 27 | 27 |              $schema->once(get_class($migration), function() use ($migration) { | 
| @@ -27,7 +27,7 @@ discard block | ||
| 27 | 27 | |
| 28 | 28 | public function addProperties($config) | 
| 29 | 29 |      { | 
| 30 | -        foreach($config as $name => $type) { | |
| 30 | +        foreach ($config as $name => $type) { | |
| 31 | 31 | $this->addProperty($name, $type); | 
| 32 | 32 | } | 
| 33 | 33 | return $this; | 
| @@ -36,8 +36,8 @@ discard block | ||
| 36 | 36 | public function addProperty($name, $type) | 
| 37 | 37 |      { | 
| 38 | 38 | $format = $this->getFormat(); | 
| 39 | -        foreach($format as $field) { | |
| 40 | -            if($field['name'] == $name) { | |
| 39 | +        foreach ($format as $field) { | |
| 40 | +            if ($field['name'] == $name) { | |
| 41 | 41 |                  throw new Exception("Property $name exists"); | 
| 42 | 42 | } | 
| 43 | 43 | } | 
| @@ -54,7 +54,7 @@ discard block | ||
| 54 | 54 |      { | 
| 55 | 55 | $format = $this->getFormat(); | 
| 56 | 56 | $last = array_pop($format); | 
| 57 | -        if($last['name'] != $name) { | |
| 57 | +        if ($last['name'] != $name) { | |
| 58 | 58 |              throw new Exception("Remove only last property"); | 
| 59 | 59 | } | 
| 60 | 60 |          $this->mapper->getClient()->evaluate("box.space[$this->id]:format(...)", [$format]); | 
| @@ -82,13 +82,13 @@ discard block | ||
| 82 | 82 | public function createIndex($config) | 
| 83 | 83 |      { | 
| 84 | 84 | |
| 85 | -        if(!is_array($config)) { | |
| 85 | +        if (!is_array($config)) { | |
| 86 | 86 | $config = ['fields' => $config]; | 
| 87 | 87 | } | 
| 88 | 88 | |
| 89 | 89 | |
| 90 | -        if(!array_key_exists('fields', $config)) { | |
| 91 | -            if(array_values($config) != $config) { | |
| 90 | +        if (!array_key_exists('fields', $config)) { | |
| 91 | +            if (array_values($config) != $config) { | |
| 92 | 92 |                  throw new Exception("Invalid index configuration"); | 
| 93 | 93 | } | 
| 94 | 94 | $config = [ | 
| @@ -96,7 +96,7 @@ discard block | ||
| 96 | 96 | ]; | 
| 97 | 97 | } | 
| 98 | 98 | |
| 99 | -        if(!is_array($config['fields'])) { | |
| 99 | +        if (!is_array($config['fields'])) { | |
| 100 | 100 | $config['fields'] = [$config['fields']]; | 
| 101 | 101 | } | 
| 102 | 102 | |
| @@ -104,17 +104,17 @@ discard block | ||
| 104 | 104 | 'parts' => [] | 
| 105 | 105 | ]; | 
| 106 | 106 | |
| 107 | -        foreach($config as $k => $v) { | |
| 108 | -            if($k != 'name' && $k != 'fields') { | |
| 107 | +        foreach ($config as $k => $v) { | |
| 108 | +            if ($k != 'name' && $k != 'fields') { | |
| 109 | 109 | $options[$k] = $v; | 
| 110 | 110 | } | 
| 111 | 111 | } | 
| 112 | 112 | |
| 113 | -        foreach($config['fields'] as $property) { | |
| 114 | -            if(!$this->getPropertyType($property)) { | |
| 113 | +        foreach ($config['fields'] as $property) { | |
| 114 | +            if (!$this->getPropertyType($property)) { | |
| 115 | 115 |                  throw new Exception("Unknown property $property", 1); | 
| 116 | 116 | } | 
| 117 | - $options['parts'][] = $this->getPropertyIndex($property)+1; | |
| 117 | + $options['parts'][] = $this->getPropertyIndex($property) + 1; | |
| 118 | 118 | $options['parts'][] = $this->getPropertyType($property); | 
| 119 | 119 | } | 
| 120 | 120 | |
| @@ -138,15 +138,15 @@ discard block | ||
| 138 | 138 | |
| 139 | 139 | public function getFormat() | 
| 140 | 140 |      { | 
| 141 | -        if(!$this->format) { | |
| 142 | -            if($this->isSpecial()) { | |
| 141 | +        if (!$this->format) { | |
| 142 | +            if ($this->isSpecial()) { | |
| 143 | 143 | $this->format = $this->mapper->getClient() | 
| 144 | 144 | ->getSpace(280)->select([$this->id])->getData()[0][6]; | 
| 145 | 145 | |
| 146 | 146 |              } else { | 
| 147 | 147 |                  $this->format = $this->mapper->findOne('_space', ['id' => $this->id])->format; | 
| 148 | 148 | } | 
| 149 | -            if(!$this->format) { | |
| 149 | +            if (!$this->format) { | |
| 150 | 150 | $this->format = []; | 
| 151 | 151 | } | 
| 152 | 152 | $this->parseFormat(); | 
| @@ -169,7 +169,7 @@ discard block | ||
| 169 | 169 |      { | 
| 170 | 170 | $this->formatTypesHash = []; | 
| 171 | 171 | $this->formatNamesHash = []; | 
| 172 | -        foreach($this->format as $key => $row) { | |
| 172 | +        foreach ($this->format as $key => $row) { | |
| 173 | 173 | $this->formatTypesHash[$row['name']] = $row['type']; | 
| 174 | 174 | $this->formatNamesHash[$row['name']] = $key; | 
| 175 | 175 | } | 
| @@ -184,7 +184,7 @@ discard block | ||
| 184 | 184 | |
| 185 | 185 | public function getPropertyType($name) | 
| 186 | 186 |      { | 
| 187 | -        if(!$this->hasProperty($name)) { | |
| 187 | +        if (!$this->hasProperty($name)) { | |
| 188 | 188 |              throw new Exception("No property $name"); | 
| 189 | 189 | } | 
| 190 | 190 | return $this->formatTypesHash[$name]; | 
| @@ -192,7 +192,7 @@ discard block | ||
| 192 | 192 | |
| 193 | 193 | public function getPropertyIndex($name) | 
| 194 | 194 |      { | 
| 195 | -        if(!$this->hasProperty($name)) { | |
| 195 | +        if (!$this->hasProperty($name)) { | |
| 196 | 196 |              throw new Exception("No property $name"); | 
| 197 | 197 | } | 
| 198 | 198 | return $this->formatNamesHash[$name]; | 
| @@ -200,14 +200,14 @@ discard block | ||
| 200 | 200 | |
| 201 | 201 | public function getIndexes() | 
| 202 | 202 |      { | 
| 203 | -        if(!$this->indexes) { | |
| 204 | -            if($this->isSpecial()) { | |
| 203 | +        if (!$this->indexes) { | |
| 204 | +            if ($this->isSpecial()) { | |
| 205 | 205 | $this->indexes = []; | 
| 206 | 206 | $indexTuples = $this->mapper->getClient()->getSpace(288)->select([$this->id])->getData(); | 
| 207 | 207 | $indexFormat = $this->mapper->getSchema()->getSpace(288)->getFormat(); | 
| 208 | -                foreach($indexTuples as $tuple) { | |
| 208 | +                foreach ($indexTuples as $tuple) { | |
| 209 | 209 | $instance = (object) []; | 
| 210 | -                    foreach($indexFormat as $index => $format) { | |
| 210 | +                    foreach ($indexFormat as $index => $format) { | |
| 211 | 211 |                          $instance->{$format['name']} = $tuple[$index]; | 
| 212 | 212 | } | 
| 213 | 213 | $this->indexes[] = $instance; | 
| @@ -225,44 +225,44 @@ discard block | ||
| 225 | 225 | $keys = array_keys($params); | 
| 226 | 226 | |
| 227 | 227 | $keys = []; | 
| 228 | -        foreach($params as $name => $value) { | |
| 228 | +        foreach ($params as $name => $value) { | |
| 229 | 229 | $keys[] = $this->getPropertyIndex($name); | 
| 230 | 230 | } | 
| 231 | -        if($keys == [0]) { | |
| 231 | +        if ($keys == [0]) { | |
| 232 | 232 | // primary index | 
| 233 | 233 | return 0; | 
| 234 | 234 | } | 
| 235 | 235 | |
| 236 | 236 | // equals | 
| 237 | -        foreach($this->getIndexes() as $index) { | |
| 237 | +        foreach ($this->getIndexes() as $index) { | |
| 238 | 238 | $equals = false; | 
| 239 | -            if(count($keys) == count($index->parts)) { | |
| 239 | +            if (count($keys) == count($index->parts)) { | |
| 240 | 240 | // same length | 
| 241 | 241 | $equals = true; | 
| 242 | -                foreach($index->parts as $part) { | |
| 242 | +                foreach ($index->parts as $part) { | |
| 243 | 243 | $equals = $equals && in_array($part[0], $keys); | 
| 244 | 244 | } | 
| 245 | 245 | } | 
| 246 | 246 | |
| 247 | -            if($equals) { | |
| 247 | +            if ($equals) { | |
| 248 | 248 | return $index->iid; | 
| 249 | 249 | } | 
| 250 | 250 | } | 
| 251 | 251 | |
| 252 | 252 | // index part | 
| 253 | -        foreach($this->getIndexes() as $index) { | |
| 253 | +        foreach ($this->getIndexes() as $index) { | |
| 254 | 254 | $partial = []; | 
| 255 | -            foreach($index->parts as $n => $part) { | |
| 256 | -                if(!array_key_exists($n, $keys)) { | |
| 255 | +            foreach ($index->parts as $n => $part) { | |
| 256 | +                if (!array_key_exists($n, $keys)) { | |
| 257 | 257 | break; | 
| 258 | 258 | } | 
| 259 | -                if($keys[$n] != $part[0]) { | |
| 259 | +                if ($keys[$n] != $part[0]) { | |
| 260 | 260 | break; | 
| 261 | 261 | } | 
| 262 | 262 | $partial[] = $keys[$n]; | 
| 263 | 263 | } | 
| 264 | 264 | |
| 265 | -            if(count($partial) == count($keys)) { | |
| 265 | +            if (count($partial) == count($keys)) { | |
| 266 | 266 | return $index->iid; | 
| 267 | 267 | } | 
| 268 | 268 | } | 
| @@ -276,9 +276,9 @@ discard block | ||
| 276 | 276 | $format = $this->getFormat(); | 
| 277 | 277 | |
| 278 | 278 | $values = []; | 
| 279 | -        foreach($index->parts as $part) { | |
| 279 | +        foreach ($index->parts as $part) { | |
| 280 | 280 | $name = $format[$part[0]]['name']; | 
| 281 | -            if(!array_key_exists($name, $params)) { | |
| 281 | +            if (!array_key_exists($name, $params)) { | |
| 282 | 282 | break; | 
| 283 | 283 | } | 
| 284 | 284 | $values[] = $this->mapper->getSchema()->formatValue($part[1], $params[$name]); | 
| @@ -289,7 +289,7 @@ discard block | ||
| 289 | 289 | public function getPrimaryIndex() | 
| 290 | 290 |      { | 
| 291 | 291 | $indexes = $this->getIndexes(); | 
| 292 | -        if(!count($indexes)) { | |
| 292 | +        if (!count($indexes)) { | |
| 293 | 293 |              throw new Exception("No primary index"); | 
| 294 | 294 | } | 
| 295 | 295 | return $indexes[0]; | 
| @@ -298,7 +298,7 @@ discard block | ||
| 298 | 298 | public function getTupleKey($tuple) | 
| 299 | 299 |      { | 
| 300 | 300 | $key = []; | 
| 301 | -        foreach($this->getPrimaryIndex()->parts as $part) { | |
| 301 | +        foreach ($this->getPrimaryIndex()->parts as $part) { | |
| 302 | 302 | $key[] = $tuple[$part[0]]; | 
| 303 | 303 | } | 
| 304 | 304 |          return count($key) == 1 ? $key[0] : implode(':', $key); | 
| @@ -309,9 +309,9 @@ discard block | ||
| 309 | 309 | |
| 310 | 310 | $key = []; | 
| 311 | 311 | |
| 312 | -        foreach($this->getPrimaryIndex()->parts as $part) { | |
| 312 | +        foreach ($this->getPrimaryIndex()->parts as $part) { | |
| 313 | 313 | $name = $this->getFormat()[$part[0]]['name']; | 
| 314 | -            if(!property_exists($instance, $name)) { | |
| 314 | +            if (!property_exists($instance, $name)) { | |
| 315 | 315 |                  throw new Exception("Field $name is undefined", 1); | 
| 316 | 316 | } | 
| 317 | 317 | $key[] = $instance->$name; | 
| @@ -323,10 +323,10 @@ discard block | ||
| 323 | 323 | public function getRepository() | 
| 324 | 324 |      { | 
| 325 | 325 | $class = Repository::class; | 
| 326 | -        foreach($this->mapper->getPlugins() as $plugin) { | |
| 326 | +        foreach ($this->mapper->getPlugins() as $plugin) { | |
| 327 | 327 | $repositoryClass = $plugin->getRepositoryClass($this); | 
| 328 | -            if($repositoryClass) { | |
| 329 | -                if($class != Repository::class) { | |
| 328 | +            if ($repositoryClass) { | |
| 329 | +                if ($class != Repository::class) { | |
| 330 | 330 |                      throw new Exception('Repository class override'); | 
| 331 | 331 | } | 
| 332 | 332 | $class = $repositoryClass; | 
| @@ -20,7 +20,7 @@ discard block | ||
| 20 | 20 | public function beforeUpdate(Entity $instance, Space $space) | 
| 21 | 21 |      { | 
| 22 | 22 | $key = $this->getKey($instance, $space); | 
| 23 | -        if(!array_key_exists($key, $this->create)) { | |
| 23 | +        if (!array_key_exists($key, $this->create)) { | |
| 24 | 24 | $this->update[$key] = $instance; | 
| 25 | 25 | } | 
| 26 | 26 | } | 
| @@ -29,12 +29,12 @@ discard block | ||
| 29 | 29 |      { | 
| 30 | 30 | $key = $this->getKey($instance, $space); | 
| 31 | 31 | |
| 32 | -        if(array_key_exists($key, $this->create)) { | |
| 32 | +        if (array_key_exists($key, $this->create)) { | |
| 33 | 33 | unset($this->create[$key]); | 
| 34 | 34 | return; | 
| 35 | 35 | } | 
| 36 | 36 | |
| 37 | -        if(array_key_exists($key, $this->update)) { | |
| 37 | +        if (array_key_exists($key, $this->update)) { | |
| 38 | 38 | unset($this->update[$key]); | 
| 39 | 39 | } | 
| 40 | 40 | |
| @@ -53,7 +53,7 @@ discard block | ||
| 53 | 53 | $key = [$space->getName()]; | 
| 54 | 54 | |
| 55 | 55 | $format = $space->getFormat(); | 
| 56 | -        foreach($space->getPrimaryIndex()->parts as $part) { | |
| 56 | +        foreach ($space->getPrimaryIndex()->parts as $part) { | |
| 57 | 57 |              $key[] = $instance->{$format[$part[0]]['name']}; | 
| 58 | 58 | } | 
| 59 | 59 | |
| @@ -64,11 +64,11 @@ discard block | ||
| 64 | 64 |      { | 
| 65 | 65 | $result = (object) []; | 
| 66 | 66 | |
| 67 | -        foreach(['create', 'update', 'remove'] as $action) { | |
| 67 | +        foreach (['create', 'update', 'remove'] as $action) { | |
| 68 | 68 | $data = []; | 
| 69 | -            foreach($this->$action as $key => $row) { | |
| 69 | +            foreach ($this->$action as $key => $row) { | |
| 70 | 70 |                  list($space) = explode(':', $key); | 
| 71 | -                if(!array_key_exists($space, $data)) { | |
| 71 | +                if (!array_key_exists($space, $data)) { | |
| 72 | 72 | $data[$space] = []; | 
| 73 | 73 | } | 
| 74 | 74 | $data[$space][] = $row; | 
| @@ -120,6 +120,9 @@ | ||
| 120 | 120 | |
| 121 | 121 | private $underscores = []; | 
| 122 | 122 | |
| 123 | + /** | |
| 124 | + * @param string $input | |
| 125 | + */ | |
| 123 | 126 | private function toUnderscore($input) | 
| 124 | 127 |      { | 
| 125 | 128 |          if(!array_key_exists($input, $this->underscores)) { | 
| @@ -22,19 +22,19 @@ discard block | ||
| 22 | 22 | $isEntity = is_subclass_of($class, Entity::class); | 
| 23 | 23 | $isRepository = is_subclass_of($class, Repository::class); | 
| 24 | 24 | |
| 25 | -        if(!$isEntity && !$isRepository) { | |
| 25 | +        if (!$isEntity && !$isRepository) { | |
| 26 | 26 |              throw new Exception("Invalid registration"); | 
| 27 | 27 | } | 
| 28 | 28 | |
| 29 | -        if($isEntity) { | |
| 30 | -            if($class == Entity::class) { | |
| 29 | +        if ($isEntity) { | |
| 30 | +            if ($class == Entity::class) { | |
| 31 | 31 |                  throw new Exception("Invalid entity registration"); | 
| 32 | 32 | } | 
| 33 | 33 | $this->entities[] = $class; | 
| 34 | 34 | } | 
| 35 | 35 | |
| 36 | -        if($isRepository) { | |
| 37 | -            if($class == Repository::class) { | |
| 36 | +        if ($isRepository) { | |
| 37 | +            if ($class == Repository::class) { | |
| 38 | 38 |                  throw new Exception("Invalid repository registration"); | 
| 39 | 39 | } | 
| 40 | 40 | $this->repositories[] = $class; | 
| @@ -42,8 +42,8 @@ discard block | ||
| 42 | 42 | |
| 43 | 43 | $reflection = new ReflectionClass($class); | 
| 44 | 44 | $space = $this->toUnderscore($reflection->getShortName()); | 
| 45 | -        if($this->mapper->getSchema()->hasSpace($space)) { | |
| 46 | -            if($isEntity) { | |
| 45 | +        if ($this->mapper->getSchema()->hasSpace($space)) { | |
| 46 | +            if ($isEntity) { | |
| 47 | 47 | $this->mapEntity($name, $class); | 
| 48 | 48 |              } else { | 
| 49 | 49 | $this->mapRepository($name, $class); | 
| @@ -57,7 +57,7 @@ discard block | ||
| 57 | 57 | |
| 58 | 58 | $schema = $this->mapper->getSchema(); | 
| 59 | 59 | |
| 60 | -        foreach($this->entities as $entity) { | |
| 60 | +        foreach ($this->entities as $entity) { | |
| 61 | 61 | |
| 62 | 62 | $class = new ReflectionClass($entity); | 
| 63 | 63 | $spaceName = $this->toUnderscore($class->getShortName()); | 
| @@ -65,34 +65,34 @@ discard block | ||
| 65 | 65 | $space = $schema->hasSpace($spaceName) ? $schema->getSpace($spaceName) : $schema->createSpace($spaceName); | 
| 66 | 66 | $this->mapEntity($spaceName, $entity); | 
| 67 | 67 | |
| 68 | -            foreach($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { | |
| 68 | +            foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { | |
| 69 | 69 | |
| 70 | 70 | $description = $factory->create($property->getDocComment()); | 
| 71 | 71 |                  $tags = $description->getTags('var'); | 
| 72 | 72 | |
| 73 | -                if(!count($tags)) { | |
| 73 | +                if (!count($tags)) { | |
| 74 | 74 |                      throw new Exception("No var tag for ".$entity.'::'.$property->getName()); | 
| 75 | 75 | } | 
| 76 | 76 | |
| 77 | -                if(count($tags) > 1) { | |
| 77 | +                if (count($tags) > 1) { | |
| 78 | 78 |                      throw new Exception("Invalid var tag for ".$entity.'::'.$property->getName()); | 
| 79 | 79 | } | 
| 80 | 80 | |
| 81 | 81 | $property = $this->toUnderscore($property->getName()); | 
| 82 | 82 | $type = $this->getTarantoolType($tags[0]->getType()); | 
| 83 | 83 | |
| 84 | -                if(!$space->hasProperty($property)) { | |
| 84 | +                if (!$space->hasProperty($property)) { | |
| 85 | 85 | $space->addProperty($property, $type); | 
| 86 | 86 | } | 
| 87 | 87 | } | 
| 88 | 88 | } | 
| 89 | 89 | |
| 90 | -        foreach($this->repositories as $repository) { | |
| 90 | +        foreach ($this->repositories as $repository) { | |
| 91 | 91 | |
| 92 | 92 | $class = new ReflectionClass($repository); | 
| 93 | 93 | $spaceName = $this->toUnderscore($class->getShortName()); | 
| 94 | 94 | |
| 95 | -            if(!$schema->hasSpace($spaceName)) { | |
| 95 | +            if (!$schema->hasSpace($spaceName)) { | |
| 96 | 96 |                  throw new Exception("Repository with no entity definition"); | 
| 97 | 97 | } | 
| 98 | 98 | |
| @@ -100,18 +100,18 @@ discard block | ||
| 100 | 100 | |
| 101 | 101 | $space = $schema->getSpace($spaceName); | 
| 102 | 102 | $properties = $class->getDefaultProperties(); | 
| 103 | -            if(array_key_exists('indexes', $properties)) { | |
| 104 | -                foreach($properties['indexes'] as $index) { | |
| 103 | +            if (array_key_exists('indexes', $properties)) { | |
| 104 | +                foreach ($properties['indexes'] as $index) { | |
| 105 | 105 | $space->addIndex($index); | 
| 106 | 106 | } | 
| 107 | 107 | } | 
| 108 | 108 | } | 
| 109 | 109 | |
| 110 | -        foreach($schema->getSpaces() as $space) { | |
| 110 | +        foreach ($schema->getSpaces() as $space) { | |
| 111 | 111 | |
| 112 | -            if(!count($space->getIndexes())) { | |
| 113 | -                if(!$space->hasProperty('id')) { | |
| 114 | -                    throw new Exception("No primary index on ". $space->getName()); | |
| 112 | +            if (!count($space->getIndexes())) { | |
| 113 | +                if (!$space->hasProperty('id')) { | |
| 114 | +                    throw new Exception("No primary index on ".$space->getName()); | |
| 115 | 115 | } | 
| 116 | 116 | $space->addIndex(['id']); | 
| 117 | 117 | } | 
| @@ -122,7 +122,7 @@ discard block | ||
| 122 | 122 | |
| 123 | 123 | private function toUnderscore($input) | 
| 124 | 124 |      { | 
| 125 | -        if(!array_key_exists($input, $this->underscores)) { | |
| 125 | +        if (!array_key_exists($input, $this->underscores)) { | |
| 126 | 126 |              preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches); | 
| 127 | 127 | $ret = $matches[0]; | 
| 128 | 128 |              foreach ($ret as &$match) { | 
| @@ -137,15 +137,15 @@ discard block | ||
| 137 | 137 | |
| 138 | 138 | private function getTarantoolType(string $type) | 
| 139 | 139 |      { | 
| 140 | -        if(array_key_exists($type, $this->tarantoolTypes)) { | |
| 140 | +        if (array_key_exists($type, $this->tarantoolTypes)) { | |
| 141 | 141 | return $this->tarantoolTypes[$type]; | 
| 142 | 142 | } | 
| 143 | 143 | |
| 144 | -        if($type[0] == '\\') { | |
| 144 | +        if ($type[0] == '\\') { | |
| 145 | 145 | return $this->tarantoolTypes[$type] = 'unsigned'; | 
| 146 | 146 | } | 
| 147 | 147 | |
| 148 | -        switch($type) { | |
| 148 | +        switch ($type) { | |
| 149 | 149 | case 'int': | 
| 150 | 150 | return $this->tarantoolTypes[$type] = 'unsigned'; | 
| 151 | 151 | |
| @@ -236,6 +236,9 @@ | ||
| 236 | 236 | $this->cache = []; | 
| 237 | 237 | } | 
| 238 | 238 | |
| 239 | + /** | |
| 240 | + * @param Entity $instance | |
| 241 | + */ | |
| 239 | 242 | public function save($instance) | 
| 240 | 243 |      { | 
| 241 | 244 | $tuple = []; | 
| @@ -24,26 +24,26 @@ 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($this); | 
| 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 | -                if($data[$row['name']] instanceof Entity) { | |
| 40 | +                if ($data[$row['name']] instanceof Entity) { | |
| 41 | 41 |                      $instance->{$row['name']} = $instance->{$row['name']}->id; | 
| 42 | 42 | } | 
| 43 | 43 | } | 
| 44 | 44 | } | 
| 45 | 45 | |
| 46 | -        foreach($this->space->getMapper()->getPlugins() as $plugin) { | |
| 46 | +        foreach ($this->space->getMapper()->getPlugins() as $plugin) { | |
| 47 | 47 | $plugin->generateKey($instance, $this->space); | 
| 48 | 48 | } | 
| 49 | 49 | |
| @@ -62,18 +62,18 @@ discard block | ||
| 62 | 62 | public function find($params = [], $one = false) | 
| 63 | 63 |      { | 
| 64 | 64 | $cacheIndex = array_search([$params, $one], $this->cache); | 
| 65 | -        if($cacheIndex !== false) { | |
| 65 | +        if ($cacheIndex !== false) { | |
| 66 | 66 | return $this->results[$cacheIndex]; | 
| 67 | 67 | } | 
| 68 | 68 | |
| 69 | -        if(!is_array($params)) { | |
| 69 | +        if (!is_array($params)) { | |
| 70 | 70 | $params = [$params]; | 
| 71 | 71 | } | 
| 72 | -        if(count($params) == 1 && array_key_exists(0, $params)) { | |
| 72 | +        if (count($params) == 1 && array_key_exists(0, $params)) { | |
| 73 | 73 | $primary = $this->space->getPrimaryIndex(); | 
| 74 | -            if(count($primary->parts) == 1) { | |
| 74 | +            if (count($primary->parts) == 1) { | |
| 75 | 75 | $formatted = $this->space->getMapper()->getSchema()->formatValue($primary->parts[0][1], $params[0]); | 
| 76 | -                if($params[0] == $formatted) { | |
| 76 | +                if ($params[0] == $formatted) { | |
| 77 | 77 | $params = [ | 
| 78 | 78 | $this->space->getFormat()[$primary->parts[0][0]]['name'] => $params[0] | 
| 79 | 79 | ]; | 
| @@ -81,8 +81,8 @@ discard block | ||
| 81 | 81 | } | 
| 82 | 82 | } | 
| 83 | 83 | |
| 84 | -        if(array_key_exists('id', $params)) { | |
| 85 | -            if(array_key_exists($params['id'], $this->persisted)) { | |
| 84 | +        if (array_key_exists('id', $params)) { | |
| 85 | +            if (array_key_exists($params['id'], $this->persisted)) { | |
| 86 | 86 | $instance = $this->persisted[$params['id']]; | 
| 87 | 87 | return $one ? $instance : [$instance]; | 
| 88 | 88 | } | 
| @@ -90,7 +90,7 @@ discard block | ||
| 90 | 90 | |
| 91 | 91 | |
| 92 | 92 | $index = $this->space->castIndex($params); | 
| 93 | -        if(is_null($index)) { | |
| 93 | +        if (is_null($index)) { | |
| 94 | 94 |              throw new Exception("No index for params ".json_encode($params)); | 
| 95 | 95 | } | 
| 96 | 96 | |
| @@ -103,15 +103,15 @@ discard block | ||
| 103 | 103 | $data = $client->getSpace($this->space->getId())->select($values, $index)->getData(); | 
| 104 | 104 | |
| 105 | 105 | $result = []; | 
| 106 | -        foreach($data as $tuple) { | |
| 106 | +        foreach ($data as $tuple) { | |
| 107 | 107 | $instance = $this->getInstance($tuple); | 
| 108 | -            if($one) { | |
| 108 | +            if ($one) { | |
| 109 | 109 | return $this->results[$cacheIndex] = $instance; | 
| 110 | 110 | } | 
| 111 | 111 | $result[] = $instance; | 
| 112 | 112 | } | 
| 113 | 113 | |
| 114 | -        if($one) { | |
| 114 | +        if ($one) { | |
| 115 | 115 | return $this->results[$cacheIndex] = null; | 
| 116 | 116 | } | 
| 117 | 117 | |
| @@ -122,15 +122,15 @@ discard block | ||
| 122 | 122 |      { | 
| 123 | 123 | $key = $this->space->getTupleKey($tuple); | 
| 124 | 124 | |
| 125 | -        if(array_key_exists($key, $this->persisted)) { | |
| 125 | +        if (array_key_exists($key, $this->persisted)) { | |
| 126 | 126 | return $this->persisted[$key]; | 
| 127 | 127 | } | 
| 128 | 128 | |
| 129 | 129 | $class = Entity::class; | 
| 130 | -        foreach($this->space->getMapper()->getPlugins() as $plugin) { | |
| 130 | +        foreach ($this->space->getMapper()->getPlugins() as $plugin) { | |
| 131 | 131 | $entityClass = $plugin->getEntityClass($this->space); | 
| 132 | -            if($entityClass) { | |
| 133 | -                if($class != Entity::class) { | |
| 132 | +            if ($entityClass) { | |
| 133 | +                if ($class != Entity::class) { | |
| 134 | 134 |                      throw new Exception('Entity class override'); | 
| 135 | 135 | } | 
| 136 | 136 | $class = $entityClass; | 
| @@ -140,7 +140,7 @@ discard block | ||
| 140 | 140 | |
| 141 | 141 | $this->original[$key] = $tuple; | 
| 142 | 142 | |
| 143 | -        foreach($this->space->getFormat() as $index => $info) { | |
| 143 | +        foreach ($this->space->getFormat() as $index => $info) { | |
| 144 | 144 |              $instance->{$info['name']} = array_key_exists($index, $tuple) ? $tuple[$index] : null; | 
| 145 | 145 | } | 
| 146 | 146 | |
| @@ -156,26 +156,26 @@ discard block | ||
| 156 | 156 | |
| 157 | 157 | public function update(Entity $instance, $operations) | 
| 158 | 158 |      { | 
| 159 | -        if(!count($operations)) { | |
| 159 | +        if (!count($operations)) { | |
| 160 | 160 | return; | 
| 161 | 161 | } | 
| 162 | 162 | |
| 163 | 163 | $tupleOperations = []; | 
| 164 | -        foreach($operations as $operation) { | |
| 164 | +        foreach ($operations as $operation) { | |
| 165 | 165 | $tupleIndex = $this->space->getPropertyIndex($operation[1]); | 
| 166 | 166 | $tupleOperations[] = [$operation[0], $tupleIndex, $operation[2]]; | 
| 167 | 167 | } | 
| 168 | 168 | |
| 169 | 169 | $pk = []; | 
| 170 | -        foreach($this->space->getPrimaryIndex()->parts as $part) { | |
| 170 | +        foreach ($this->space->getPrimaryIndex()->parts as $part) { | |
| 171 | 171 |              $pk[] = $instance->{$this->space->getFormat()[$part[0]]['name']}; | 
| 172 | 172 | } | 
| 173 | 173 | |
| 174 | 174 | $client = $this->space->getMapper()->getClient(); | 
| 175 | 175 | $result = $client->getSpace($this->space->getId())->update($pk, $tupleOperations); | 
| 176 | -        foreach($result->getData() as $tuple) { | |
| 177 | -            foreach($this->space->getFormat() as $index => $info) { | |
| 178 | -                if(array_key_exists($index, $tuple)) { | |
| 176 | +        foreach ($result->getData() as $tuple) { | |
| 177 | +            foreach ($this->space->getFormat() as $index => $info) { | |
| 178 | +                if (array_key_exists($index, $tuple)) { | |
| 179 | 179 |                      $instance->{$info['name']} = $tuple[$index]; | 
| 180 | 180 | } | 
| 181 | 181 | } | 
| @@ -192,15 +192,15 @@ discard block | ||
| 192 | 192 | |
| 193 | 193 | public function remove($params = []) | 
| 194 | 194 |      { | 
| 195 | -        if($params instanceof Entity) { | |
| 195 | +        if ($params instanceof Entity) { | |
| 196 | 196 | return $this->removeEntity($params); | 
| 197 | 197 | } | 
| 198 | 198 | |
| 199 | -        if(!count($params)) { | |
| 199 | +        if (!count($params)) { | |
| 200 | 200 |              throw new Exception("Use truncate to flush space"); | 
| 201 | 201 | } | 
| 202 | 202 | |
| 203 | -        foreach($this->find($params) as $entity) { | |
| 203 | +        foreach ($this->find($params) as $entity) { | |
| 204 | 204 | $this->removeEntity($entity); | 
| 205 | 205 | } | 
| 206 | 206 | } | 
| @@ -209,19 +209,19 @@ discard block | ||
| 209 | 209 |      { | 
| 210 | 210 | $key = $this->space->getInstanceKey($instance); | 
| 211 | 211 | |
| 212 | -        if(!array_key_exists($key, $this->original)) { | |
| 212 | +        if (!array_key_exists($key, $this->original)) { | |
| 213 | 213 | return; | 
| 214 | 214 | } | 
| 215 | 215 | |
| 216 | -        if(array_key_exists($key, $this->persisted)) { | |
| 216 | +        if (array_key_exists($key, $this->persisted)) { | |
| 217 | 217 | |
| 218 | 218 | unset($this->persisted[$key]); | 
| 219 | 219 | |
| 220 | 220 | $pk = []; | 
| 221 | -            foreach($this->space->getPrimaryIndex()->parts as $part) { | |
| 221 | +            foreach ($this->space->getPrimaryIndex()->parts as $part) { | |
| 222 | 222 | $pk[] = $this->original[$key][$part[0]]; | 
| 223 | 223 | } | 
| 224 | -            foreach($this->space->getMapper()->getPlugins() as $plugin) { | |
| 224 | +            foreach ($this->space->getMapper()->getPlugins() as $plugin) { | |
| 225 | 225 | $plugin->beforeRemove($instance, $this->space); | 
| 226 | 226 | } | 
| 227 | 227 | |
| @@ -243,8 +243,8 @@ discard block | ||
| 243 | 243 | $size = count(get_object_vars($instance)); | 
| 244 | 244 | $skipped = 0; | 
| 245 | 245 | |
| 246 | -        foreach($this->space->getFormat() as $index => $info) { | |
| 247 | -            if(!property_exists($instance, $info['name'])) { | |
| 246 | +        foreach ($this->space->getFormat() as $index => $info) { | |
| 247 | +            if (!property_exists($instance, $info['name'])) { | |
| 248 | 248 | $skipped++; | 
| 249 | 249 |                  $instance->{$info['name']} = null; | 
| 250 | 250 | } | 
| @@ -253,7 +253,7 @@ discard block | ||
| 253 | 253 |                  ->formatValue($info['type'], $instance->{$info['name']}); | 
| 254 | 254 |              $tuple[$index] = $instance->{$info['name']}; | 
| 255 | 255 | |
| 256 | -            if(count($tuple) == $size + $skipped) { | |
| 256 | +            if (count($tuple) == $size + $skipped) { | |
| 257 | 257 | break; | 
| 258 | 258 | } | 
| 259 | 259 | } | 
| @@ -261,24 +261,24 @@ discard block | ||
| 261 | 261 | $key = $this->space->getInstanceKey($instance); | 
| 262 | 262 | $client = $this->space->getMapper()->getClient(); | 
| 263 | 263 | |
| 264 | -        if(array_key_exists($key, $this->persisted)) { | |
| 264 | +        if (array_key_exists($key, $this->persisted)) { | |
| 265 | 265 | // update | 
| 266 | 266 | $update = array_diff_assoc($tuple, $this->original[$key]); | 
| 267 | -            if(!count($update)) { | |
| 267 | +            if (!count($update)) { | |
| 268 | 268 | return $instance; | 
| 269 | 269 | } | 
| 270 | 270 | |
| 271 | 271 | $operations = []; | 
| 272 | -            foreach($update as $index => $value) { | |
| 272 | +            foreach ($update as $index => $value) { | |
| 273 | 273 | $operations[] = ['=', $index, $value]; | 
| 274 | 274 | } | 
| 275 | 275 | |
| 276 | 276 | $pk = []; | 
| 277 | -            foreach($this->space->getPrimaryIndex()->parts as $part) { | |
| 277 | +            foreach ($this->space->getPrimaryIndex()->parts as $part) { | |
| 278 | 278 | $pk[] = $this->original[$key][$part[0]]; | 
| 279 | 279 | } | 
| 280 | 280 | |
| 281 | -            foreach($this->space->getMapper()->getPlugins() as $plugin) { | |
| 281 | +            foreach ($this->space->getMapper()->getPlugins() as $plugin) { | |
| 282 | 282 | $plugin->beforeUpdate($instance, $this->space); | 
| 283 | 283 | } | 
| 284 | 284 | |
| @@ -287,7 +287,7 @@ discard block | ||
| 287 | 287 | |
| 288 | 288 |          } else { | 
| 289 | 289 | |
| 290 | -            foreach($this->space->getMapper()->getPlugins() as $plugin) { | |
| 290 | +            foreach ($this->space->getMapper()->getPlugins() as $plugin) { | |
| 291 | 291 | $plugin->beforeCreate($instance, $this->space); | 
| 292 | 292 | } | 
| 293 | 293 | |