|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license MIT |
|
5
|
|
|
* @author Samuel Adeshina <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This file is part of the EmmetBlue project, please read the license document |
|
8
|
|
|
* available in the root level of the project |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace EmmetBlue\Core\Builder\QueryBuilder; |
|
11
|
|
|
|
|
12
|
|
|
use EmmetBlue\Core\Builder\BuildableInterface; |
|
13
|
|
|
/** |
|
14
|
|
|
* class QueryBuilder. |
|
15
|
|
|
* Implements the QueryBuildableInterface contract {@see QueryBuildableInterface}. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Samuel Adeshina <[email protected]> |
|
18
|
|
|
* |
|
19
|
|
|
* @since v0.0.1 27/05/2016 13:35 |
|
20
|
|
|
*/ |
|
21
|
|
|
class QueryBuilder implements BuildableInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string Global Sql Statement |
|
25
|
|
|
*/ |
|
26
|
|
|
private $sqlStatement; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string SPACE |
|
30
|
|
|
*/ |
|
31
|
|
|
const SPACE = ' '; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string | null $sqlStatement SQL Statement to be processed by the build method |
|
35
|
|
|
* |
|
36
|
|
|
* @throws {@todo Write exception class} |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(string $sqlStatement = null) |
|
39
|
|
|
{ |
|
40
|
|
|
self::setSqlStatement($sqlStatement); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Returns the string value of the global sqlStatement variable. |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
private function getSqlStatement() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->sqlStatement; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Sets/Modifies the value of the global sqlStatement variable. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $sqlStatement to set/replace global equivalent to. |
|
57
|
|
|
* |
|
58
|
|
|
* @return null |
|
59
|
|
|
*/ |
|
60
|
|
|
private function setSqlStatement(string $sqlStatement = null) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->sqlStatement = $sqlStatement; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Builds a QueryBuilder object. |
|
67
|
|
|
* {@see QueryBuildableInterface}. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string | null $sqlStringToAppend |
|
70
|
|
|
* |
|
71
|
|
|
* @throws {@todo Create exceptions} |
|
72
|
|
|
* |
|
73
|
|
|
* @return QueryBuilder new instance of the QueryBuilder object. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function build(string $sqlStringToAppend) : BuildableInterface |
|
76
|
|
|
{ |
|
77
|
|
|
$separator = (empty(self::getSqlStatement())) ? '' : self::SPACE; |
|
78
|
|
|
$newSqlString = self::getSqlStatement().$separator.$sqlStringToAppend; |
|
79
|
|
|
self::setSqlStatement($newSqlString); |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* returns a `built sql` when the QueryBuilder object is casted to a string. |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public function __toString() |
|
90
|
|
|
{ |
|
91
|
|
|
return self::getSqlStatement(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Implodes an array into a string. |
|
96
|
|
|
* |
|
97
|
|
|
* @param array $arrayToImplode |
|
98
|
|
|
* @param string $delimiter Optional. |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function getImplodedString(array $arrayToImplode, string $delimiter = ',') : string |
|
103
|
|
|
{ |
|
104
|
|
|
return implode($delimiter, $arrayToImplode); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Implodes an array into a string while keeping track of the keys. |
|
109
|
|
|
* |
|
110
|
|
|
* @param array $arrayToImplode |
|
111
|
|
|
* @param string $delimiter Optional. |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function getImplodedStringWithKeys(array $arrayToImplode, string $keyDelimiter='=', string $delimiter = ',') : string |
|
116
|
|
|
{ |
|
117
|
|
|
$implodedStrings = []; |
|
118
|
|
|
|
|
119
|
|
|
foreach ($arrayToImplode as $key=>$value) |
|
120
|
|
|
{ |
|
121
|
|
|
$implodedStrings[] = $key.$keyDelimiter.$value; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return implode($delimiter, $implodedStrings); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Wraps a string with specified characters. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $strBefore |
|
131
|
|
|
* @param string|null $strAfter |
|
132
|
|
|
* @param string $strToWrap |
|
133
|
|
|
* |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
public static function wrapString(string $strToWrap, string $strBefore, string $strAfter = null) : string |
|
137
|
|
|
{ |
|
138
|
|
|
return $strBefore.$strToWrap.(is_null($strAfter) ? $strBefore : $strAfter); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* {@inheritdoc} |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $condition |
|
145
|
|
|
* |
|
146
|
|
|
* @return \EmmetBlue\Core\Builder\BuildableInterface |
|
147
|
|
|
*/ |
|
148
|
|
|
public function where(string $condition) |
|
149
|
|
|
{ |
|
150
|
|
|
$whereString = "WHERE $condition"; |
|
151
|
|
|
|
|
152
|
|
|
$this->queryBuilder = $this->queryBuilder->build($whereString); |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* {@inheritdoc} |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $condition |
|
161
|
|
|
* |
|
162
|
|
|
* @return \EmmetBlue\Core\Builder\BuildableInterface |
|
163
|
|
|
*/ |
|
164
|
|
|
public function andWhere(string $condition) |
|
165
|
|
|
{ |
|
166
|
|
|
$whereString = "AND $condition"; |
|
167
|
|
|
|
|
168
|
|
|
$this->queryBuilder = $this->queryBuilder->build($whereString); |
|
169
|
|
|
|
|
170
|
|
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* {@inheritdoc} |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $condition |
|
177
|
|
|
* |
|
178
|
|
|
* @return \EmmetBlue\Core\Builder\BuildableInterface |
|
179
|
|
|
*/ |
|
180
|
|
|
public function orWhere(string $condition) |
|
181
|
|
|
{ |
|
182
|
|
|
$whereString = "OR $condition"; |
|
183
|
|
|
|
|
184
|
|
|
$this->queryBuilder = $this->queryBuilder->build($whereString); |
|
185
|
|
|
|
|
186
|
|
|
return $this; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: