|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Puzzle\QueryBuilder\Queries\Snippets\Joins; |
|
6
|
|
|
|
|
7
|
|
|
use Puzzle\QueryBuilder\Snippet; |
|
8
|
|
|
use Puzzle\QueryBuilder\Queries\Snippets\Join; |
|
9
|
|
|
use Puzzle\QueryBuilder\Queries\Snippets; |
|
10
|
|
|
use Puzzle\QueryBuilder\Queries\Snippets\NeedTableAware; |
|
11
|
|
|
|
|
12
|
|
|
abstract class AbstractJoin implements Join, Snippet, NeedTableAware |
|
13
|
|
|
{ |
|
14
|
|
|
private |
|
15
|
|
|
$table, |
|
|
|
|
|
|
16
|
|
|
$using, |
|
17
|
|
|
$on; |
|
18
|
|
|
|
|
19
|
25 |
|
public function __construct(string $table, ?string $alias = null) |
|
20
|
|
|
{ |
|
21
|
25 |
|
$this->table = new Snippets\TableName($table, $alias); |
|
22
|
25 |
|
$this->on = []; |
|
23
|
25 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param array[string]|string $column |
|
|
|
|
|
|
27
|
|
|
*/ |
|
28
|
9 |
|
public function using($column): self |
|
29
|
|
|
{ |
|
30
|
9 |
|
$this->on = []; |
|
31
|
|
|
|
|
32
|
9 |
|
$this->using = new Snippets\Using($column); |
|
33
|
|
|
|
|
34
|
9 |
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
18 |
|
public function on(?string $leftColumn, ?string $rightColumn): self |
|
38
|
|
|
{ |
|
39
|
18 |
|
$this->using = null; |
|
40
|
18 |
|
$this->on[] = new Snippets\On($leftColumn, $rightColumn); |
|
41
|
|
|
|
|
42
|
18 |
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
25 |
|
public function toString(): string |
|
46
|
|
|
{ |
|
47
|
25 |
|
$joinQueryPart = sprintf( |
|
48
|
25 |
|
'%s %s', |
|
49
|
25 |
|
$this->getJoinDeclaration(), |
|
50
|
25 |
|
$this->table->toString() |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
25 |
|
$joinQueryPart .= $this->buildOnConditionClause(); |
|
54
|
25 |
|
$joinQueryPart .= $this->buildUsingConditionClause(); |
|
55
|
|
|
|
|
56
|
25 |
|
return $joinQueryPart; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
public function hasNeededTable(string $tableName): bool |
|
60
|
|
|
{ |
|
61
|
2 |
|
if($this->table->getName() === $tableName || $this->table->getAlias() === $tableName) |
|
62
|
|
|
{ |
|
63
|
2 |
|
return true; |
|
64
|
|
|
} |
|
65
|
1 |
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
abstract protected function getJoinDeclaration(): string; |
|
69
|
|
|
|
|
70
|
25 |
|
private function buildUsingConditionClause(): string |
|
71
|
|
|
{ |
|
72
|
25 |
|
if(!$this->using instanceof Snippet) |
|
73
|
|
|
{ |
|
74
|
19 |
|
return ''; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
9 |
|
return ' ' . $this->using->toString(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
25 |
|
private function buildOnConditionClause(): string |
|
81
|
|
|
{ |
|
82
|
25 |
|
$conditionClause = array(); |
|
83
|
|
|
|
|
84
|
25 |
|
foreach($this->on as $on) |
|
85
|
|
|
{ |
|
86
|
18 |
|
if($on instanceof Snippet) |
|
87
|
|
|
{ |
|
88
|
18 |
|
$conditionClause[] = $on->toString(); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
25 |
|
return empty($conditionClause) ? '' : ' ' . implode('', $conditionClause); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.