@@ -14,17 +14,17 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | public function __construct(array $spec, array $sortFieldsSpec) { |
| 16 | 16 | $expressions = []; |
| 17 | - foreach($spec as $specReference => $dbExpr) { |
|
| 18 | - if(is_int($specReference)) { |
|
| 17 | + foreach ($spec as $specReference => $dbExpr) { |
|
| 18 | + if (is_int($specReference)) { |
|
| 19 | 19 | $specReference = $dbExpr; |
| 20 | 20 | } |
| 21 | 21 | $expressions[$specReference] = $dbExpr; |
| 22 | 22 | } |
| 23 | - foreach($sortFieldsSpec as $sortFieldSpec) { |
|
| 24 | - if(array_key_exists(0, $sortFieldSpec)) { |
|
| 25 | - if(array_key_exists($sortFieldSpec[0], $expressions)) { |
|
| 23 | + foreach ($sortFieldsSpec as $sortFieldSpec) { |
|
| 24 | + if (array_key_exists(0, $sortFieldSpec)) { |
|
| 25 | + if (array_key_exists($sortFieldSpec[0], $expressions)) { |
|
| 26 | 26 | $direction = 'ASC'; |
| 27 | - if(array_key_exists(1, $sortFieldSpec) && strtoupper($sortFieldSpec[1]) !== 'ASC') { |
|
| 27 | + if (array_key_exists(1, $sortFieldSpec) && strtoupper($sortFieldSpec[1]) !== 'ASC') { |
|
| 28 | 28 | $direction = 'DESC'; |
| 29 | 29 | } |
| 30 | 30 | $this->fields[] = [ |
@@ -33,9 +33,9 @@ discard block |
||
| 33 | 33 | ]; |
| 34 | 34 | } |
| 35 | 35 | } else { // @phpstan-ignore-line |
| 36 | - foreach($sortFieldSpec as $alias => $direction) { |
|
| 36 | + foreach ($sortFieldSpec as $alias => $direction) { |
|
| 37 | 37 | $direction = strtoupper($direction) === 'DESC' ? 'DESC' : 'ASC'; |
| 38 | - if(array_key_exists($alias, $expressions)) { |
|
| 38 | + if (array_key_exists($alias, $expressions)) { |
|
| 39 | 39 | $this->fields[] = [$expressions[$alias], $direction]; |
| 40 | 40 | } |
| 41 | 41 | } |
@@ -36,7 +36,7 @@ |
||
| 36 | 36 | * @return DBExprFilter |
| 37 | 37 | */ |
| 38 | 38 | public function __invoke(string $expression, $keyPath, $validator = null) { |
| 39 | - if(!RecursiveStructureAccess::recursiveHas($this->map, $keyPath)) { |
|
| 39 | + if (!RecursiveStructureAccess::recursiveHas($this->map, $keyPath)) { |
|
| 40 | 40 | throw new RequiredValueNotFoundException(sprintf("Required value %s not found", json_encode($keyPath, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE))); |
| 41 | 41 | } |
| 42 | 42 | return new DBExprFilter($expression, $this->map, $keyPath, $validator); |
@@ -41,13 +41,13 @@ |
||
| 41 | 41 | public function getFields(): array { |
| 42 | 42 | $fields = []; |
| 43 | 43 | $max = $this->options['max_sort_instructions'] ?? 16; |
| 44 | - foreach($this->sortingInstruction as $alias => $direction) { |
|
| 44 | + foreach ($this->sortingInstruction as $alias => $direction) { |
|
| 45 | 45 | $direction = strtolower($direction) === 'desc' ? 'DESC' : 'ASC'; |
| 46 | - if(!array_key_exists($alias, $this->sortSpecification)) { |
|
| 46 | + if (!array_key_exists($alias, $this->sortSpecification)) { |
|
| 47 | 47 | throw new DBSortAliasNotFoundException('Sorting: Alias for DB-Expression not found'); |
| 48 | 48 | } |
| 49 | 49 | $fields[] = [$this->sortSpecification[$alias], $direction]; |
| 50 | - if($max < 1) { |
|
| 50 | + if ($max < 1) { |
|
| 51 | 51 | throw new DBSortTooManyInstructionsException(); |
| 52 | 52 | } |
| 53 | 53 | $max--; |
@@ -28,9 +28,9 @@ discard block |
||
| 28 | 28 | if (!$count) { |
| 29 | 29 | return $default; |
| 30 | 30 | } |
| 31 | - foreach($arrayPath as $idxValue) { |
|
| 31 | + foreach ($arrayPath as $idxValue) { |
|
| 32 | 32 | $part = $idxValue; |
| 33 | - if(!array_key_exists($part, $data)) { |
|
| 33 | + if (!array_key_exists($part, $data)) { |
|
| 34 | 34 | return $default; |
| 35 | 35 | } |
| 36 | 36 | $data = $data[$part]; |
@@ -45,14 +45,14 @@ discard block |
||
| 45 | 45 | */ |
| 46 | 46 | private static function _recursiveHas($structure, array $path): bool { |
| 47 | 47 | $data = self::ensureArrayIsArray($structure); |
| 48 | - if($data === null) { |
|
| 48 | + if ($data === null) { |
|
| 49 | 49 | return false; |
| 50 | 50 | } |
| 51 | - if(!count($path)) { |
|
| 51 | + if (!count($path)) { |
|
| 52 | 52 | return false; |
| 53 | 53 | } |
| 54 | 54 | $key = array_shift($path); |
| 55 | - if(count($path)) { |
|
| 55 | + if (count($path)) { |
|
| 56 | 56 | return self::_recursiveHas($data[$key] ?? null, $path); |
| 57 | 57 | } |
| 58 | 58 | return array_key_exists($key, $data); |
@@ -63,11 +63,11 @@ discard block |
||
| 63 | 63 | * @return array<int, string> |
| 64 | 64 | */ |
| 65 | 65 | private static function getArrayPath($path): array { |
| 66 | - if(is_array($path)) { |
|
| 66 | + if (is_array($path)) { |
|
| 67 | 67 | return $path; |
| 68 | 68 | } |
| 69 | 69 | $parts = preg_split('{(?<!\\x5C)(?:\\x5C\\x5C)*\\.}', $path); |
| 70 | - if($parts === false) { |
|
| 70 | + if ($parts === false) { |
|
| 71 | 71 | return [$path]; |
| 72 | 72 | } |
| 73 | 73 | return $parts; |
@@ -78,13 +78,13 @@ discard block |
||
| 78 | 78 | * @return array<mixed, mixed> |
| 79 | 79 | */ |
| 80 | 80 | private static function ensureArrayIsArray($array): ?array { |
| 81 | - if($array instanceof ArrayObject) { |
|
| 81 | + if ($array instanceof ArrayObject) { |
|
| 82 | 82 | return $array->getArrayCopy(); |
| 83 | 83 | } |
| 84 | - if(is_object($array)) { |
|
| 84 | + if (is_object($array)) { |
|
| 85 | 85 | return (array) $array; |
| 86 | 86 | } |
| 87 | - if(is_array($array)) { |
|
| 87 | + if (is_array($array)) { |
|
| 88 | 88 | return $array; |
| 89 | 89 | } |
| 90 | 90 | return null; |
@@ -27,10 +27,10 @@ |
||
| 27 | 27 | * @return bool |
| 28 | 28 | */ |
| 29 | 29 | public function isValid(): bool { |
| 30 | - if(!RecursiveStructureAccess::recursiveHas($this->data, $this->path)) { |
|
| 30 | + if (!RecursiveStructureAccess::recursiveHas($this->data, $this->path)) { |
|
| 31 | 31 | return false; |
| 32 | 32 | } |
| 33 | - if($this->validator !== null) { |
|
| 33 | + if ($this->validator !== null) { |
|
| 34 | 34 | $value = $this->getValue(); |
| 35 | 35 | return call_user_func($this->validator, $value); |
| 36 | 36 | } |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | * @return null|int |
| 13 | 13 | */ |
| 14 | 14 | protected function getOffset(): ?int { |
| 15 | - if($this->offset instanceof OptionalValue) { |
|
| 15 | + if ($this->offset instanceof OptionalValue) { |
|
| 16 | 16 | return $this->offset->getValue(); |
| 17 | 17 | } |
| 18 | 18 | return $this->offset; |
@@ -33,15 +33,15 @@ discard block |
||
| 33 | 33 | */ |
| 34 | 34 | protected function buildOffset(string $query): string { |
| 35 | 35 | $offset = $this->getOffset(); |
| 36 | - if($this->offset instanceof OptionalValue) { |
|
| 37 | - if($this->offset->isValid()) { |
|
| 36 | + if ($this->offset instanceof OptionalValue) { |
|
| 37 | + if ($this->offset->isValid()) { |
|
| 38 | 38 | $value = $this->offset->getValue(); |
| 39 | - if(!preg_match('{\\d+}', $value)) { |
|
| 39 | + if (!preg_match('{\\d+}', $value)) { |
|
| 40 | 40 | throw new InvalidValueException('Value for OFFSET has to be a number'); |
| 41 | 41 | } |
| 42 | 42 | $query .= "OFFSET\n\t{$this->offset->getValue()}\n"; |
| 43 | 43 | } |
| 44 | - } elseif($offset !== null) { |
|
| 44 | + } elseif ($offset !== null) { |
|
| 45 | 45 | $query .= "OFFSET\n\t{$this->offset}\n"; |
| 46 | 46 | } |
| 47 | 47 | return $query; |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | * @return null|int |
| 13 | 13 | */ |
| 14 | 14 | protected function getLimit(): ?int { |
| 15 | - if($this->limit instanceof OptionalValue) { |
|
| 15 | + if ($this->limit instanceof OptionalValue) { |
|
| 16 | 16 | return $this->limit->getValue(); |
| 17 | 17 | } |
| 18 | 18 | return $this->limit; |
@@ -34,18 +34,18 @@ discard block |
||
| 34 | 34 | */ |
| 35 | 35 | protected function buildLimit(string $query, ?int $offset = null) { |
| 36 | 36 | $limit = $this->getLimit(); |
| 37 | - if($limit === null && $offset !== null) { |
|
| 37 | + if ($limit === null && $offset !== null) { |
|
| 38 | 38 | $limit = '18446744073709551615'; |
| 39 | 39 | } |
| 40 | - if($this->limit instanceof OptionalValue) { |
|
| 41 | - if($this->limit->isValid()) { |
|
| 40 | + if ($this->limit instanceof OptionalValue) { |
|
| 41 | + if ($this->limit->isValid()) { |
|
| 42 | 42 | $value = $this->limit->getValue(); |
| 43 | - if(!preg_match('{\\d+}', $value)) { |
|
| 43 | + if (!preg_match('{\\d+}', $value)) { |
|
| 44 | 44 | throw new InvalidValueException('Value for LIMIT has to be a number'); |
| 45 | 45 | } |
| 46 | 46 | $query .= "LIMIT\n\t{$this->limit->getValue()}\n"; |
| 47 | 47 | } |
| 48 | - } elseif($limit !== null) { |
|
| 48 | + } elseif ($limit !== null) { |
|
| 49 | 49 | $query .= "LIMIT\n\t{$limit}\n"; |
| 50 | 50 | } |
| 51 | 51 | return $query; |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | * @return $this |
| 90 | 90 | */ |
| 91 | 91 | public function addExpr(string $str, ...$args) { |
| 92 | - if(count($args) > 0) { |
|
| 92 | + if (count($args) > 0) { |
|
| 93 | 93 | $this->fields[] = func_get_args(); |
| 94 | 94 | } else { |
| 95 | 95 | $this->fields[] = $str; |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | * @return $this |
| 104 | 104 | */ |
| 105 | 105 | public function updateExpr(string $str, ...$args) { |
| 106 | - if(count($args) > 0) { |
|
| 106 | + if (count($args) > 0) { |
|
| 107 | 107 | $this->update[] = func_get_args(); |
| 108 | 108 | } else { |
| 109 | 109 | $this->update[] = $str; |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | * @return $this |
| 118 | 118 | */ |
| 119 | 119 | public function addOrUpdateExpr(string $expr, ...$args) { |
| 120 | - if(count($args) > 0) { |
|
| 120 | + if (count($args) > 0) { |
|
| 121 | 121 | $this->fields[] = func_get_args(); |
| 122 | 122 | $this->update[] = func_get_args(); |
| 123 | 123 | } else { |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | * @return $this |
| 135 | 135 | */ |
| 136 | 136 | public function addAll(array $data, array $mask = null, array $excludeFields = null) { |
| 137 | - $this->addAllTo($data, $mask, $excludeFields, function ($field, $value) { |
|
| 137 | + $this->addAllTo($data, $mask, $excludeFields, function($field, $value) { |
|
| 138 | 138 | $this->add($field, $value); |
| 139 | 139 | }); |
| 140 | 140 | return $this; |
@@ -147,8 +147,8 @@ discard block |
||
| 147 | 147 | * @return $this |
| 148 | 148 | */ |
| 149 | 149 | public function updateAll(array $data, array $mask = null, array $excludeFields = null) { |
| 150 | - $this->addAllTo($data, $mask, $excludeFields, function ($field, $value) { |
|
| 151 | - if($field !== $this->keyField) { |
|
| 150 | + $this->addAllTo($data, $mask, $excludeFields, function($field, $value) { |
|
| 151 | + if ($field !== $this->keyField) { |
|
| 152 | 152 | $this->update($field, $value); |
| 153 | 153 | } |
| 154 | 154 | }); |
@@ -192,7 +192,7 @@ discard block |
||
| 192 | 192 | * @return string |
| 193 | 193 | */ |
| 194 | 194 | public function __toString(): string { |
| 195 | - if($this->table === null) { |
|
| 195 | + if ($this->table === null) { |
|
| 196 | 196 | throw new RuntimeException('Specify a table-name'); |
| 197 | 197 | } |
| 198 | 198 | |
@@ -202,21 +202,21 @@ discard block |
||
| 202 | 202 | $ignoreStr = $this->ignore ? ' IGNORE' : ''; |
| 203 | 203 | $queryArr[] = "INSERT{$ignoreStr} INTO\n\t{$tableName}\n"; |
| 204 | 204 | |
| 205 | - if($this->from !== null) { |
|
| 205 | + if ($this->from !== null) { |
|
| 206 | 206 | $fields = $this->from->getFields(); |
| 207 | 207 | $queryArr[] = sprintf("\t(%s)\n", implode(', ', array_keys($fields))); |
| 208 | 208 | $queryArr[] = $this->from; |
| 209 | 209 | } else { |
| 210 | 210 | $fields = $this->fields; |
| 211 | 211 | $insertData = $this->buildFieldList($fields); |
| 212 | - if(!count($insertData)) { |
|
| 212 | + if (!count($insertData)) { |
|
| 213 | 213 | throw new RuntimeException('No field-data found'); |
| 214 | 214 | } |
| 215 | 215 | $queryArr[] = sprintf("SET\n%s\n", implode(",\n", $insertData)); |
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | $updateData = $this->buildUpdate(); |
| 219 | - if($updateData) { |
|
| 219 | + if ($updateData) { |
|
| 220 | 220 | $queryArr[] = "{$updateData}\n"; |
| 221 | 221 | } |
| 222 | 222 | |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | * @return array<string|int, mixed> |
| 231 | 231 | */ |
| 232 | 232 | private function addTo(array $fields, string $field, $value): array { |
| 233 | - if(!$this->isFieldNameValid($field)) { |
|
| 233 | + if (!$this->isFieldNameValid($field)) { |
|
| 234 | 234 | throw new UnexpectedValueException('Field name is invalid'); |
| 235 | 235 | } |
| 236 | 236 | $sqlField = $field; |
@@ -246,18 +246,18 @@ discard block |
||
| 246 | 246 | * @param callable(string, mixed): void $fn |
| 247 | 247 | */ |
| 248 | 248 | private function addAllTo(array $data, ?array $mask, ?array $excludeFields, $fn): void { |
| 249 | - if($mask !== null) { |
|
| 249 | + if ($mask !== null) { |
|
| 250 | 250 | $data = array_intersect_key($data, array_combine($mask, $mask)); |
| 251 | 251 | } |
| 252 | - if($excludeFields !== null) { |
|
| 253 | - foreach($excludeFields as $excludeField) { |
|
| 254 | - if(array_key_exists($excludeField, $data)) { |
|
| 252 | + if ($excludeFields !== null) { |
|
| 253 | + foreach ($excludeFields as $excludeField) { |
|
| 254 | + if (array_key_exists($excludeField, $data)) { |
|
| 255 | 255 | unset($data[$excludeField]); |
| 256 | 256 | } |
| 257 | 257 | } |
| 258 | 258 | } |
| 259 | 259 | $data = $this->clearValues($data); |
| 260 | - foreach($data as $field => $value) { |
|
| 260 | + foreach ($data as $field => $value) { |
|
| 261 | 261 | $fn($field, $value); |
| 262 | 262 | } |
| 263 | 263 | } |
@@ -267,10 +267,10 @@ discard block |
||
| 267 | 267 | */ |
| 268 | 268 | private function buildUpdate(): string { |
| 269 | 269 | $queryArr = []; |
| 270 | - if(!empty($this->update)) { |
|
| 270 | + if (!empty($this->update)) { |
|
| 271 | 271 | $queryArr[] = "ON DUPLICATE KEY UPDATE\n"; |
| 272 | 272 | $updateArr = []; |
| 273 | - if($this->keyField !== null) { |
|
| 273 | + if ($this->keyField !== null) { |
|
| 274 | 274 | $updateArr[] = "\t`{$this->keyField}` = LAST_INSERT_ID({$this->keyField})"; |
| 275 | 275 | } |
| 276 | 276 | $updateArr = $this->buildFieldList($this->update, $updateArr); |
@@ -293,7 +293,7 @@ discard block |
||
| 293 | 293 | * @return array<string, mixed> |
| 294 | 294 | */ |
| 295 | 295 | private function clearValues(array $values): array { |
| 296 | - if(!count($values)) { |
|
| 296 | + if (!count($values)) { |
|
| 297 | 297 | return []; |
| 298 | 298 | } |
| 299 | 299 | |
@@ -301,8 +301,8 @@ discard block |
||
| 301 | 301 | $fields = $this->db()->getTableFields($tableName); |
| 302 | 302 | $result = []; |
| 303 | 303 | |
| 304 | - foreach($values as $fieldName => $fieldValue) { |
|
| 305 | - if(in_array($fieldName, $fields)) { |
|
| 304 | + foreach ($values as $fieldName => $fieldValue) { |
|
| 305 | + if (in_array($fieldName, $fields)) { |
|
| 306 | 306 | $result[$fieldName] = $fieldValue; |
| 307 | 307 | } |
| 308 | 308 | } |
@@ -19,7 +19,7 @@ |
||
| 19 | 19 | /** @link http://php.net/manual/en/class.exception.php#Hcom115813 (cHao's comment) */ |
| 20 | 20 | $code = is_array($errorInfo) && isset($errorInfo[1]) ? ((int) $errorInfo[1]) : ((int) $exception->getCode()); |
| 21 | 21 | $message = $exception->getMessage(); |
| 22 | - switch($code) { |
|
| 22 | + switch ($code) { |
|
| 23 | 23 | case 2006: return new DatabaseHasGoneAwayException($message, $code, $exception); |
| 24 | 24 | case 1213: return new SqlDeadLockException($message, $code, $exception); |
| 25 | 25 | case 1205: return new LockWaitTimeoutExceededException($message, $code, $exception); |