@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | * @param string $interface |
| 29 | 29 | * @return Junction |
| 30 | 30 | */ |
| 31 | - public static function fromInterface (DB $db, string $interface) { |
|
| 31 | + public static function fromInterface(DB $db, string $interface) { |
|
| 32 | 32 | try { |
| 33 | 33 | $ref = new ReflectionClass($interface); |
| 34 | 34 | } |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | * @param string $table |
| 52 | 52 | * @param string[] $keys Columns, keyed by class. |
| 53 | 53 | */ |
| 54 | - public function __construct (DB $db, string $table, array $keys) { |
|
| 54 | + public function __construct(DB $db, string $table, array $keys) { |
|
| 55 | 55 | parent::__construct($db, $table, $keys); |
| 56 | 56 | $this->keys = $keys; |
| 57 | 57 | } |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | * @param EntityInterface $entity |
| 63 | 63 | * @return int |
| 64 | 64 | */ |
| 65 | - public function count (EntityInterface $entity): int { |
|
| 65 | + public function count(EntityInterface $entity): int { |
|
| 66 | 66 | $key = $this->getKey($entity); |
| 67 | 67 | $count = $this->cache("count.{$key}", function() use ($key) { |
| 68 | 68 | return $this->select(['COUNT(*)'])->where("{$key} = ?")->prepare(); |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | * @param string $class |
| 80 | 80 | * @return Select |
| 81 | 81 | */ |
| 82 | - public function getCollection (EntityInterface $owner, string $class): Select { |
|
| 82 | + public function getCollection(EntityInterface $owner, string $class): Select { |
|
| 83 | 83 | $record = $this->db->getRecord($class); |
| 84 | 84 | $select = $record->select(); |
| 85 | 85 | $select->join($this, $this[$class]->isEqual($record['id'])); |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | * @param EntityInterface|string $class |
| 94 | 94 | * @return string |
| 95 | 95 | */ |
| 96 | - public function getKey ($class): string { |
|
| 96 | + public function getKey($class): string { |
|
| 97 | 97 | if (is_object($class)) { |
| 98 | 98 | $class = get_class($class); |
| 99 | 99 | } |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | * @param EntityInterface[] $entities |
| 112 | 112 | * @return int Rows affected. |
| 113 | 113 | */ |
| 114 | - public function link (array $entities): int { |
|
| 114 | + public function link(array $entities): int { |
|
| 115 | 115 | $link = $this->cache(__FUNCTION__, function() { |
| 116 | 116 | $slots = implode(',', SQL::slots($this->keys)); |
| 117 | 117 | $columns = implode(',', $this->keys); |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | * @param string $name |
| 134 | 134 | * @return bool |
| 135 | 135 | */ |
| 136 | - public function offsetExists ($name): bool { |
|
| 136 | + public function offsetExists($name): bool { |
|
| 137 | 137 | return isset($this->columns[$name]) or isset($this->keys[$name]); |
| 138 | 138 | } |
| 139 | 139 | |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | * @param string $name |
| 144 | 144 | * @return Column |
| 145 | 145 | */ |
| 146 | - public function offsetGet ($name): Column { |
|
| 146 | + public function offsetGet($name): Column { |
|
| 147 | 147 | return $this->columns[$name] ?? $this->columns[$this->keys[$name]]; |
| 148 | 148 | } |
| 149 | 149 | |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | * @param EntityInterface[] $entities |
| 157 | 157 | * @return int Rows affected. |
| 158 | 158 | */ |
| 159 | - public function unlink (array $entities): int { |
|
| 159 | + public function unlink(array $entities): int { |
|
| 160 | 160 | $unlink = $this->cache(__FUNCTION__, function() { |
| 161 | 161 | $slots = Predicate::all(SQL::slotsEqual($this->keys)); |
| 162 | 162 | return $this->db->prepare("DELETE FROM {$this} WHERE {$slots}"); |
@@ -31,8 +31,7 @@ |
||
| 31 | 31 | public static function fromInterface (DB $db, string $interface) { |
| 32 | 32 | try { |
| 33 | 33 | $ref = new ReflectionClass($interface); |
| 34 | - } |
|
| 35 | - catch (ReflectionException $exception) { |
|
| 34 | + } catch (ReflectionException $exception) { |
|
| 36 | 35 | throw new LogicException('Unexpected ReflectionException', 0, $exception); |
| 37 | 36 | } |
| 38 | 37 | $doc = $ref->getDocComment(); |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | * @param string|EntityInterface $class |
| 60 | 60 | * @return Record |
| 61 | 61 | */ |
| 62 | - public static function fromClass (DB $db, $class) { |
|
| 62 | + public static function fromClass(DB $db, $class) { |
|
| 63 | 63 | try { |
| 64 | 64 | $rClass = new ReflectionClass($class); |
| 65 | 65 | } |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | * @param string[] $columns Property names. |
| 93 | 93 | * @param string[] $eav Table names, keyed by property name. |
| 94 | 94 | */ |
| 95 | - public function __construct (DB $db, EntityInterface $proto, string $table, array $columns, array $eav = []) { |
|
| 95 | + public function __construct(DB $db, EntityInterface $proto, string $table, array $columns, array $eav = []) { |
|
| 96 | 96 | parent::__construct($db, $table, $columns); |
| 97 | 97 | $this->proto = $proto; |
| 98 | 98 | try { |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | * @param Statement $statement |
| 126 | 126 | * @return EntityInterface[] Enumerated |
| 127 | 127 | */ |
| 128 | - public function fetchAll (Statement $statement): array { |
|
| 128 | + public function fetchAll(Statement $statement): array { |
|
| 129 | 129 | return iterator_to_array($this->fetchEach($statement)); |
| 130 | 130 | } |
| 131 | 131 | |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | * @param Statement $statement |
| 137 | 137 | * @return Generator |
| 138 | 138 | */ |
| 139 | - public function fetchEach (Statement $statement) { |
|
| 139 | + public function fetchEach(Statement $statement) { |
|
| 140 | 140 | do { |
| 141 | 141 | $entities = []; |
| 142 | 142 | for ($i = 0; $i < 256 and false !== $row = $statement->fetch(); $i++) { |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | * @param array[] $eavMatch Additional `[eav property => attribute => mixed]` |
| 160 | 160 | * @return Select |
| 161 | 161 | */ |
| 162 | - public function find (array $match, array $eavMatch = []): Select { |
|
| 162 | + public function find(array $match, array $eavMatch = []): Select { |
|
| 163 | 163 | $select = $this->select(); |
| 164 | 164 | foreach ($match as $column => $value) { |
| 165 | 165 | $select->where($this->db->match($column, $value)); |
@@ -175,14 +175,14 @@ discard block |
||
| 175 | 175 | * @param string $property |
| 176 | 176 | * @return EAV |
| 177 | 177 | */ |
| 178 | - final public function getEav (string $property): EAV { |
|
| 178 | + final public function getEav(string $property): EAV { |
|
| 179 | 179 | return $this->eav[$property]; |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | /** |
| 183 | 183 | * @return EntityInterface |
| 184 | 184 | */ |
| 185 | - public function getProto (): EntityInterface { |
|
| 185 | + public function getProto(): EntityInterface { |
|
| 186 | 186 | return $this->proto; |
| 187 | 187 | } |
| 188 | 188 | |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | * @param EntityInterface $entity |
| 191 | 191 | * @return array |
| 192 | 192 | */ |
| 193 | - protected function getValues (EntityInterface $entity): array { |
|
| 193 | + protected function getValues(EntityInterface $entity): array { |
|
| 194 | 194 | $values = []; |
| 195 | 195 | foreach (array_keys($this->columns) as $name) { |
| 196 | 196 | $values[$name] = $this->properties[$name]->getValue($entity); |
@@ -204,7 +204,7 @@ discard block |
||
| 204 | 204 | * @param int $id |
| 205 | 205 | * @return null|EntityInterface |
| 206 | 206 | */ |
| 207 | - public function load (int $id): ?EntityInterface { |
|
| 207 | + public function load(int $id): ?EntityInterface { |
|
| 208 | 208 | $load = $this->cache(__FUNCTION__, function() { |
| 209 | 209 | return $this->select(array_keys($this->columns))->where('id = ?')->prepare(); |
| 210 | 210 | }); |
@@ -222,7 +222,7 @@ discard block |
||
| 222 | 222 | * |
| 223 | 223 | * @param EntityInterface[] $entities |
| 224 | 224 | */ |
| 225 | - protected function loadEav (array $entities): void { |
|
| 225 | + protected function loadEav(array $entities): void { |
|
| 226 | 226 | $ids = array_keys($entities); |
| 227 | 227 | foreach ($this->eav as $name => $eav) { |
| 228 | 228 | foreach ($eav->loadAll($ids) as $id => $values) { |
@@ -239,7 +239,7 @@ discard block |
||
| 239 | 239 | * @param EntityInterface $entity |
| 240 | 240 | * @return int ID |
| 241 | 241 | */ |
| 242 | - public function save (EntityInterface $entity): int { |
|
| 242 | + public function save(EntityInterface $entity): int { |
|
| 243 | 243 | if (!$entity->getId()) { |
| 244 | 244 | $this->saveInsert($entity); |
| 245 | 245 | } |
@@ -253,7 +253,7 @@ discard block |
||
| 253 | 253 | /** |
| 254 | 254 | * @param EntityInterface $entity |
| 255 | 255 | */ |
| 256 | - protected function saveEav (EntityInterface $entity): void { |
|
| 256 | + protected function saveEav(EntityInterface $entity): void { |
|
| 257 | 257 | $id = $entity->getId(); |
| 258 | 258 | foreach ($this->eav as $name => $eav) { |
| 259 | 259 | $values = $this->properties[$name]->getValue($entity); |
@@ -268,7 +268,7 @@ discard block |
||
| 268 | 268 | * |
| 269 | 269 | * @param EntityInterface $entity |
| 270 | 270 | */ |
| 271 | - protected function saveInsert (EntityInterface $entity): void { |
|
| 271 | + protected function saveInsert(EntityInterface $entity): void { |
|
| 272 | 272 | $insert = $this->cache(__FUNCTION__, function() { |
| 273 | 273 | $slots = SQL::slots(array_keys($this->columns)); |
| 274 | 274 | unset($slots['id']); |
@@ -286,7 +286,7 @@ discard block |
||
| 286 | 286 | * |
| 287 | 287 | * @param EntityInterface $entity |
| 288 | 288 | */ |
| 289 | - protected function saveUpdate (EntityInterface $entity): void { |
|
| 289 | + protected function saveUpdate(EntityInterface $entity): void { |
|
| 290 | 290 | $this->cache(__FUNCTION__, function() { |
| 291 | 291 | $slots = SQL::slotsEqual(array_keys($this->columns)); |
| 292 | 292 | unset($slots['id']); |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | * @param array $columns Defaults to all columns. |
| 302 | 302 | * @return Select |
| 303 | 303 | */ |
| 304 | - public function select (array $columns = []): Select { |
|
| 304 | + public function select(array $columns = []): Select { |
|
| 305 | 305 | return parent::select($columns)->setFetcher(function(Statement $statement) { |
| 306 | 306 | yield from $this->fetchEach($statement); |
| 307 | 307 | }); |
@@ -311,7 +311,7 @@ discard block |
||
| 311 | 311 | * @param EntityInterface $proto |
| 312 | 312 | * @return $this |
| 313 | 313 | */ |
| 314 | - public function setProto (EntityInterface $proto) { |
|
| 314 | + public function setProto(EntityInterface $proto) { |
|
| 315 | 315 | $this->proto = $proto; |
| 316 | 316 | return $this; |
| 317 | 317 | } |
@@ -320,7 +320,7 @@ discard block |
||
| 320 | 320 | * @param EntityInterface $entity |
| 321 | 321 | * @param array $values |
| 322 | 322 | */ |
| 323 | - protected function setValues (EntityInterface $entity, array $values): void { |
|
| 323 | + protected function setValues(EntityInterface $entity, array $values): void { |
|
| 324 | 324 | foreach ($values as $name => $value) { |
| 325 | 325 | settype($value, $this->types[$name]); |
| 326 | 326 | $this->properties[$name]->setValue($entity, $value); |
@@ -62,8 +62,7 @@ discard block |
||
| 62 | 62 | public static function fromClass (DB $db, $class) { |
| 63 | 63 | try { |
| 64 | 64 | $rClass = new ReflectionClass($class); |
| 65 | - } |
|
| 66 | - catch (ReflectionException $exception) { |
|
| 65 | + } catch (ReflectionException $exception) { |
|
| 67 | 66 | throw new LogicException('Unexpected ReflectionException', 0, $exception); |
| 68 | 67 | } |
| 69 | 68 | $columns = []; |
@@ -73,8 +72,7 @@ discard block |
||
| 73 | 72 | $name = $rProp->getName(); |
| 74 | 73 | if (preg_match('/@col(umn)?[\s$]/', $doc)) { |
| 75 | 74 | $columns[] = $name; |
| 76 | - } |
|
| 77 | - elseif (preg_match('/@eav\s+(?<table>\S+)/', $doc, $match)) { |
|
| 75 | + } elseif (preg_match('/@eav\s+(?<table>\S+)/', $doc, $match)) { |
|
| 78 | 76 | $eav[$name] = $match['table']; |
| 79 | 77 | } |
| 80 | 78 | } |
@@ -113,8 +111,7 @@ discard block |
||
| 113 | 111 | $this->properties[$name] = $rProp; |
| 114 | 112 | $this->eav[$name] = new EAV($this, $table); |
| 115 | 113 | } |
| 116 | - } |
|
| 117 | - catch (ReflectionException $exception) { |
|
| 114 | + } catch (ReflectionException $exception) { |
|
| 118 | 115 | throw new LogicException('Unexpected ReflectionException', 0, $exception); |
| 119 | 116 | } |
| 120 | 117 | } |
@@ -242,8 +239,7 @@ discard block |
||
| 242 | 239 | public function save (EntityInterface $entity): int { |
| 243 | 240 | if (!$entity->getId()) { |
| 244 | 241 | $this->saveInsert($entity); |
| 245 | - } |
|
| 246 | - else { |
|
| 242 | + } else { |
|
| 247 | 243 | $this->saveUpdate($entity); |
| 248 | 244 | } |
| 249 | 245 | $this->saveEav($entity); |