|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Puzzle\QueryBuilder\Queries\Snippets\Builders; |
|
6
|
|
|
|
|
7
|
|
|
use Puzzle\QueryBuilder\Queries\Snippets; |
|
8
|
|
|
|
|
9
|
|
|
trait Join |
|
10
|
|
|
{ |
|
11
|
|
|
protected |
|
12
|
|
|
$joins = array(); |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
5 |
|
public function innerJoin(string $table, ?string $alias = null): self |
|
15
|
|
|
{ |
|
16
|
5 |
|
$this->joins[] = new Snippets\Joins\InnerJoin($table, $alias); |
|
17
|
|
|
|
|
18
|
5 |
|
return $this; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
4 |
|
public function leftJoin(string $table, ?string $alias = null): self |
|
22
|
|
|
{ |
|
23
|
4 |
|
$this->joins[] = new Snippets\Joins\LeftJoin($table, $alias); |
|
24
|
|
|
|
|
25
|
4 |
|
return $this; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
4 |
|
public function rightJoin(string $table, ?string $alias = null): self |
|
29
|
|
|
{ |
|
30
|
4 |
|
$this->joins[] = new Snippets\Joins\RightJoin($table, $alias); |
|
31
|
|
|
|
|
32
|
4 |
|
return $this; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
10 |
|
public function on(string $leftColumn, string $rightColumn): self |
|
36
|
|
|
{ |
|
37
|
10 |
|
$join = $this->getLastJoin(); |
|
38
|
9 |
|
$join->on($leftColumn, $rightColumn); |
|
39
|
|
|
|
|
40
|
9 |
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param array[string]|string $column |
|
|
|
|
|
|
45
|
|
|
*/ |
|
46
|
2 |
|
public function using($column): self |
|
47
|
|
|
{ |
|
48
|
2 |
|
$join = $this->getLastJoin(); |
|
49
|
2 |
|
$join->using($column); |
|
50
|
|
|
|
|
51
|
2 |
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
30 |
|
protected function buildJoin(): string |
|
55
|
|
|
{ |
|
56
|
30 |
|
$joins = []; |
|
57
|
|
|
|
|
58
|
30 |
|
foreach($this->joins as $innerJoin) |
|
59
|
|
|
{ |
|
60
|
9 |
|
$joins[] = $innerJoin->toString(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
30 |
|
return implode(' ', $joins); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
10 |
|
private function getLastJoin(): Snippets\Join |
|
67
|
|
|
{ |
|
68
|
10 |
|
$lastJoins = end($this->joins); |
|
69
|
|
|
|
|
70
|
10 |
|
if(! $lastJoins instanceof Snippets\Join) |
|
71
|
|
|
{ |
|
72
|
1 |
|
throw new \LogicException('Erreur dans la récupération de la dernière jointure'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
9 |
|
return $lastJoins; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.