| 1 | <?php |
||
| 2 | |||
| 3 | namespace Kir\MySQL\Builder\Traits; |
||
| 4 | |||
| 5 | use Kir\MySQL\Builder\Internal\Types; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * @phpstan-import-type DBParameterValueType from Types |
||
| 9 | * @phpstan-import-type DBTableNameType from Types |
||
| 10 | */ |
||
| 11 | trait JoinBuilder { |
||
| 12 | use AbstractDB; |
||
| 13 | use AbstractTableNameBuilder; |
||
| 14 | |||
| 15 | /** @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...
|
|||
| 16 | private array $joinTables = []; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @param string $alias |
||
| 20 | * @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...
|
|||
| 21 | * @param string|null $expression |
||
| 22 | * @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...
|
|||
| 23 | * @return $this |
||
| 24 | */ |
||
| 25 | public function joinInner(string $alias, $table, ?string $expression = null, ...$args) { |
||
| 26 | return $this->addJoin('INNER', $alias, $table, $expression, $args); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param string $alias |
||
| 31 | * @param DBTableNameType $table |
||
| 32 | * @param string $expression |
||
| 33 | * @param DBParameterValueType ...$args |
||
| 34 | * @return $this |
||
| 35 | */ |
||
| 36 | public function joinLeft(string $alias, $table, string $expression, ...$args) { |
||
| 37 | return $this->addJoin('LEFT', $alias, $table, $expression, $args); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param string $alias |
||
| 42 | * @param DBTableNameType $table |
||
| 43 | * @param string $expression |
||
| 44 | * @param DBParameterValueType ...$args |
||
| 45 | * @return $this |
||
| 46 | */ |
||
| 47 | public function joinRight(string $alias, $table, string $expression, ...$args) { |
||
| 48 | return $this->addJoin('RIGHT', $alias, $table, $expression, $args); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param string $query |
||
| 53 | * @return string |
||
| 54 | */ |
||
| 55 | protected function buildJoins(string $query): string { |
||
| 56 | $arr = []; |
||
| 57 | foreach($this->joinTables as $table) { |
||
| 58 | $join = $table['type'] . " JOIN\n"; |
||
| 59 | $join .= "\t" . $this->buildTableName($table['alias'], $table['name']); |
||
| 60 | if($table['expression']) { |
||
| 61 | $join .= " ON " . $this->db()->quoteExpression($table['expression'], $table['arguments']); |
||
| 62 | } |
||
| 63 | $arr[] = $join; |
||
| 64 | } |
||
| 65 | if(count($arr)) { |
||
| 66 | $query .= implode("\n", $arr) . "\n"; |
||
| 67 | } |
||
| 68 | |||
| 69 | return $query; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param string $type |
||
| 74 | * @param string $alias |
||
| 75 | * @param DBTableNameType $name |
||
| 76 | * @param string|null $expression |
||
| 77 | * @param array<DBParameterValueType> $arguments |
||
| 78 | * @return $this |
||
| 79 | */ |
||
| 80 | private function addJoin(string $type, string $alias, $name, ?string $expression = null, array $arguments = []) { |
||
| 81 | $this->joinTables[] = [ |
||
| 82 | 'type' => $type, |
||
| 83 | 'alias' => $alias, |
||
| 84 | 'name' => $name, |
||
| 85 | 'expression' => $expression, |
||
| 86 | 'arguments' => $arguments, |
||
| 87 | ]; |
||
| 88 | |||
| 89 | return $this; |
||
| 90 | } |
||
| 91 | } |
||
| 92 |