|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Smoren\QueryRelationManager\Base\Structs; |
|
4
|
|
|
|
|
5
|
|
|
use Countable; |
|
6
|
|
|
use IteratorAggregate; |
|
7
|
|
|
use Smoren\QueryRelationManager\Base\QueryRelationManagerException; |
|
8
|
|
|
use Traversable; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Collection of join conditions used in select-query |
|
12
|
|
|
* @author Smoren <[email protected]> |
|
13
|
|
|
* @implements IteratorAggregate<JoinCondition> |
|
14
|
|
|
*/ |
|
15
|
|
|
class JoinConditionCollection implements Countable, IteratorAggregate |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var JoinCondition[] map of JoinCondition objects indexed by alias of joined table |
|
19
|
|
|
*/ |
|
20
|
|
|
protected array $mapByJoinAs = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var JoinCondition[][] map of JoinCondition objects lists indexed by alias of the table to join to |
|
24
|
|
|
*/ |
|
25
|
|
|
protected array $matrixByJoinTo = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Adds JoinCondition to collection |
|
29
|
|
|
* @param JoinCondition $condition JoinCondition object |
|
30
|
|
|
* @return $this |
|
31
|
|
|
* @throws QueryRelationManagerException |
|
32
|
|
|
*/ |
|
33
|
|
|
public function add(JoinCondition $condition): self |
|
34
|
|
|
{ |
|
35
|
|
|
if(isset($this->mapByJoinAs[$condition->table->alias])) { |
|
36
|
|
|
throw new QueryRelationManagerException("duplicate table alias '{$condition->table->alias}'"); |
|
37
|
|
|
} |
|
38
|
|
|
$this->mapByJoinAs[$condition->table->alias] = $condition; |
|
39
|
|
|
|
|
40
|
|
|
if(!isset($this->matrixByJoinTo[$condition->joinTo->alias])) { |
|
41
|
|
|
$this->matrixByJoinTo[$condition->joinTo->alias] = []; |
|
42
|
|
|
} |
|
43
|
|
|
if(isset($this->matrixByJoinTo[$condition->joinTo->alias][$condition->table->alias])) { |
|
44
|
|
|
throw new QueryRelationManagerException("duplicate table alias '{$condition->table->alias}'"); |
|
45
|
|
|
} |
|
46
|
|
|
$this->matrixByJoinTo[$condition->joinTo->alias][$condition->table->alias] = $condition; |
|
47
|
|
|
|
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Returns true if condition exists for the joined table alias |
|
53
|
|
|
* @param string $joinAs joined table alias |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
|
|
public function issetByJoinAs(string $joinAs): bool |
|
57
|
|
|
{ |
|
58
|
|
|
if(!isset($this->mapByJoinAs[$joinAs])) { |
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns join condition for the joined table alias |
|
67
|
|
|
* @param string $joinAs joined table alias |
|
68
|
|
|
* @return JoinCondition |
|
69
|
|
|
*/ |
|
70
|
|
|
public function byJoinAs(string $joinAs): JoinCondition |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->mapByJoinAs[$joinAs]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Returns list of join conditions by alias of table to join to |
|
77
|
|
|
* @param string $joinTo alias of table to join to |
|
78
|
|
|
* @return JoinCondition[] |
|
79
|
|
|
*/ |
|
80
|
|
|
public function byJoinTo(string $joinTo): array |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->matrixByJoinTo[$joinTo] ?? []; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @inheritDoc |
|
87
|
|
|
* @return Traversable<JoinCondition> |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getIterator(): Traversable |
|
90
|
|
|
{ |
|
91
|
|
|
foreach($this->mapByJoinAs as $condition) { |
|
92
|
|
|
yield $condition; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @inheritDoc |
|
98
|
|
|
*/ |
|
99
|
|
|
public function count(): int |
|
100
|
|
|
{ |
|
101
|
|
|
return count($this->mapByJoinAs); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|