1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Samrap\Acf\Fluent; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use Samrap\Acf\Exceptions\BuilderException; |
7
|
|
|
|
8
|
|
|
class Builder |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The Builder's runner. |
12
|
|
|
* |
13
|
|
|
* @var \Samrap\Acf\Fluent\Runner |
14
|
|
|
*/ |
15
|
|
|
protected $runner; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* An array of functions callable as methods of this class. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $macros; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The field name or key to build off of. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
public $field; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The post ID to get the field from. |
33
|
|
|
* |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
public $id; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The internal type to expect when retrieving a value. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
public $expect; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The default value to use when retrieving a value that is null or does not |
47
|
|
|
* pass components added to the builder, such as the 'expect' component. |
48
|
|
|
* |
49
|
|
|
* @var mixed |
50
|
|
|
*/ |
51
|
|
|
public $default; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The function to use to escape a retrieved value. |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
public $escape; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Whether or not to do shortcodes. |
62
|
|
|
* |
63
|
|
|
* @var bool |
64
|
|
|
*/ |
65
|
|
|
public $shortcodes; |
66
|
|
|
|
67
|
|
|
/* |
68
|
|
|
* Get the field raw (unformatted). |
69
|
|
|
* |
70
|
|
|
* @var bool |
71
|
|
|
*/ |
72
|
|
|
public $raw = false; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* The regular expression that the field's value must match. |
76
|
|
|
* |
77
|
|
|
* @var string |
78
|
|
|
*/ |
79
|
|
|
public $matches; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Create a new Builder instance. |
83
|
|
|
* |
84
|
|
|
* @param \Samrap\Acf\Fluent\Runner $runner |
85
|
|
|
*/ |
86
|
|
|
public function __construct(Runner $runner, array $macros = []) |
87
|
|
|
{ |
88
|
|
|
$this->runner = $runner; |
89
|
|
|
$this->macros = $macros; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the runner instance. |
94
|
|
|
* |
95
|
|
|
* @return \Samrap\Acf\Fluent\Runner |
96
|
|
|
*/ |
97
|
|
|
public function getRunner() |
98
|
|
|
{ |
99
|
|
|
return $this->runner; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set the field component. |
104
|
|
|
* |
105
|
|
|
* @param string $name |
106
|
|
|
* @return \Samrap\Acf\Fluent\Builder |
107
|
|
|
*/ |
108
|
|
|
public function field($name) |
109
|
|
|
{ |
110
|
|
|
$this->field = $name; |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set the post component. |
117
|
|
|
* |
118
|
|
|
* @param int $id |
119
|
|
|
* @return \Samrap\Acf\Fluent\Builder |
120
|
|
|
*/ |
121
|
|
|
public function id($id) |
122
|
|
|
{ |
123
|
|
|
$this->id = $id; |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set the expect component. |
130
|
|
|
* |
131
|
|
|
* @param string $type |
132
|
|
|
* @return \Samrap\Acf\Fluent\Builder |
133
|
|
|
*/ |
134
|
|
|
public function expect($type) |
135
|
|
|
{ |
136
|
|
|
$this->expect = $type; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set the default component. |
143
|
|
|
* |
144
|
|
|
* @param string $default |
145
|
|
|
* @return \Samrap\Acf\Fluent\Builder |
146
|
|
|
*/ |
147
|
|
|
public function default($default) |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
$this->default = $default; |
150
|
|
|
|
151
|
|
|
return $this; |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set the escape component. |
156
|
|
|
* |
157
|
|
|
* @param callable $func |
158
|
|
|
* @return \Samrap\Acf\Fluent\Builder |
159
|
|
|
*/ |
160
|
|
|
public function escape($func = 'esc_html') |
161
|
|
|
{ |
162
|
|
|
// It is up to the runner to prevent malicious code. |
163
|
|
|
$this->escape = $func; |
|
|
|
|
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Set the shortcodes component. |
170
|
|
|
* |
171
|
|
|
* @return \Samrap\Acf\Fluent\Builder |
172
|
|
|
*/ |
173
|
|
|
public function shortcodes() |
174
|
|
|
{ |
175
|
|
|
$this->shortcodes = true; |
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/* |
181
|
|
|
* Set the raw component. |
182
|
|
|
* |
183
|
|
|
* @return \Samrap\Acf\Fluent\Builder |
184
|
|
|
*/ |
185
|
|
|
public function raw() |
186
|
|
|
{ |
187
|
|
|
$this->raw = true; |
188
|
|
|
|
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Set the matches component. |
194
|
|
|
* |
195
|
|
|
* @param string $pattern |
196
|
|
|
* @return \Samrap\Acf\Fluent\Builder |
197
|
|
|
*/ |
198
|
|
|
public function matches($pattern) |
199
|
|
|
{ |
200
|
|
|
$this->matches = $pattern; |
201
|
|
|
|
202
|
|
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Pass the builder to the runner's get method. |
207
|
|
|
* |
208
|
|
|
* @return mixed |
209
|
|
|
*/ |
210
|
|
|
public function get() |
211
|
|
|
{ |
212
|
|
|
if (is_null($this->field)) { |
213
|
|
|
throw new BuilderException('Cannot get a null field.'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $this->runner->get($this); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Pass the builder and the given value to the runner's update method. |
221
|
|
|
* |
222
|
|
|
* @param mixed $value |
223
|
|
|
* @return void |
224
|
|
|
*/ |
225
|
|
|
public function update($value) |
226
|
|
|
{ |
227
|
|
|
return $this->runner->update($this, $value); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Call a macro if it exists. |
232
|
|
|
* |
233
|
|
|
* @param string $name |
234
|
|
|
* @param array $arguments |
235
|
|
|
* @return \Samrap\Acf\Fluent\Builder |
236
|
|
|
*/ |
237
|
|
|
public function __call($name, $arguments) |
238
|
|
|
{ |
239
|
|
|
if (! isset($this->macros[$name])) { |
240
|
|
|
throw new BadMethodCallException( |
241
|
|
|
"The method or macro {$name} does not exist." |
242
|
|
|
); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$this->macros[$name]($this, ...$arguments); |
246
|
|
|
|
247
|
|
|
return $this; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.