1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Compolomus\LSQLQueryBuilder\Parts; |
4
|
|
|
|
5
|
|
|
use Compolomus\LSQLQueryBuilder\System\{ |
6
|
|
|
Traits\Helper, |
7
|
|
|
Traits\Caller |
8
|
|
|
}; |
9
|
|
|
|
10
|
|
|
class Join |
11
|
|
|
{ |
12
|
|
|
use Caller, Helper; |
13
|
|
|
|
14
|
|
|
private $table; |
15
|
|
|
|
16
|
|
|
private $onPairs; |
17
|
|
|
|
18
|
|
|
private $using = null; |
19
|
|
|
|
20
|
|
|
private $alias = null; |
21
|
|
|
|
22
|
|
|
private $joinType = null; |
23
|
|
|
|
24
|
|
|
private $joinTypes = ['left', 'right', 'cross', 'inner']; //TODO: add types |
25
|
|
|
|
26
|
|
|
private $counter = 0; |
27
|
|
|
|
28
|
|
|
public function __construct(string $table, ?string $alias = null, array $onPairs = [], string $joinType = 'left') |
29
|
|
|
{ |
30
|
|
|
$this->join($table, $alias, $onPairs, $joinType); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function debug() |
34
|
|
|
{ |
35
|
|
|
echo '<pre>' . print_r($this, true) . '</pre>'; |
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function join(string $table, ?string $alias = null, array $onPairs = [], string $joinType = 'left'): Join |
40
|
|
|
{ |
41
|
|
|
$this->counter++; |
42
|
|
|
$this->table[$this->counter] = $table; |
43
|
|
|
$this->addOn($onPairs); |
44
|
|
|
$this->setAlias($alias); |
45
|
|
|
$this->setType($joinType); |
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function setType(string $joinType): Join |
50
|
|
|
{ |
51
|
|
|
if (!in_array($joinType, $this->joinTypes)) { |
52
|
|
|
throw new \InvalidArgumentException('DIE |JOIN construct|'); |
53
|
|
|
} |
54
|
|
|
$this->joinType[$this->counter] = $joinType; |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function setAlias(?string $alias): Join |
59
|
|
|
{ |
60
|
|
|
if (!is_null($alias)) { |
61
|
|
|
$this->alias[$this->counter] = $alias; |
62
|
|
|
} |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getTable(int $counter): string |
67
|
|
|
{ |
68
|
|
|
return (!is_null($this->alias[$counter])) ? $this->escapeField($this->alias[$counter]) : $this->escapeField($this->table[$counter]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function using(string $field): Join |
72
|
|
|
{ |
73
|
|
|
$this->using = $this->escapeField($field); |
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function addOn(array $onPairs): Join |
78
|
|
|
{ |
79
|
|
|
if (count($onPairs)) { |
80
|
|
|
$this->onPairs[$this->counter] = $onPairs; |
81
|
|
|
} |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function onMap(): ?string |
86
|
|
|
{ |
87
|
|
|
if (!empty($this->onPairs)) { |
88
|
|
|
$result = []; |
89
|
|
|
foreach ($this->table as $counter => $join) { |
90
|
|
|
$result[] = $this->concatWhere(array_map(function ($item) use ($join, $counter) { |
91
|
|
|
return $this->base->table() . '.' . $this->escapeField($item[0]) . ' = ' . $this->escapeField($join) . '.' . $this->escapeField($item[1]); |
92
|
|
|
}, $this->onPairs[$counter])); |
93
|
|
|
} |
94
|
|
|
return ' ON ' . $this->concatWhere($result); |
95
|
|
|
} |
96
|
|
|
return null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function result(): ?string |
100
|
|
|
{ |
101
|
|
|
$result = []; |
102
|
|
|
foreach ($this->table as $counter => $join) { |
103
|
|
|
$result[] = ' ' . strtoupper($this->joinType[$counter]) . ' JOIN ' |
104
|
|
|
. $this->escapeField($join) |
105
|
|
|
. (!is_null($this->alias[$counter]) ? ' AS ' . $this->escapeField($this->alias[$counter]) : ''); |
106
|
|
|
} |
107
|
|
|
$result[] = ($this->onMap() |
108
|
|
|
? $this->onMap() |
109
|
|
|
: (!is_null($this->using) |
110
|
|
|
? ' USING(' . $this->using . ')' |
111
|
|
|
: '' |
112
|
|
|
) |
113
|
|
|
); |
114
|
|
|
return implode(' ', $result); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|