@@ -16,8 +16,8 @@ |
||
| 16 | 16 | * @return $this |
| 17 | 17 | */ |
| 18 | 18 | public function where($expression, $_ = null) { |
| 19 | - if($expression instanceof OptionalExpression) { |
|
| 20 | - if($expression->isValid()) { |
|
| 19 | + if ($expression instanceof OptionalExpression) { |
|
| 20 | + if ($expression->isValid()) { |
|
| 21 | 21 | $this->where[] = [$expression->getExpression(), $expression->getValue()]; |
| 22 | 22 | } |
| 23 | 23 | } else { |
@@ -30,10 +30,10 @@ |
||
| 30 | 30 | * @return Select|null |
| 31 | 31 | */ |
| 32 | 32 | public function get($tableName) { |
| 33 | - if($this->has($tableName)) { |
|
| 33 | + if ($this->has($tableName)) { |
|
| 34 | 34 | $table = $this->virtualTables[(string) $tableName]; |
| 35 | - if($table instanceof \Closure) { |
|
| 36 | - if($tableName instanceof VirtualTable) { |
|
| 35 | + if ($table instanceof \Closure) { |
|
| 36 | + if ($tableName instanceof VirtualTable) { |
|
| 37 | 37 | $params = $tableName->getParams(); |
| 38 | 38 | return call_user_func($table, $params); |
| 39 | 39 | } |
@@ -13,36 +13,36 @@ |
||
| 13 | 13 | * @return string |
| 14 | 14 | */ |
| 15 | 15 | protected function buildTableName($alias, $name) { |
| 16 | - if(is_object($name) && !($name instanceof VirtualTable)) { |
|
| 16 | + if (is_object($name) && !($name instanceof VirtualTable)) { |
|
| 17 | 17 | $name = (string) $name; |
| 18 | 18 | $lines = explode("\n", $name); |
| 19 | - foreach($lines as &$line) { |
|
| 19 | + foreach ($lines as &$line) { |
|
| 20 | 20 | $line = "\t{$line}"; |
| 21 | 21 | } |
| 22 | 22 | $name = join("\n", $lines); |
| 23 | - $name = '(' . trim(rtrim(trim($name), ';')) . ')'; |
|
| 23 | + $name = '('.trim(rtrim(trim($name), ';')).')'; |
|
| 24 | 24 | } |
| 25 | - if(is_array($name)) { |
|
| 25 | + if (is_array($name)) { |
|
| 26 | 26 | $parts = []; |
| 27 | - foreach($name as $index => $bucket) { |
|
| 28 | - if(ctype_digit((string) $index) && is_scalar($bucket)) { |
|
| 27 | + foreach ($name as $index => $bucket) { |
|
| 28 | + if (ctype_digit((string) $index) && is_scalar($bucket)) { |
|
| 29 | 29 | $parts[] = "SELECT {$this->db()->quote($bucket)} AS {$this->db()->quoteField('value')}"; |
| 30 | 30 | } else { |
| 31 | 31 | $values = []; |
| 32 | - foreach($bucket as $field => $value) { |
|
| 32 | + foreach ($bucket as $field => $value) { |
|
| 33 | 33 | $values[] = sprintf('%s AS %s', $this->db()->quote($value), $this->db()->quoteField($field)); |
| 34 | 34 | } |
| 35 | 35 | $parts[] = sprintf("SELECT %s", join(', ', $values)); |
| 36 | 36 | } |
| 37 | 37 | } |
| 38 | - $name = '(' . join("\n\tUNION\n\t", $parts) . ')'; |
|
| 38 | + $name = '('.join("\n\tUNION\n\t", $parts).')'; |
|
| 39 | 39 | } |
| 40 | - if($this->db()->getVirtualTables()->has($name)) { |
|
| 40 | + if ($this->db()->getVirtualTables()->has($name)) { |
|
| 41 | 41 | $select = (string) $this->db()->getVirtualTables()->get($name); |
| 42 | 42 | $name = sprintf('(%s)', join("\n\t", explode("\n", trim($select)))); |
| 43 | 43 | } |
| 44 | 44 | $name = $this->aliasReplacer()->replace($name); |
| 45 | - if($alias !== null) { |
|
| 45 | + if ($alias !== null) { |
|
| 46 | 46 | return sprintf("%s %s", $name, $alias); |
| 47 | 47 | } |
| 48 | 48 | return $name; |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | $errorInfo = $exception->errorInfo; |
| 19 | 19 | $code = is_array($errorInfo) && isset($errorInfo[1]) ? ((int) $errorInfo[1]) : ((int) $exception->getCode()); |
| 20 | 20 | $message = (string) $exception->getMessage(); |
| 21 | - switch($code) { |
|
| 21 | + switch ($code) { |
|
| 22 | 22 | case 2006: throw new DatabaseHasGoneAwayException($message, $code, $exception); |
| 23 | 23 | case 1213: throw new SqlDeadLockException($message, $code, $exception); |
| 24 | 24 | case 1205: throw new LockWaitTimeoutExceededException($message, $code, $exception); |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | case 1452: throw new IntegrityConstraintViolationException($message, (int) $code, $exception); |
| 32 | 32 | } |
| 33 | 33 | /** @link http://php.net/manual/en/class.exception.php#Hcom115813 (cHao's comment) */ |
| 34 | - if(!is_string($message) || !is_int($code)) { |
|
| 34 | + if (!is_string($message) || !is_int($code)) { |
|
| 35 | 35 | throw new SqlException((string) $message, (int) $code, $exception); |
| 36 | 36 | } |
| 37 | 37 | throw $exception; |
@@ -27,16 +27,16 @@ discard block |
||
| 27 | 27 | $this->value = $data; |
| 28 | 28 | $this->keyPath = $this->buildKey($keyPath); |
| 29 | 29 | $this->value = $this->recursiveGet($data, $this->keyPath, null); |
| 30 | - if($validator === null) { |
|
| 31 | - $validator = function ($data) { |
|
| 32 | - if(is_array($data)) { |
|
| 30 | + if ($validator === null) { |
|
| 31 | + $validator = function($data) { |
|
| 32 | + if (is_array($data)) { |
|
| 33 | 33 | return $this->isValidArray($data); |
| 34 | 34 | } |
| 35 | 35 | return (string) $data !== ''; |
| 36 | 36 | }; |
| 37 | 37 | } |
| 38 | - if($validationResultHandler === null) { |
|
| 39 | - $validationResultHandler = function () {}; |
|
| 38 | + if ($validationResultHandler === null) { |
|
| 39 | + $validationResultHandler = function() {}; |
|
| 40 | 40 | } |
| 41 | 41 | $this->validator = $validator; |
| 42 | 42 | $this->validationResultHandler = $validationResultHandler; |
@@ -73,10 +73,10 @@ discard block |
||
| 73 | 73 | * @return string |
| 74 | 74 | */ |
| 75 | 75 | private function buildKey($keyPath) { |
| 76 | - if(is_string($keyPath)) { |
|
| 76 | + if (is_string($keyPath)) { |
|
| 77 | 77 | $keyPath = explode('.', $keyPath); |
| 78 | 78 | } |
| 79 | - if(!is_array($keyPath)) { |
|
| 79 | + if (!is_array($keyPath)) { |
|
| 80 | 80 | throw new RuntimeException('Invalid key'); |
| 81 | 81 | } |
| 82 | 82 | return $keyPath; |
@@ -87,8 +87,8 @@ discard block |
||
| 87 | 87 | * @return bool |
| 88 | 88 | */ |
| 89 | 89 | private function isValidArray(array $array) { |
| 90 | - $data = array_filter($array, function ($value) { |
|
| 91 | - if(is_array($value)) { |
|
| 90 | + $data = array_filter($array, function($value) { |
|
| 91 | + if (is_array($value)) { |
|
| 92 | 92 | return $this->isValidArray($value); |
| 93 | 93 | } |
| 94 | 94 | return (string) $value !== ''; |
@@ -107,9 +107,9 @@ discard block |
||
| 107 | 107 | if (!$count) { |
| 108 | 108 | return $default; |
| 109 | 109 | } |
| 110 | - for($idx = 0; $idx < $count; $idx++) { |
|
| 110 | + for ($idx = 0; $idx < $count; $idx++) { |
|
| 111 | 111 | $part = $path[$idx]; |
| 112 | - if(!array_key_exists($part, $array)) { |
|
| 112 | + if (!array_key_exists($part, $array)) { |
|
| 113 | 113 | return $default; |
| 114 | 114 | } |
| 115 | 115 | $array = $array[$part]; |
@@ -15,8 +15,8 @@ discard block |
||
| 15 | 15 | * @return $this |
| 16 | 16 | */ |
| 17 | 17 | public function orderBy($expression, $direction = 'asc') { |
| 18 | - if($expression instanceof OrderBySpecification) { |
|
| 19 | - foreach($expression->getFields() as $field) { |
|
| 18 | + if ($expression instanceof OrderBySpecification) { |
|
| 19 | + foreach ($expression->getFields() as $field) { |
|
| 20 | 20 | $this->addOrder($field[0], $field[1]); |
| 21 | 21 | } |
| 22 | 22 | return $this; |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | */ |
| 33 | 33 | public function orderByValues($fieldName, array $values) { |
| 34 | 34 | $expr = []; |
| 35 | - foreach(array_values($values) as $idx => $value) { |
|
| 35 | + foreach (array_values($values) as $idx => $value) { |
|
| 36 | 36 | $expr[] = $this->db()->quoteExpression("WHEN ? THEN ?", [$value, $idx]); |
| 37 | 37 | } |
| 38 | 38 | $this->orderBy[] = [sprintf("CASE %s\n\t\t%s\n\tEND", $this->db()->quoteField($fieldName), join("\n\t\t", $expr)), 'ASC']; |
@@ -44,12 +44,12 @@ discard block |
||
| 44 | 44 | * @return string |
| 45 | 45 | */ |
| 46 | 46 | protected function buildOrder($query) { |
| 47 | - if(!count($this->orderBy)) { |
|
| 47 | + if (!count($this->orderBy)) { |
|
| 48 | 48 | return $query; |
| 49 | 49 | } |
| 50 | 50 | $query .= "ORDER BY\n"; |
| 51 | 51 | $arr = []; |
| 52 | - foreach($this->orderBy as $order) { |
|
| 52 | + foreach ($this->orderBy as $order) { |
|
| 53 | 53 | list($expression, $direction) = $order; |
| 54 | 54 | $arr[] = sprintf("\t%s %s", $expression, strtoupper($direction)); |
| 55 | 55 | } |
@@ -62,8 +62,8 @@ discard block |
||
| 62 | 62 | */ |
| 63 | 63 | private function addOrder($expression, $direction) { |
| 64 | 64 | $direction = $this->fixDirection($direction); |
| 65 | - if(is_array($expression)) { |
|
| 66 | - if(!count($expression)) { |
|
| 65 | + if (is_array($expression)) { |
|
| 66 | + if (!count($expression)) { |
|
| 67 | 67 | return; |
| 68 | 68 | } |
| 69 | 69 | $arguments = [ |
@@ -47,15 +47,15 @@ |
||
| 47 | 47 | */ |
| 48 | 48 | protected function buildJoins($query) { |
| 49 | 49 | $arr = []; |
| 50 | - foreach($this->joinTables as $table) { |
|
| 50 | + foreach ($this->joinTables as $table) { |
|
| 51 | 51 | $join = $table['type']." JOIN\n"; |
| 52 | - $join .= "\t" . $this->buildTableName($table['alias'], $table['name']); |
|
| 53 | - if($table['expression']) { |
|
| 54 | - $join .= " ON " . $this->db()->quoteExpression($table['expression'], $table['arguments']); |
|
| 52 | + $join .= "\t".$this->buildTableName($table['alias'], $table['name']); |
|
| 53 | + if ($table['expression']) { |
|
| 54 | + $join .= " ON ".$this->db()->quoteExpression($table['expression'], $table['arguments']); |
|
| 55 | 55 | } |
| 56 | 56 | $arr[] = $join; |
| 57 | 57 | } |
| 58 | - if(count($arr)) { |
|
| 58 | + if (count($arr)) { |
|
| 59 | 59 | $query .= join("\n", $arr)."\n"; |
| 60 | 60 | } |
| 61 | 61 | return $query; |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | * @return $this |
| 14 | 14 | */ |
| 15 | 15 | protected function addTable($alias, $table = null) { |
| 16 | - if($table === null) { |
|
| 16 | + if ($table === null) { |
|
| 17 | 17 | list($alias, $table) = [$table, $alias]; |
| 18 | 18 | } |
| 19 | 19 | $this->tables[] = ['alias' => $alias, 'name' => $table]; |
@@ -26,10 +26,10 @@ discard block |
||
| 26 | 26 | */ |
| 27 | 27 | protected function buildTables($query) { |
| 28 | 28 | $arr = []; |
| 29 | - foreach($this->tables as $table) { |
|
| 29 | + foreach ($this->tables as $table) { |
|
| 30 | 30 | $arr[] = "\t".$this->buildTableName($table['alias'], $table['name']); |
| 31 | 31 | } |
| 32 | - if(count($arr)) { |
|
| 32 | + if (count($arr)) { |
|
| 33 | 33 | $query .= join(",\n", $arr)."\n"; |
| 34 | 34 | } |
| 35 | 35 | return $query; |
@@ -11,9 +11,9 @@ discard block |
||
| 11 | 11 | * @return $this |
| 12 | 12 | */ |
| 13 | 13 | public function groupBy() { |
| 14 | - foreach(func_get_args() as $expression) { |
|
| 15 | - if(is_array($expression)) { |
|
| 16 | - if(!count($expression)) { |
|
| 14 | + foreach (func_get_args() as $expression) { |
|
| 15 | + if (is_array($expression)) { |
|
| 16 | + if (!count($expression)) { |
|
| 17 | 17 | continue; |
| 18 | 18 | } |
| 19 | 19 | $arguments = [ |
@@ -32,12 +32,12 @@ discard block |
||
| 32 | 32 | * @return string |
| 33 | 33 | */ |
| 34 | 34 | protected function buildGroups($query) { |
| 35 | - if(!count($this->groupBy)) { |
|
| 35 | + if (!count($this->groupBy)) { |
|
| 36 | 36 | return $query; |
| 37 | 37 | } |
| 38 | 38 | $query .= "GROUP BY\n"; |
| 39 | 39 | $arr = []; |
| 40 | - foreach($this->groupBy as $expression) { |
|
| 40 | + foreach ($this->groupBy as $expression) { |
|
| 41 | 41 | $arr[] = "\t{$expression}"; |
| 42 | 42 | } |
| 43 | 43 | return $query.join(",\n", $arr)."\n"; |