| 1 | <?php |
||
| 2 | namespace Kir\MySQL\Builder\Traits; |
||
| 3 | |||
| 4 | use Kir\MySQL\Builder\Internal\Types; |
||
| 5 | |||
| 6 | /** |
||
| 7 | * @phpstan-import-type DBParameterValueType from Types |
||
| 8 | * @phpstan-import-type DBTableNameType from Types |
||
| 9 | */ |
||
| 10 | trait JoinBuilder { |
||
| 11 | use AbstractDB; |
||
| 12 | use AbstractTableNameBuilder; |
||
| 13 | |||
| 14 | /** @var array<int, array{type: string, alias: string, name: DBTableNameType, expression: string|null, arguments: list<DBParameterValueType>}> */ |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 15 | private array $joinTables = []; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @param string $alias |
||
| 19 | * @param DBTableNameType $table |
||
|
0 ignored issues
–
show
The type
Kir\MySQL\Builder\Traits\DBTableNameType was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 20 | * @param string|null $expression |
||
| 21 | * @param DBParameterValueType ...$args |
||
|
0 ignored issues
–
show
The type
Kir\MySQL\Builder\Traits\DBParameterValueType was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 22 | * @return $this |
||
| 23 | */ |
||
| 24 | public function joinInner(string $alias, $table, ?string $expression = null, ...$args) { |
||
| 25 | return $this->addJoin('INNER', $alias, $table, $expression, $args); |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param string $alias |
||
| 30 | * @param DBTableNameType $table |
||
| 31 | * @param string $expression |
||
| 32 | * @param DBParameterValueType ...$args |
||
| 33 | * @return $this |
||
| 34 | */ |
||
| 35 | public function joinLeft(string $alias, $table, string $expression, ...$args) { |
||
| 36 | return $this->addJoin('LEFT', $alias, $table, $expression, $args); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param string $alias |
||
| 41 | * @param DBTableNameType $table |
||
| 42 | * @param string $expression |
||
| 43 | * @param DBParameterValueType ...$args |
||
| 44 | * @return $this |
||
| 45 | */ |
||
| 46 | public function joinRight(string $alias, $table, string $expression, ...$args) { |
||
| 47 | return $this->addJoin('RIGHT', $alias, $table, $expression, $args); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param string $query |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | protected function buildJoins(string $query): string { |
||
| 55 | $arr = []; |
||
| 56 | foreach($this->joinTables as $table) { |
||
| 57 | $join = $table['type']." JOIN\n"; |
||
| 58 | $join .= "\t" . $this->buildTableName($table['alias'], $table['name']); |
||
| 59 | if($table['expression']) { |
||
| 60 | $join .= " ON " . $this->db()->quoteExpression($table['expression'], $table['arguments']); |
||
| 61 | } |
||
| 62 | $arr[] = $join; |
||
| 63 | } |
||
| 64 | if(count($arr)) { |
||
| 65 | $query .= implode("\n", $arr)."\n"; |
||
| 66 | } |
||
| 67 | return $query; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param string $type |
||
| 72 | * @param string $alias |
||
| 73 | * @param DBTableNameType $name |
||
| 74 | * @param string|null $expression |
||
| 75 | * @param array<DBParameterValueType> $arguments |
||
| 76 | * @return $this |
||
| 77 | */ |
||
| 78 | private function addJoin(string $type, string $alias, $name, ?string $expression = null, array $arguments = []) { |
||
| 79 | $this->joinTables[] = [ |
||
| 80 | 'type' => $type, |
||
| 81 | 'alias' => $alias, |
||
| 82 | 'name' => $name, |
||
| 83 | 'expression' => $expression, |
||
| 84 | 'arguments' => $arguments |
||
| 85 | ]; |
||
| 86 | return $this; |
||
| 87 | } |
||
| 88 | } |
||
| 89 |