@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | /** |
| 35 | 35 | * @return scalar[] |
| 36 | 36 | */ |
| 37 | - public function getAttributes (): array { |
|
| 37 | + public function getAttributes(): array { |
|
| 38 | 38 | return $this->attributes ?? []; |
| 39 | 39 | } |
| 40 | 40 | |
@@ -42,7 +42,7 @@ discard block |
||
| 42 | 42 | * @param mixed $attr |
| 43 | 43 | * @return bool |
| 44 | 44 | */ |
| 45 | - public function offsetExists ($attr): bool { |
|
| 45 | + public function offsetExists($attr): bool { |
|
| 46 | 46 | return isset($this->attributes[$attr]); |
| 47 | 47 | } |
| 48 | 48 | |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | * @param mixed $attr |
| 51 | 51 | * @return null|scalar |
| 52 | 52 | */ |
| 53 | - public function offsetGet ($attr) { |
|
| 53 | + public function offsetGet($attr) { |
|
| 54 | 54 | return $this->attributes[$attr] ?? null; |
| 55 | 55 | } |
| 56 | 56 | |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | * @param mixed $attr |
| 59 | 59 | * @param null|scalar $value |
| 60 | 60 | */ |
| 61 | - public function offsetSet ($attr, $value): void { |
|
| 61 | + public function offsetSet($attr, $value): void { |
|
| 62 | 62 | if (isset($attr)) { |
| 63 | 63 | if (isset($value)) { |
| 64 | 64 | assert(is_scalar($value)); |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | /** |
| 81 | 81 | * @param mixed $attr |
| 82 | 82 | */ |
| 83 | - public function offsetUnset ($attr): void { |
|
| 83 | + public function offsetUnset($attr): void { |
|
| 84 | 84 | unset($this->attributes[$attr]); |
| 85 | 85 | } |
| 86 | 86 | |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | * @param scalar[] $attributes |
| 89 | 89 | * @return $this |
| 90 | 90 | */ |
| 91 | - public function setAttributes (array $attributes) { |
|
| 91 | + public function setAttributes(array $attributes) { |
|
| 92 | 92 | $this->attributes = array_filter($attributes, 'is_scalar'); |
| 93 | 93 | return $this; |
| 94 | 94 | } |
@@ -63,12 +63,10 @@ |
||
| 63 | 63 | if (isset($value)) { |
| 64 | 64 | assert(is_scalar($value)); |
| 65 | 65 | $this->attributes[$attr] = $value; |
| 66 | - } |
|
| 67 | - else { |
|
| 66 | + } else { |
|
| 68 | 67 | $this->offsetUnset($attr); |
| 69 | 68 | } |
| 70 | - } |
|
| 71 | - else { |
|
| 69 | + } else { |
|
| 72 | 70 | // appending must not be null. |
| 73 | 71 | // even though missing numeric offsets would yield null when fetched individually, |
| 74 | 72 | // getAttributes() would not have them. |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | * @param string|EntityInterface $class Class or prototype instance. |
| 139 | 139 | * @return Record |
| 140 | 140 | */ |
| 141 | - public static function fromClass (DB $db, $class) { |
|
| 141 | + public static function fromClass(DB $db, $class) { |
|
| 142 | 142 | $rClass = new ReflectionClass($class); |
| 143 | 143 | assert($rClass->isInstantiable()); |
| 144 | 144 | $columns = []; |
@@ -169,7 +169,7 @@ discard block |
||
| 169 | 169 | * @param string[] $properties Property names. |
| 170 | 170 | * @param EAV[] $eav Keyed by property name. |
| 171 | 171 | */ |
| 172 | - public function __construct (DB $db, EntityInterface $proto, string $table, array $properties, array $eav = []) { |
|
| 172 | + public function __construct(DB $db, EntityInterface $proto, string $table, array $properties, array $eav = []) { |
|
| 173 | 173 | parent::__construct($db, $table, $properties); |
| 174 | 174 | $this->proto = $proto; |
| 175 | 175 | $this->utc = new DateTimeZone('UTC'); |
@@ -208,7 +208,7 @@ discard block |
||
| 208 | 208 | * @param ReflectionProperty $rProp |
| 209 | 209 | * @return null|string Storage type, or `null` for unknown. |
| 210 | 210 | */ |
| 211 | - protected function __construct_getType (string $prop, ReflectionProperty $rProp): ?string { |
|
| 211 | + protected function __construct_getType(string $prop, ReflectionProperty $rProp): ?string { |
|
| 212 | 212 | return $this->__construct_getType_fromReflection($prop, $rProp) |
| 213 | 213 | ?? $this->__construct_getType_fromVar($prop, $rProp); |
| 214 | 214 | } |
@@ -220,7 +220,7 @@ discard block |
||
| 220 | 220 | * @param ReflectionProperty $rProp |
| 221 | 221 | * @return null|string |
| 222 | 222 | */ |
| 223 | - protected function __construct_getType_fromReflection (string $prop, ReflectionProperty $rProp): ?string { |
|
| 223 | + protected function __construct_getType_fromReflection(string $prop, ReflectionProperty $rProp): ?string { |
|
| 224 | 224 | if ($rType = $rProp->getType() and $rType instanceof ReflectionNamedType) { |
| 225 | 225 | $type = $rType->getName(); |
| 226 | 226 | if (isset(static::SCALARS[$type])) { |
@@ -240,7 +240,7 @@ discard block |
||
| 240 | 240 | * @param ReflectionProperty $rProp |
| 241 | 241 | * @return null|string |
| 242 | 242 | */ |
| 243 | - protected function __construct_getType_fromVar (string $prop, ReflectionProperty $rProp): ?string { |
|
| 243 | + protected function __construct_getType_fromVar(string $prop, ReflectionProperty $rProp): ?string { |
|
| 244 | 244 | if (preg_match(static::RX_VAR, $rProp->getDocComment(), $var)) { |
| 245 | 245 | $type = preg_replace(static::RX_NULL, '', $var['type']); // remove null |
| 246 | 246 | if (isset(static::SCALARS[$type])) { |
@@ -266,7 +266,7 @@ discard block |
||
| 266 | 266 | * @param ReflectionProperty $rProp |
| 267 | 267 | * @return null|bool |
| 268 | 268 | */ |
| 269 | - protected function __construct_isNullable (string $prop, ReflectionProperty $rProp): ?bool { |
|
| 269 | + protected function __construct_isNullable(string $prop, ReflectionProperty $rProp): ?bool { |
|
| 270 | 270 | return $this->__construct_isNullable_fromReflection($prop, $rProp) |
| 271 | 271 | ?? $this->__construct_isNullable_fromVar($prop, $rProp); |
| 272 | 272 | } |
@@ -276,7 +276,7 @@ discard block |
||
| 276 | 276 | * @param ReflectionProperty $rProp |
| 277 | 277 | * @return null|bool |
| 278 | 278 | */ |
| 279 | - protected function __construct_isNullable_fromReflection (string $prop, ReflectionProperty $rProp): ?bool { |
|
| 279 | + protected function __construct_isNullable_fromReflection(string $prop, ReflectionProperty $rProp): ?bool { |
|
| 280 | 280 | if ($rType = $rProp->getType()) { |
| 281 | 281 | return $rType->allowsNull(); |
| 282 | 282 | } |
@@ -288,7 +288,7 @@ discard block |
||
| 288 | 288 | * @param ReflectionProperty $rProp |
| 289 | 289 | * @return null|bool |
| 290 | 290 | */ |
| 291 | - protected function __construct_isNullable_fromVar (string $prop, ReflectionProperty $rProp): ?bool { |
|
| 291 | + protected function __construct_isNullable_fromVar(string $prop, ReflectionProperty $rProp): ?bool { |
|
| 292 | 292 | if (preg_match(static::RX_VAR, $rProp->getDocComment(), $var)) { |
| 293 | 293 | preg_replace(static::RX_NULL, '', $var['type'], -1, $nullable); |
| 294 | 294 | return (bool)$nullable; |
@@ -302,7 +302,7 @@ discard block |
||
| 302 | 302 | * @param Statement $statement |
| 303 | 303 | * @return EntityInterface[] Keyed by ID |
| 304 | 304 | */ |
| 305 | - public function fetchAll (Statement $statement): array { |
|
| 305 | + public function fetchAll(Statement $statement): array { |
|
| 306 | 306 | return iterator_to_array($this->fetchEach($statement)); |
| 307 | 307 | } |
| 308 | 308 | |
@@ -313,7 +313,7 @@ discard block |
||
| 313 | 313 | * @param Statement $statement |
| 314 | 314 | * @return Generator|EntityInterface[] Keyed by ID |
| 315 | 315 | */ |
| 316 | - public function fetchEach (Statement $statement) { |
|
| 316 | + public function fetchEach(Statement $statement) { |
|
| 317 | 317 | do { |
| 318 | 318 | $entities = []; |
| 319 | 319 | for ($i = 0; $i < 256 and false !== $row = $statement->fetch(); $i++) { |
@@ -335,7 +335,7 @@ discard block |
||
| 335 | 335 | * @param array[] $eavMatch `[eav property => attribute => value]` |
| 336 | 336 | * @return Select|EntityInterface[] |
| 337 | 337 | */ |
| 338 | - public function findAll (array $match, array $eavMatch = []) { |
|
| 338 | + public function findAll(array $match, array $eavMatch = []) { |
|
| 339 | 339 | $select = $this->loadAll(); |
| 340 | 340 | foreach ($match as $a => $b) { |
| 341 | 341 | $select->where($this->db->match($this[$a] ?? $a, $b)); |
@@ -354,21 +354,21 @@ discard block |
||
| 354 | 354 | * @param array $eavMatch `[eav property => attribute => value]` |
| 355 | 355 | * @return null|EntityInterface |
| 356 | 356 | */ |
| 357 | - public function findFirst (array $match, array $eavMatch = []) { |
|
| 357 | + public function findFirst(array $match, array $eavMatch = []) { |
|
| 358 | 358 | return $this->findAll($match, $eavMatch)->limit(1)->getFirst(); |
| 359 | 359 | } |
| 360 | 360 | |
| 361 | 361 | /** |
| 362 | 362 | * @return string |
| 363 | 363 | */ |
| 364 | - final public function getClass (): string { |
|
| 364 | + final public function getClass(): string { |
|
| 365 | 365 | return get_class($this->proto); |
| 366 | 366 | } |
| 367 | 367 | |
| 368 | 368 | /** |
| 369 | 369 | * @return EAV[] |
| 370 | 370 | */ |
| 371 | - public function getEav () { |
|
| 371 | + public function getEav() { |
|
| 372 | 372 | return $this->eav; |
| 373 | 373 | } |
| 374 | 374 | |
@@ -377,14 +377,14 @@ discard block |
||
| 377 | 377 | * |
| 378 | 378 | * @return string[] |
| 379 | 379 | */ |
| 380 | - final public function getProperties (): array { |
|
| 380 | + final public function getProperties(): array { |
|
| 381 | 381 | return array_keys($this->properties); |
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | /** |
| 385 | 385 | * @return EntityInterface |
| 386 | 386 | */ |
| 387 | - public function getProto () { |
|
| 387 | + public function getProto() { |
|
| 388 | 388 | return $this->proto; |
| 389 | 389 | } |
| 390 | 390 | |
@@ -396,7 +396,7 @@ discard block |
||
| 396 | 396 | * @param string $property |
| 397 | 397 | * @return string |
| 398 | 398 | */ |
| 399 | - final public function getType (string $property): string { |
|
| 399 | + final public function getType(string $property): string { |
|
| 400 | 400 | return $this->types[$property]; |
| 401 | 401 | } |
| 402 | 402 | |
@@ -407,7 +407,7 @@ discard block |
||
| 407 | 407 | * |
| 408 | 408 | * @return string[] |
| 409 | 409 | */ |
| 410 | - final public function getTypes (): array { |
|
| 410 | + final public function getTypes(): array { |
|
| 411 | 411 | return $this->types; |
| 412 | 412 | } |
| 413 | 413 | |
@@ -415,7 +415,7 @@ discard block |
||
| 415 | 415 | * @param EntityInterface $entity |
| 416 | 416 | * @return array |
| 417 | 417 | */ |
| 418 | - protected function getValues (EntityInterface $entity): array { |
|
| 418 | + protected function getValues(EntityInterface $entity): array { |
|
| 419 | 419 | $values = []; |
| 420 | 420 | foreach (array_keys($this->columns) as $name) { |
| 421 | 421 | $value = $this->properties[$name]->getValue($entity); |
@@ -439,7 +439,7 @@ discard block |
||
| 439 | 439 | * @param array|object $hydrated |
| 440 | 440 | * @return scalar |
| 441 | 441 | */ |
| 442 | - protected function getValues_dehydrate (string $to, string $from, $hydrated) { |
|
| 442 | + protected function getValues_dehydrate(string $to, string $from, $hydrated) { |
|
| 443 | 443 | unset($from); // we don't need it here but it's given for posterity |
| 444 | 444 | switch ($to) { |
| 445 | 445 | case 'DateTime': |
@@ -454,7 +454,7 @@ discard block |
||
| 454 | 454 | * @param string $property |
| 455 | 455 | * @return bool |
| 456 | 456 | */ |
| 457 | - final public function isNullable (string $property): bool { |
|
| 457 | + final public function isNullable(string $property): bool { |
|
| 458 | 458 | return $this->nullable[$property]; |
| 459 | 459 | } |
| 460 | 460 | |
@@ -464,7 +464,7 @@ discard block |
||
| 464 | 464 | * @param int $id |
| 465 | 465 | * @return null|EntityInterface |
| 466 | 466 | */ |
| 467 | - public function load (int $id) { |
|
| 467 | + public function load(int $id) { |
|
| 468 | 468 | $statement = $this->cache(__FUNCTION__, function() { |
| 469 | 469 | return $this->select()->where('id = ?')->prepare(); |
| 470 | 470 | }); |
@@ -484,7 +484,7 @@ discard block |
||
| 484 | 484 | * |
| 485 | 485 | * @return Select|EntityInterface[] |
| 486 | 486 | */ |
| 487 | - public function loadAll () { |
|
| 487 | + public function loadAll() { |
|
| 488 | 488 | return $this->select()->setFetcher(function(Statement $statement) { |
| 489 | 489 | yield from $this->fetchEach($statement); |
| 490 | 490 | }); |
@@ -495,7 +495,7 @@ discard block |
||
| 495 | 495 | * |
| 496 | 496 | * @param EntityInterface[] $entities |
| 497 | 497 | */ |
| 498 | - protected function loadEav (array $entities): void { |
|
| 498 | + protected function loadEav(array $entities): void { |
|
| 499 | 499 | $ids = array_keys($entities); |
| 500 | 500 | foreach ($this->eav as $name => $eav) { |
| 501 | 501 | foreach ($eav->loadAll($ids) as $id => $values) { |
@@ -510,7 +510,7 @@ discard block |
||
| 510 | 510 | * @param EntityInterface $entity |
| 511 | 511 | * @return int ID |
| 512 | 512 | */ |
| 513 | - public function save (EntityInterface $entity): int { |
|
| 513 | + public function save(EntityInterface $entity): int { |
|
| 514 | 514 | if (!$entity->getId()) { |
| 515 | 515 | $this->saveInsert($entity); |
| 516 | 516 | } |
@@ -524,7 +524,7 @@ discard block |
||
| 524 | 524 | /** |
| 525 | 525 | * @param EntityInterface $entity |
| 526 | 526 | */ |
| 527 | - protected function saveEav (EntityInterface $entity): void { |
|
| 527 | + protected function saveEav(EntityInterface $entity): void { |
|
| 528 | 528 | $id = $entity->getId(); |
| 529 | 529 | foreach ($this->eav as $name => $eav) { |
| 530 | 530 | // may be null to skip |
@@ -540,7 +540,7 @@ discard block |
||
| 540 | 540 | * |
| 541 | 541 | * @param EntityInterface $entity |
| 542 | 542 | */ |
| 543 | - protected function saveInsert (EntityInterface $entity): void { |
|
| 543 | + protected function saveInsert(EntityInterface $entity): void { |
|
| 544 | 544 | $statement = $this->cache(__FUNCTION__, function() { |
| 545 | 545 | $slots = $this->db->slots(array_keys($this->columns)); |
| 546 | 546 | unset($slots['id']); |
@@ -559,7 +559,7 @@ discard block |
||
| 559 | 559 | * |
| 560 | 560 | * @param EntityInterface $entity |
| 561 | 561 | */ |
| 562 | - protected function saveUpdate (EntityInterface $entity): void { |
|
| 562 | + protected function saveUpdate(EntityInterface $entity): void { |
|
| 563 | 563 | $statement = $this->cache(__FUNCTION__, function() { |
| 564 | 564 | $slots = $this->db->slotsEqual(array_keys($this->columns)); |
| 565 | 565 | unset($slots['id']); |
@@ -574,7 +574,7 @@ discard block |
||
| 574 | 574 | * @param EntityInterface $proto |
| 575 | 575 | * @return $this |
| 576 | 576 | */ |
| 577 | - public function setProto (EntityInterface $proto) { |
|
| 577 | + public function setProto(EntityInterface $proto) { |
|
| 578 | 578 | $this->proto = $proto; |
| 579 | 579 | return $this; |
| 580 | 580 | } |
@@ -586,7 +586,7 @@ discard block |
||
| 586 | 586 | * @param mixed $value |
| 587 | 587 | * @return mixed |
| 588 | 588 | */ |
| 589 | - protected function setType (string $property, $value) { |
|
| 589 | + protected function setType(string $property, $value) { |
|
| 590 | 590 | if (isset($value)) { |
| 591 | 591 | // complex? |
| 592 | 592 | if (isset($this->hydration[$property])) { |
@@ -610,7 +610,7 @@ discard block |
||
| 610 | 610 | * @param scalar $dehydrated |
| 611 | 611 | * @return array|object |
| 612 | 612 | */ |
| 613 | - protected function setType_hydrate (string $to, string $from, $dehydrated) { |
|
| 613 | + protected function setType_hydrate(string $to, string $from, $dehydrated) { |
|
| 614 | 614 | switch ($from) { |
| 615 | 615 | case 'DateTime': |
| 616 | 616 | /** |
@@ -633,7 +633,7 @@ discard block |
||
| 633 | 633 | * @param EntityInterface $entity |
| 634 | 634 | * @param array $values |
| 635 | 635 | */ |
| 636 | - protected function setValues (EntityInterface $entity, array $values): void { |
|
| 636 | + protected function setValues(EntityInterface $entity, array $values): void { |
|
| 637 | 637 | foreach ($values as $name => $value) { |
| 638 | 638 | if (isset($this->properties[$name])) { |
| 639 | 639 | $this->properties[$name]->setValue($entity, $this->setType($name, $value)); |
@@ -147,8 +147,7 @@ discard block |
||
| 147 | 147 | $doc = $rProp->getDocComment(); |
| 148 | 148 | if (preg_match(static::RX_IS_COLUMN, $doc)) { |
| 149 | 149 | $columns[] = $rProp->getName(); |
| 150 | - } |
|
| 151 | - elseif (preg_match(static::RX_EAV, $doc, $eav)) { |
|
| 150 | + } elseif (preg_match(static::RX_EAV, $doc, $eav)) { |
|
| 152 | 151 | preg_match(static::RX_EAV_VAR, $doc, $var); |
| 153 | 152 | $type = $var['type'] ?? 'string'; |
| 154 | 153 | $type = static::SCALARS[$type] ?? 'string'; |
@@ -513,8 +512,7 @@ discard block |
||
| 513 | 512 | public function save (EntityInterface $entity): int { |
| 514 | 513 | if (!$entity->getId()) { |
| 515 | 514 | $this->saveInsert($entity); |
| 516 | - } |
|
| 517 | - else { |
|
| 515 | + } else { |
|
| 518 | 516 | $this->saveUpdate($entity); |
| 519 | 517 | } |
| 520 | 518 | $this->saveEav($entity); |
@@ -637,8 +635,7 @@ discard block |
||
| 637 | 635 | foreach ($values as $name => $value) { |
| 638 | 636 | if (isset($this->properties[$name])) { |
| 639 | 637 | $this->properties[$name]->setValue($entity, $this->setType($name, $value)); |
| 640 | - } |
|
| 641 | - else { |
|
| 638 | + } else { |
|
| 642 | 639 | // attempt to set unknown fields directly on the instance. |
| 643 | 640 | $entity->{$name} = $value; |
| 644 | 641 | } |
@@ -143,16 +143,16 @@ discard block |
||
| 143 | 143 | // bool |
| 144 | 144 | 'BOOLEAN' => 'bool', |
| 145 | 145 | // int |
| 146 | - 'BIGINT' => 'int', // mysql |
|
| 146 | + 'BIGINT' => 'int', // mysql |
|
| 147 | 147 | 'INTEGER' => 'int', // sqlite (must be this type to allow AUTOINCREMENT) |
| 148 | 148 | // float |
| 149 | 149 | 'DOUBLE PRECISION' => 'float', |
| 150 | 150 | // string <= 255 |
| 151 | 151 | 'VARCHAR(255)' => 'string', |
| 152 | 152 | // string <= 64k |
| 153 | - 'TEXT' => 'String', // @var String |
|
| 153 | + 'TEXT' => 'String', // @var String |
|
| 154 | 154 | // string > 64k |
| 155 | - 'BLOB' => 'STRING', // @var STRING (sqlite) |
|
| 155 | + 'BLOB' => 'STRING', // @var STRING (sqlite) |
|
| 156 | 156 | 'LONGBLOB' => 'STRING', // @var STRING (mysql) |
| 157 | 157 | // DateTime |
| 158 | 158 | 'DATETIME' => 'DateTime', |
@@ -220,7 +220,7 @@ discard block |
||
| 220 | 220 | /** |
| 221 | 221 | * @param DB $db |
| 222 | 222 | */ |
| 223 | - public function __construct (DB $db) { |
|
| 223 | + public function __construct(DB $db) { |
|
| 224 | 224 | $this->db = $db; |
| 225 | 225 | $this->colDefs ??= self::COLUMN_DEFINITIONS[$db->getDriver()]; |
| 226 | 226 | } |
@@ -233,7 +233,7 @@ discard block |
||
| 233 | 233 | * @param int $type |
| 234 | 234 | * @return $this |
| 235 | 235 | */ |
| 236 | - public function addColumn (string $table, string $column, int $type = self::T_STRING_NULL) { |
|
| 236 | + public function addColumn(string $table, string $column, int $type = self::T_STRING_NULL) { |
|
| 237 | 237 | $type = $this->colDefs[$type & self::T_MASK]; |
| 238 | 238 | $this->db->exec("ALTER TABLE {$table} ADD COLUMN {$column} {$type}"); |
| 239 | 239 | unset($this->tables[$table]); |
@@ -259,7 +259,7 @@ discard block |
||
| 259 | 259 | * @param array[] $constraints `[ <TABLE_CONST> => spec ]` |
| 260 | 260 | * @return $this |
| 261 | 261 | */ |
| 262 | - public function createTable (string $table, array $columns, array $constraints = []) { |
|
| 262 | + public function createTable(string $table, array $columns, array $constraints = []) { |
|
| 263 | 263 | $defs = $this->toColumnDefinitions($columns); |
| 264 | 264 | |
| 265 | 265 | /** @var string[] $pk */ |
@@ -295,7 +295,7 @@ discard block |
||
| 295 | 295 | * @param string $column |
| 296 | 296 | * @return $this |
| 297 | 297 | */ |
| 298 | - public function dropColumn (string $table, string $column) { |
|
| 298 | + public function dropColumn(string $table, string $column) { |
|
| 299 | 299 | $this->db->exec("ALTER TABLE {$table} DROP COLUMN {$column}"); |
| 300 | 300 | unset($this->tables[$table]); |
| 301 | 301 | return $this; |
@@ -306,7 +306,7 @@ discard block |
||
| 306 | 306 | * |
| 307 | 307 | * @param string $table |
| 308 | 308 | */ |
| 309 | - public function dropTable (string $table): void { |
|
| 309 | + public function dropTable(string $table): void { |
|
| 310 | 310 | $this->db->exec("DROP TABLE IF EXISTS {$table}"); |
| 311 | 311 | unset($this->tables[$table]); |
| 312 | 312 | } |
@@ -325,7 +325,7 @@ discard block |
||
| 325 | 325 | * @param string $column |
| 326 | 326 | * @return array[] Keyed by name. |
| 327 | 327 | */ |
| 328 | - public function getColumnInfo (string $table): array { |
|
| 328 | + public function getColumnInfo(string $table): array { |
|
| 329 | 329 | if ($this->db->isSQLite()) { |
| 330 | 330 | $info = $this->db->query("PRAGMA table_info({$table})")->fetchAll(); |
| 331 | 331 | return array_combine(array_column($info, 'name'), array_map(fn(array $each) => [ |
@@ -345,7 +345,7 @@ discard block |
||
| 345 | 345 | /** |
| 346 | 346 | * @return DB |
| 347 | 347 | */ |
| 348 | - public function getDb () { |
|
| 348 | + public function getDb() { |
|
| 349 | 349 | return $this->db; |
| 350 | 350 | } |
| 351 | 351 | |
@@ -353,7 +353,7 @@ discard block |
||
| 353 | 353 | * @param string $name |
| 354 | 354 | * @return null|Table |
| 355 | 355 | */ |
| 356 | - public function getTable (string $name) { |
|
| 356 | + public function getTable(string $name) { |
|
| 357 | 357 | if (!isset($this->tables[$name])) { |
| 358 | 358 | if ($this->db->isSQLite()) { |
| 359 | 359 | $info = $this->db->query("PRAGMA table_info({$name})")->fetchAll(); |
@@ -376,7 +376,7 @@ discard block |
||
| 376 | 376 | * @param string $table |
| 377 | 377 | * @return bool |
| 378 | 378 | */ |
| 379 | - final public function offsetExists ($table): bool { |
|
| 379 | + final public function offsetExists($table): bool { |
|
| 380 | 380 | return (bool)$this->offsetGet($table); |
| 381 | 381 | } |
| 382 | 382 | |
@@ -386,7 +386,7 @@ discard block |
||
| 386 | 386 | * @param string $table |
| 387 | 387 | * @return null|Table |
| 388 | 388 | */ |
| 389 | - public function offsetGet ($table) { |
|
| 389 | + public function offsetGet($table) { |
|
| 390 | 390 | return $this->getTable($table); |
| 391 | 391 | } |
| 392 | 392 | |
@@ -395,7 +395,7 @@ discard block |
||
| 395 | 395 | * @param $value |
| 396 | 396 | * @throws LogicException |
| 397 | 397 | */ |
| 398 | - final public function offsetSet ($offset, $value) { |
|
| 398 | + final public function offsetSet($offset, $value) { |
|
| 399 | 399 | throw new LogicException('The schema cannot be altered this way.'); |
| 400 | 400 | } |
| 401 | 401 | |
@@ -403,7 +403,7 @@ discard block |
||
| 403 | 403 | * @param $offset |
| 404 | 404 | * @throws LogicException |
| 405 | 405 | */ |
| 406 | - final public function offsetUnset ($offset) { |
|
| 406 | + final public function offsetUnset($offset) { |
|
| 407 | 407 | throw new LogicException('The schema cannot be altered this way.'); |
| 408 | 408 | } |
| 409 | 409 | |
@@ -415,7 +415,7 @@ discard block |
||
| 415 | 415 | * @param string $newName |
| 416 | 416 | * @return $this |
| 417 | 417 | */ |
| 418 | - public function renameColumn (string $table, string $oldName, string $newName) { |
|
| 418 | + public function renameColumn(string $table, string $oldName, string $newName) { |
|
| 419 | 419 | $this->db->exec("ALTER TABLE {$table} RENAME COLUMN {$oldName} TO {$newName}"); |
| 420 | 420 | unset($this->tables[$table]); |
| 421 | 421 | return $this; |
@@ -428,7 +428,7 @@ discard block |
||
| 428 | 428 | * @param string $newName |
| 429 | 429 | * @return $this |
| 430 | 430 | */ |
| 431 | - public function renameTable (string $oldName, string $newName) { |
|
| 431 | + public function renameTable(string $oldName, string $newName) { |
|
| 432 | 432 | $this->db->exec("ALTER TABLE {$oldName} RENAME TO {$newName}"); |
| 433 | 433 | unset($this->tables[$oldName]); |
| 434 | 434 | return $this; |
@@ -440,7 +440,7 @@ discard block |
||
| 440 | 440 | * @param int[] $types |
| 441 | 441 | * @return int[] |
| 442 | 442 | */ |
| 443 | - protected function sortColumns (array $types): array { |
|
| 443 | + protected function sortColumns(array $types): array { |
|
| 444 | 444 | uksort($types, function(string $a, string $b) use ($types) { |
| 445 | 445 | // descending type constant, ascending name |
| 446 | 446 | return $types[$b] <=> $types[$a] ?: $a <=> $b; |
@@ -452,7 +452,7 @@ discard block |
||
| 452 | 452 | * @param int[] $columns `[ name => <I_CONST> | <T_CONST> ]` |
| 453 | 453 | * @return string[] |
| 454 | 454 | */ |
| 455 | - protected function toColumnDefinitions (array $columns): array { |
|
| 455 | + protected function toColumnDefinitions(array $columns): array { |
|
| 456 | 456 | assert(count($columns) > 0); |
| 457 | 457 | $columns = $this->sortColumns($columns); |
| 458 | 458 | $defs = []; |
@@ -478,7 +478,7 @@ discard block |
||
| 478 | 478 | * @param Column $foreign |
| 479 | 479 | * @return string |
| 480 | 480 | */ |
| 481 | - protected function toForeignKeyConstraint (string $table, string $local, int $type, Column $foreign): string { |
|
| 481 | + protected function toForeignKeyConstraint(string $table, string $local, int $type, Column $foreign): string { |
|
| 482 | 482 | return sprintf( |
| 483 | 483 | 'CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s(%s) ON UPDATE CASCADE ON DELETE %s', |
| 484 | 484 | $this->toForeignKeyConstraint_name($table, $local), |
@@ -496,7 +496,7 @@ discard block |
||
| 496 | 496 | * @param string $column |
| 497 | 497 | * @return string |
| 498 | 498 | */ |
| 499 | - protected function toForeignKeyConstraint_name (string $table, string $column): string { |
|
| 499 | + protected function toForeignKeyConstraint_name(string $table, string $column): string { |
|
| 500 | 500 | return 'FK_' . $table . '__' . $column; |
| 501 | 501 | } |
| 502 | 502 | |
@@ -505,7 +505,7 @@ discard block |
||
| 505 | 505 | * @param string[] $columns |
| 506 | 506 | * @return string |
| 507 | 507 | */ |
| 508 | - protected function toPrimaryKeyConstraint (string $table, array $columns): string { |
|
| 508 | + protected function toPrimaryKeyConstraint(string $table, array $columns): string { |
|
| 509 | 509 | return sprintf( |
| 510 | 510 | 'CONSTRAINT %s PRIMARY KEY (%s)', |
| 511 | 511 | $this->toPrimaryKeyConstraint_name($table, $columns), |
@@ -520,7 +520,7 @@ discard block |
||
| 520 | 520 | * @param string[] $columns |
| 521 | 521 | * @return string |
| 522 | 522 | */ |
| 523 | - protected function toPrimaryKeyConstraint_name (string $table, array $columns): string { |
|
| 523 | + protected function toPrimaryKeyConstraint_name(string $table, array $columns): string { |
|
| 524 | 524 | sort($columns, SORT_STRING); |
| 525 | 525 | return 'PK_' . $table . '__' . implode('__', $columns); |
| 526 | 526 | } |
@@ -530,7 +530,7 @@ discard block |
||
| 530 | 530 | * @param string[] $columns |
| 531 | 531 | * @return string |
| 532 | 532 | */ |
| 533 | - protected function toUniqueKeyConstraint (string $table, array $columns): string { |
|
| 533 | + protected function toUniqueKeyConstraint(string $table, array $columns): string { |
|
| 534 | 534 | return sprintf( |
| 535 | 535 | 'CONSTRAINT %s UNIQUE (%s)', |
| 536 | 536 | $this->toUniqueKeyConstraint_name($table, $columns), |
@@ -545,7 +545,7 @@ discard block |
||
| 545 | 545 | * @param string[] $columns |
| 546 | 546 | * @return string |
| 547 | 547 | */ |
| 548 | - protected function toUniqueKeyConstraint_name (string $table, array $columns): string { |
|
| 548 | + protected function toUniqueKeyConstraint_name(string $table, array $columns): string { |
|
| 549 | 549 | sort($columns, SORT_STRING); |
| 550 | 550 | return 'UQ_' . $table . '__' . implode('__', $columns); |
| 551 | 551 | } |
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | * @param string $name |
| 24 | 24 | * @param string $type |
| 25 | 25 | */ |
| 26 | - public function __construct (DB $db, string $name, string $type = 'string') { |
|
| 26 | + public function __construct(DB $db, string $name, string $type = 'string') { |
|
| 27 | 27 | parent::__construct($db, $name, ['entity', 'attribute', 'value']); |
| 28 | 28 | $this->type = $type; |
| 29 | 29 | } |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | * @param string $attribute |
| 36 | 36 | * @return bool |
| 37 | 37 | */ |
| 38 | - public function exists (int $id, string $attribute): bool { |
|
| 38 | + public function exists(int $id, string $attribute): bool { |
|
| 39 | 39 | $statement = $this->cache(__FUNCTION__, function() { |
| 40 | 40 | return $this->select(['COUNT(*) > 0'])->where('entity = ? AND attribute = ?')->prepare(); |
| 41 | 41 | }); |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | * @param array $match `[attribute => value]`. If empty, selects all IDs for entities having at least one attribute. |
| 54 | 54 | * @return Select |
| 55 | 55 | */ |
| 56 | - public function findAll (array $match) { |
|
| 56 | + public function findAll(array $match) { |
|
| 57 | 57 | $select = $this->select([$this['entity']]); |
| 58 | 58 | $prior = $this; |
| 59 | 59 | foreach ($match as $attribute => $value) { |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | /** |
| 73 | 73 | * @return string |
| 74 | 74 | */ |
| 75 | - final public function getType (): string { |
|
| 75 | + final public function getType(): string { |
|
| 76 | 76 | return $this->type; |
| 77 | 77 | } |
| 78 | 78 | |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | * @param int $id |
| 83 | 83 | * @return scalar[] `[attribute => value]` |
| 84 | 84 | */ |
| 85 | - public function load (int $id): array { |
|
| 85 | + public function load(int $id): array { |
|
| 86 | 86 | $statement = $this->cache(__FUNCTION__, function() { |
| 87 | 87 | $select = $this->select(['attribute', 'value']); |
| 88 | 88 | $select->where('entity = ?'); |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | * @param int[] $ids |
| 99 | 99 | * @return scalar[][] `[id => attribute => value] |
| 100 | 100 | */ |
| 101 | - public function loadAll (array $ids): array { |
|
| 101 | + public function loadAll(array $ids): array { |
|
| 102 | 102 | if (empty($ids)) { |
| 103 | 103 | return []; |
| 104 | 104 | } |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | * @param array $values `[attribute => value]` |
| 127 | 127 | * @return $this |
| 128 | 128 | */ |
| 129 | - public function save (int $id, array $values) { |
|
| 129 | + public function save(int $id, array $values) { |
|
| 130 | 130 | // delete stale |
| 131 | 131 | $this->delete([ |
| 132 | 132 | $this['entity']->isEqual($id), |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | * @param scalar $value |
| 167 | 167 | * @return scalar |
| 168 | 168 | */ |
| 169 | - protected function setType ($value) { |
|
| 169 | + protected function setType($value) { |
|
| 170 | 170 | settype($value, $this->type); |
| 171 | 171 | return $value; |
| 172 | 172 | } |