1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sanderdekroon\Parlant\Builder; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Sanderdekroon\Parlant\Container; |
7
|
|
|
use Sanderdekroon\Parlant\Grammar\PosttypeGrammar; |
8
|
|
|
use Sanderdekroon\Parlant\Compiler\PosttypeCompiler; |
9
|
|
|
use Sanderdekroon\Parlant\Configurator\ParlantConfigurator; |
10
|
|
|
use Sanderdekroon\Parlant\Configurator\ConfiguratorInterface; |
11
|
|
|
|
12
|
|
|
class PosttypeBuilder implements BuilderInterface |
13
|
|
|
{ |
14
|
|
|
use BuildsQueries, QueriesMeta, QueriesTaxonomies; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
protected $grammar; |
18
|
|
|
|
19
|
|
|
protected $compiler; |
20
|
|
|
protected $bindings; |
21
|
|
|
|
22
|
|
|
protected $configuration; |
23
|
|
|
|
24
|
|
|
|
25
|
35 |
|
public function __construct(Container $container = null) |
26
|
|
|
{ |
27
|
35 |
|
$this->grammar = new PosttypeGrammar; // Replace via DI |
28
|
35 |
|
$this->compiler = new PosttypeCompiler($this->getGrammar()); // Replace via DI |
29
|
35 |
|
$this->bindings = $container ?: new Container; |
30
|
35 |
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
public function configure($configuration) |
34
|
|
|
{ |
35
|
|
|
// If the supplied configuration is an instance of the ConfiguratorInterface |
36
|
|
|
// we can add it directly to the PosttypeBuilder. This way a developer |
37
|
|
|
// can supply their own configurator implementation. |
38
|
|
|
if ($configuration instanceof ConfiguratorInterface) { |
39
|
|
|
return $this->configuration = $configuration; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// If the developer wants to add additional configuration but has not |
43
|
|
|
// supplied an instance of the ConfiguratorInterface, we'll create |
44
|
|
|
// a new instance of our own implementation of the configurator. |
45
|
|
|
if (is_null($this->getConfiguration())) { |
46
|
|
|
$this->configuration = new ParlantConfigurator; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this->configuration->add($configuration); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
3 |
|
public function setConfig($key, $value) |
54
|
|
|
{ |
55
|
3 |
|
if ($this->requiresConfiguration()) { |
56
|
|
|
$this->applyDefaultConfiguration(); |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
return $this->updateConfiguration($key, $value); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
3 |
|
protected function updateConfiguration($key, $value) |
64
|
|
|
{ |
65
|
3 |
|
$this->getConfiguration()->add($key, $value); |
66
|
|
|
|
67
|
3 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Determine if the builder requires additional configuration. |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
35 |
|
protected function requiresConfiguration() |
75
|
|
|
{ |
76
|
35 |
|
return is_null($this->getConfiguration()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Fill the configuration property with an instance of our ParlantConfigurator. |
81
|
|
|
* @return ParlantConfigurator |
82
|
|
|
*/ |
83
|
35 |
|
protected function applyDefaultConfiguration() |
84
|
|
|
{ |
85
|
35 |
|
return $this->configuration = new ParlantConfigurator; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set the posttype the developer is querying. |
90
|
|
|
* @param string $posttype |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
35 |
|
public function type($posttype) |
94
|
|
|
{ |
95
|
|
|
// Since this is the entry method (for now), we'll assume the developer allready |
96
|
|
|
// has supplied some form of configuration. If nothing is found, we'll create a |
97
|
|
|
// new instance of our ParlantConfigurator which sets some default settings. |
98
|
35 |
|
if ($this->requiresConfiguration()) { |
99
|
35 |
|
$this->applyDefaultConfiguration(); |
100
|
|
|
} |
101
|
|
|
|
102
|
35 |
|
$this->setBinding('post_type', $posttype); |
103
|
35 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Start a new Where clause. This simply adds the arguments to the outer array. |
108
|
|
|
* @param string|array $column |
109
|
|
|
* @param string|null $operator |
110
|
|
|
* @param string|null $value |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
4 |
|
public function where($column, $operator = null, $value = null) |
114
|
|
|
{ |
115
|
4 |
|
$clause = new WhereClause($this->getGrammar()); |
116
|
|
|
|
117
|
4 |
|
foreach ($clause->build($column, $operator, $value) as $where) { |
118
|
3 |
|
$this->appendBinding('wheres', $where); |
119
|
|
|
} |
120
|
|
|
|
121
|
3 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Limit the number of posts to return to the $number |
126
|
|
|
* @param int $number |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
1 |
|
public function limit($number) |
130
|
|
|
{ |
131
|
1 |
|
$this->setBinding('limit', (int)$number); |
132
|
1 |
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set the post offset. |
137
|
|
|
* @param int $number |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
1 |
|
public function offset($number) |
141
|
|
|
{ |
142
|
1 |
|
$this->setBinding('offset', (int)$number); |
143
|
1 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
2 |
|
public function order($direction) |
148
|
|
|
{ |
149
|
2 |
|
$this->setBinding('order', is_string($direction) ? strtoupper($direction) : $direction); |
150
|
2 |
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
2 |
|
public function orderBy($column, $direction = null) |
155
|
|
|
{ |
156
|
2 |
|
$this->setBinding('orderby', $column); |
157
|
|
|
|
158
|
2 |
|
if (!empty($direction)) { |
159
|
1 |
|
return $this->order($direction); |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Prepare the value and operator. If $useDefault is true, return the default operator (=) |
167
|
|
|
* Throws an exception if the operator is not supported with the current grammer. |
168
|
|
|
* @param mixed $value |
169
|
|
|
* @param string $operator |
170
|
|
|
* @param boolean $useDefault |
171
|
|
|
* @throws InvalidArgumentException |
172
|
|
|
* @return array |
173
|
|
|
* @todo make deprecated |
174
|
|
|
*/ |
175
|
|
|
protected function prepareValueAndOperator($value, $operator, $useDefault = false, $termDefault = false) |
176
|
|
|
{ |
177
|
|
|
if ($useDefault) { |
178
|
|
|
return [$operator, $termDefault ? 'IN' : '=']; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if ($this->invalidOperator($operator) && !is_null($value)) { |
182
|
|
|
throw new InvalidArgumentException('Illegal operator and value combination.'); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return [$value, $operator]; |
186
|
|
|
} |
187
|
|
|
|
188
|
35 |
|
protected function getGrammar() |
189
|
|
|
{ |
190
|
35 |
|
return $this->grammar; |
191
|
|
|
} |
192
|
|
|
|
193
|
28 |
|
protected function getCompiler() |
194
|
|
|
{ |
195
|
28 |
|
return $this->compiler; |
196
|
|
|
} |
197
|
|
|
|
198
|
35 |
|
protected function getBindings() |
199
|
|
|
{ |
200
|
35 |
|
return $this->bindings; |
201
|
|
|
} |
202
|
|
|
|
203
|
35 |
|
protected function getConfiguration() |
204
|
|
|
{ |
205
|
35 |
|
return $this->configuration; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Determine if an operator is invalid or unsupported |
210
|
|
|
* @param string $operator |
211
|
|
|
* @return bool |
212
|
|
|
* @todo make deprecated |
213
|
|
|
*/ |
214
|
|
|
private function invalidOperator($operator) |
215
|
|
|
{ |
216
|
|
|
return !in_array($operator, $this->getGrammar()->getOperators()); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Set an query binding |
221
|
|
|
* @param string $key @todo validate if the $key is valid with the grammar |
222
|
|
|
* @param mixed $data |
223
|
|
|
* @return bool |
224
|
|
|
*/ |
225
|
35 |
|
private function setBinding($key, $data) |
226
|
|
|
{ |
227
|
35 |
|
return $this->getBindings()->bind($key, $data); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Append an query binding |
232
|
|
|
* @param string $key @todo validate if the $key is valid with the grammar |
233
|
|
|
* @param mixed $data |
234
|
|
|
* @return bool |
235
|
|
|
*/ |
236
|
14 |
|
private function appendBinding($key, $data) |
237
|
|
|
{ |
238
|
14 |
|
return $this->getBindings()->append($key, $data); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
|
242
|
11 |
|
private function getBinding($key) |
|
|
|
|
243
|
|
|
{ |
244
|
11 |
|
return $this->getBindings()->get($key); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.