1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HexMakina\Crudites\Grammar\Clause; |
4
|
|
|
|
5
|
|
|
use HexMakina\Crudites\Grammar\Predicate; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* LEFT JOIN, two ways |
9
|
|
|
* (new Join('cbx_order', 'Orders'))->on('user_id', 'User', 'id')->type('LEFT'); |
10
|
|
|
* (new Join('cbx_order', 'Orders'))->on('user_id', 'User', 'id')->left(); |
11
|
|
|
|
12
|
|
|
* INNER JOIN, with helper, before on |
13
|
|
|
* (new Join('cbx_order', 'Orders'))->inner()->on('user_id', 'User', 'id'); |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
class Join extends Clause |
17
|
|
|
{ |
18
|
|
|
protected string $type; |
19
|
|
|
protected string $table; |
20
|
|
|
protected string $alias; |
21
|
|
|
|
22
|
|
|
protected ?string $column; |
23
|
|
|
protected ?string $referenced_table; |
24
|
|
|
protected ?string $referenced_column; |
25
|
|
|
|
26
|
|
|
public function __construct(string $table, string $alias = null) |
27
|
|
|
{ |
28
|
|
|
$this->type = ''; |
29
|
|
|
$this->table = $table; |
30
|
|
|
$this->alias = $alias ?? $table; |
31
|
|
|
|
32
|
|
|
$this->column = null; |
33
|
|
|
$this->referenced_table = null; |
34
|
|
|
$this->referenced_column = null; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function on(string $column, string $join_table, string $join_column): self |
38
|
|
|
{ |
39
|
|
|
$this->column = $column; |
40
|
|
|
$this->referenced_table = $join_table; |
41
|
|
|
$this->referenced_column = $join_column; |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function type(string $join_type): self |
47
|
|
|
{ |
48
|
|
|
$this->type = $join_type; |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function __toString(): string |
54
|
|
|
{ |
55
|
|
|
if(isset($this->column, $this->referenced_table, $this->referenced_column)) { |
56
|
|
|
$on = (string)(new Predicate([$this->alias, $this->column], '=', [$this->referenced_table, $this->referenced_column])); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return trim(sprintf('%s JOIN `%s` `%s` ON %s', $this->type, $this->table, $this->alias, $on ?? '')); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string the name of the clause |
64
|
|
|
*/ |
65
|
|
|
public function name(): string |
66
|
|
|
{ |
67
|
|
|
return self::JOIN; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string the alias of the table |
72
|
|
|
*/ |
73
|
|
|
public function alias(): string |
74
|
|
|
{ |
75
|
|
|
return $this->alias; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string the table name |
80
|
|
|
*/ |
81
|
|
|
public function table(): string |
82
|
|
|
{ |
83
|
|
|
return $this->table; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function column(): ?string |
87
|
|
|
{ |
88
|
|
|
return $this->column; |
89
|
|
|
} |
90
|
|
|
/** |
91
|
|
|
* @return array [table, column] the referenced table and column |
92
|
|
|
*/ |
93
|
|
|
public function referenced(): ?array |
94
|
|
|
{ |
95
|
|
|
if(isset($this->referenced_table, $this->referenced_column)) { |
96
|
|
|
return [$this->referenced_table, $this->referenced_column]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return null; |
100
|
|
|
} |
101
|
|
|
} |