Repository navigation
Expand file tree
/
Copy pathJoin.php
More file actions
165 lines (140 loc) · 3.97 KB
/
Copy pathJoin.php
File metadata and controls
165 lines (140 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
declare(strict_types=1);
namespace PhpDb\Sql;
use Countable;
use Iterator;
use Override;
use ReturnTypeWillChange;
use function array_shift;
use function count;
use function is_array;
use function is_string;
use function key;
use function sprintf;
/**
* Aggregate JOIN specifications.
* Each specification is an array with the following keys:
* - name: the JOIN name
* - on: the table on which the JOIN occurs
* - columns: the columns to include with the JOIN operation; defaults to
* `Select::SQL_STAR`.
* - type: the type of JOIN being performed; see the `JOIN_*` constants;
* defaults to `JOIN_INNER`
*/
class Join implements Iterator, Countable
{
final public const JOIN_INNER = 'inner';
final public const JOIN_OUTER = 'outer';
final public const JOIN_FULL_OUTER = 'full outer';
final public const JOIN_LEFT = 'left';
final public const JOIN_RIGHT = 'right';
final public const JOIN_RIGHT_OUTER = 'right outer';
final public const JOIN_LEFT_OUTER = 'left outer';
/**
* Current iterator position.
*/
private int $position = 0;
/**
* JOIN specifications
*/
protected array $joins = [];
/**
* Rewind iterator.
*/
#[Override]
#[ReturnTypeWillChange]
public function rewind(): void
{
$this->position = 0;
}
/**
* Return current join specification.
*/
#[Override]
#[ReturnTypeWillChange]
public function current(): array
{
return $this->joins[$this->position];
}
/**
* Return the current iterator index.
*/
#[Override]
#[ReturnTypeWillChange]
public function key(): int
{
return $this->position;
}
/**
* Advance to the next JOIN specification.
*/
#[Override]
#[ReturnTypeWillChange]
public function next(): void
{
++$this->position;
}
/**
* Is the iterator at a valid position?
*/
#[Override]
#[ReturnTypeWillChange]
public function valid(): bool
{
return isset($this->joins[$this->position]);
}
public function getJoins(): array
{
return $this->joins;
}
/**
* @param array|string|TableIdentifier $name A table name on which to join, or a single
* element associative array, of the form alias => table, or TableIdentifier instance
* @param string|Predicate\Expression $on A specification describing the fields to join on.
* @param string|string[] $columns A single column name, an array
* of column names, or (a) specification(s) such as SQL_STAR representing
* the columns to join.
* @param string $type The JOIN type to use; see the JOIN_* constants.
* @throws Exception\InvalidArgumentException For invalid $name values.
*/
// phpcs:ignore Generic.NamingConventions.ConstructorName.OldStyle
public function join(
array|string|TableIdentifier $name,
string|Predicate\PredicateInterface $on,
array|string $columns = [Select::SQL_STAR],
string $type = self::JOIN_INNER
): static {
if (is_array($name) && (! is_string(key($name)) || count($name) !== 1)) {
throw new Exception\InvalidArgumentException(
sprintf("join() expects '%s' as a single element associative array", array_shift($name))
);
}
if (! is_array($columns)) {
$columns = [$columns];
}
$this->joins[] = [
'name' => $name,
'on' => $on,
'columns' => $columns,
'type' => $type,
];
return $this;
}
/**
* Reset to an empty list of JOIN specifications.
*/
public function reset(): static
{
$this->joins = [];
return $this;
}
/**
* Get count of attached predicates
*/
#[Override]
#[ReturnTypeWillChange]
public function count(): int
{
return count($this->joins);
}
}