1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StoutLogic\AcfBuilder; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Builds configurations for an ACF Field |
7
|
|
|
* @method FieldBuilder addField(string $name, string $type, array $args = []) |
8
|
|
|
* @method FieldBuilder addChoiceField(string $name, string $type, array $args = []) |
9
|
|
|
* @method FieldBuilder addText(string $name, array $args = []) |
10
|
|
|
* @method FieldBuilder addTextarea(string $name, array $args = []) |
11
|
|
|
* @method FieldBuilder addNumber(string $name, array $args = []) |
12
|
|
|
* @method FieldBuilder addEmail(string $name, array $args = []) |
13
|
|
|
* @method FieldBuilder addUrl(string $name, array $args = []) |
14
|
|
|
* @method FieldBuilder addPassword(string $name, array $args = []) |
15
|
|
|
* @method FieldBuilder addWysiwyg(string $name, array $args = []) |
16
|
|
|
* @method FieldBuilder addOembed(string $name, array $args = []) |
17
|
|
|
* @method FieldBuilder addImage(string $name, array $args = []) |
18
|
|
|
* @method FieldBuilder addFile(string $name, array $args = []) |
19
|
|
|
* @method FieldBuilder addGallery(string $name, array $args = []) |
20
|
|
|
* @method FieldBuilder addTrueFalse(string $name, array $args = []) |
21
|
|
|
* @method FieldBuilder addSelect(string $name, array $args = []) |
22
|
|
|
* @method FieldBuilder addRadio(string $name, array $args = []) |
23
|
|
|
* @method FieldBuilder addCheckbox(string $name, array $args = []) |
24
|
|
|
* @method FieldBuilder addPostObject(string $name, array $args = []) |
25
|
|
|
* @method FieldBuilder addPageLink(string $name, array $args = []) |
26
|
|
|
* @method FieldBuilder addTaxonomy(string $name, array $args = []) |
27
|
|
|
* @method FieldBuilder addUser(string $name, array $args = []) |
28
|
|
|
* @method FieldBuilder addDatePicker(string $name, array $args = []) |
29
|
|
|
* @method FieldBuilder addTimePicker(string $name, array $args = []) |
30
|
|
|
* @method FieldBuilder addDateTimePicker(string $name, array $args = []) |
31
|
|
|
* @method FieldBuilder addColorPicker(string $name, array $args = []) |
32
|
|
|
* @method FieldBuilder addGoogleMap(string $name, array $args = []) |
33
|
|
|
* @method FieldBuilder addLink(string $name, array $args = []) |
34
|
|
|
* @method FieldBuilder addTab(string $label, array $args = []) |
35
|
|
|
* @method FieldBuilder addMessage(string $label, string $message, array $args = []) |
36
|
|
|
* @method GroupBuilder addGroup(string $name, array $args = []) |
37
|
|
|
* @method RepeaterBuilder addRepeater(string $name, array $args = []) |
38
|
|
|
* @method FlexibleContentBuilder addFlexibleContent(string $name, array $args = []) |
39
|
|
|
* @method FieldsBuilder addLayout(string|FieldsBuilder $layout, array $args = []) |
40
|
|
|
*/ |
41
|
|
|
class FieldBuilder extends ParentDelegationBuilder implements NamedBuilder |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* Field Type |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $type; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Additional Field Configuration |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
private $config; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $name Field Name, conventionally 'snake_case'. |
57
|
|
|
* @param string $type Field Type. |
58
|
|
|
* @param array $config Additional Field Configuration. |
59
|
|
|
*/ |
60
|
|
|
public function __construct($name, $type, $config = []) |
61
|
|
|
{ |
62
|
|
|
$this->config = [ |
63
|
|
|
'name' => $name, |
64
|
|
|
'label' => $this->generateLabel($name), |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
$this->type = $type; |
68
|
|
|
$this->setKey($name); |
69
|
|
|
$this->updateConfig($config); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
private function getConfig() |
76
|
|
|
{ |
77
|
|
|
return $this->config; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set a config key -> value pair |
82
|
|
|
* @param string $key |
83
|
|
|
* @param mixed $value |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
|
|
public function setConfig($key, $value) |
87
|
|
|
{ |
88
|
|
|
return $this->updateConfig([$key => $value]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Update multiple config values using and array of key -> value pairs. |
93
|
|
|
* @param array $config |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function updateConfig($config) |
97
|
|
|
{ |
98
|
|
|
$this->config = array_merge($this->config, $config); |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function getName() |
106
|
|
|
{ |
107
|
|
|
return $this->config['name']; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getKey() |
114
|
|
|
{ |
115
|
|
|
return $this->config['key']; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getLabel() |
122
|
|
|
{ |
123
|
|
|
return $this->config['label']; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Will prepend `field_` if missing. |
128
|
|
|
* @param string $key |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
|
|
public function setKey($key) |
132
|
|
|
{ |
133
|
|
|
if (!preg_match('/^field_/', $key)) { |
134
|
|
|
$key = 'field_'.$key; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $this->setConfig('key', $key); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function setCustomKey($key) |
141
|
|
|
{ |
142
|
|
|
return $this |
143
|
|
|
->setConfig('key', $key) |
144
|
|
|
->setConfig('_has_custom_key', true); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function hasCustomKey() |
151
|
|
|
{ |
152
|
|
|
return array_key_exists('_has_custom_key', $this->config) && $this->config['_has_custom_key']; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Will set field required. |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
|
|
public function setRequired() |
161
|
|
|
{ |
162
|
|
|
return $this->setConfig('required', 1); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Will set field unrequired. |
167
|
|
|
* @return $this |
168
|
|
|
*/ |
169
|
|
|
public function setUnrequired() |
170
|
|
|
{ |
171
|
|
|
return $this->setConfig('required', 0); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Will set field's instructions. |
176
|
|
|
* @param string $instructions |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
|
|
public function setInstructions($instructions) |
180
|
|
|
{ |
181
|
|
|
return $this->setConfig('instructions', $instructions); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Will set field's defaultValue. |
186
|
|
|
* @param string $defaultValue |
187
|
|
|
* @return $this |
188
|
|
|
*/ |
189
|
|
|
public function setDefaultValue($defaultValue) |
190
|
|
|
{ |
191
|
|
|
return $this->setConfig('default_value', $defaultValue); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Add a conditional logic statement that will determine if the last added |
196
|
|
|
* field will display or not. You can add `or` or `and` calls after |
197
|
|
|
* to build complex logic. Any other function call will return you to the |
198
|
|
|
* parentContext. |
199
|
|
|
* @param string $name Dependent field name |
200
|
|
|
* (choice type: radio, checkbox, select, trueFalse) |
201
|
|
|
* @param string $operator ==, != |
202
|
|
|
* @param string $value 1 or choice value |
203
|
|
|
* @return ConditionalBuilder |
204
|
|
|
*/ |
205
|
|
|
public function conditional($name, $operator, $value) |
206
|
|
|
{ |
207
|
|
|
$conditionalBuilder = new ConditionalBuilder($name, $operator, $value); |
208
|
|
|
$conditionalBuilder->setParentContext($this); |
209
|
|
|
|
210
|
|
|
$this->setConfig('conditional_logic', $conditionalBuilder); |
211
|
|
|
|
212
|
|
|
return $conditionalBuilder; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Build the field configuration array |
217
|
|
|
* @return array Field configuration array |
218
|
|
|
*/ |
219
|
|
|
public function build() |
220
|
|
|
{ |
221
|
|
|
$config = array_merge([ |
222
|
|
|
'type' => $this->type, |
223
|
|
|
], $this->getConfig()); |
224
|
|
|
|
225
|
|
|
foreach ($config as $key => $value) { |
226
|
|
|
if ($value instanceof Builder) { |
227
|
|
|
$config[$key] = $value->build(); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return $config; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Create a field label based on the field's name. Generates title case. |
236
|
|
|
* @param string $name |
237
|
|
|
* @return string label |
238
|
|
|
*/ |
239
|
|
|
protected function generateLabel($name) |
240
|
|
|
{ |
241
|
|
|
return ucwords(str_replace("_", " ", $name)); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Generates a snaked cased name. |
246
|
|
|
* @param string $name |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
|
|
protected function generateName($name) |
250
|
|
|
{ |
251
|
|
|
return strtolower(str_replace(" ", "_", $name)); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|