1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Phuria\SQLBuilder\Table; |
4
|
|
|
|
5
|
|
|
use Phuria\SQLBuilder\QueryBuilder\AbstractBuilder; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
abstract class AbstractTable |
11
|
|
|
{ |
12
|
|
|
const CROSS_JOIN = 'CROSS JOIN'; |
13
|
|
|
const LEFT_JOIN = 'LEFT JOIN'; |
14
|
|
|
const INNER_JOIN = 'INNER JOIN'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var AbstractBuilder $qb |
18
|
|
|
*/ |
19
|
|
|
private $qb; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string $tableAlias |
23
|
|
|
*/ |
24
|
|
|
private $tableAlias; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string $joinType |
28
|
|
|
*/ |
29
|
|
|
private $joinType; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string $joinOn |
33
|
|
|
*/ |
34
|
|
|
private $joinOn; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var bool $join |
38
|
|
|
*/ |
39
|
|
|
private $join = false; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param AbstractBuilder $qb |
43
|
|
|
*/ |
44
|
30 |
|
public function __construct(AbstractBuilder $qb) |
45
|
|
|
{ |
46
|
30 |
|
$this->qb = $qb; |
47
|
30 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
20 |
|
public function __toString() |
53
|
|
|
{ |
54
|
20 |
|
return $this->qb->getReferenceManager()->register($this); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
abstract public function getTableName(); |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return AbstractBuilder |
64
|
|
|
*/ |
65
|
1 |
|
public function getQueryBuilder() |
66
|
|
|
{ |
67
|
1 |
|
return $this->qb; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
28 |
|
public function getAlias() |
74
|
|
|
{ |
75
|
28 |
|
return $this->tableAlias; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $alias |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
9 |
|
public function setAlias($alias) |
84
|
|
|
{ |
85
|
9 |
|
$this->tableAlias = $alias; |
86
|
|
|
|
87
|
9 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
28 |
|
public function isJoin() |
94
|
|
|
{ |
95
|
28 |
|
return $this->join; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
3 |
|
public function getJoinType() |
102
|
|
|
{ |
103
|
3 |
|
return $this->joinType; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $joinType |
108
|
|
|
* |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
3 |
|
public function setJoinType($joinType) |
112
|
|
|
{ |
113
|
3 |
|
$this->joinType = $joinType; |
114
|
3 |
|
$this->join = true; |
115
|
|
|
|
116
|
3 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
20 |
|
public function getAliasOrName() |
123
|
|
|
{ |
124
|
20 |
|
return $this->getAlias() ?: $this->getTableName(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $clause |
129
|
|
|
* |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
4 |
|
public function addSelect($clause) |
133
|
|
|
{ |
134
|
4 |
|
$this->qb->addSelect($clause); |
|
|
|
|
135
|
|
|
|
136
|
4 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $clause |
141
|
|
|
* |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
2 |
|
public function joinOn($clause) |
145
|
|
|
{ |
146
|
2 |
|
$this->joinOn = $clause; |
147
|
|
|
|
148
|
2 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
28 |
|
public function getJoinOn() |
155
|
|
|
{ |
156
|
28 |
|
return $this->joinOn; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $name |
161
|
|
|
* |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
20 |
|
public function column($name) |
165
|
|
|
{ |
166
|
20 |
|
return $this . '.' . $name; |
167
|
|
|
} |
168
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: