1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StoutLogic\AcfBuilder; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Builds configurations for an ACF Field |
7
|
|
|
*/ |
8
|
|
|
class FieldBuilder extends ParentDelegationBuilder implements NamedBuilder |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Field Type |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $type; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Additional Field Configuration |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $config; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $name Field Name, conventionally 'snake_case'. |
24
|
|
|
* @param string $type Field Type. |
25
|
|
|
* @param array $config Additional Field Configuration. |
26
|
|
|
*/ |
27
|
|
|
public function __construct($name, $type, $config = []) |
28
|
|
|
{ |
29
|
|
|
$this->config = [ |
30
|
|
|
'name' => $name, |
31
|
|
|
'label' => $this->generateLabel($name), |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
$this->type = $type; |
35
|
|
|
$this->setKey($name); |
36
|
|
|
$this->updateConfig($config); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
private function getConfig() |
43
|
|
|
{ |
44
|
|
|
return $this->config; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Set a config key -> value pair |
49
|
|
|
* @param string $key |
50
|
|
|
* @param mixed $value |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
|
|
public function setConfig($key, $value) |
54
|
|
|
{ |
55
|
|
|
return $this->updateConfig([$key => $value]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Update multiple config values using and array of key -> value pairs. |
60
|
|
|
* @param array $config |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
|
|
public function updateConfig($config) |
64
|
|
|
{ |
65
|
|
|
$this->config = array_merge($this->config, $config); |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function getName() |
73
|
|
|
{ |
74
|
|
|
return $this->config['name']; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function getKey() |
81
|
|
|
{ |
82
|
|
|
return $this->config['key']; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getLabel() |
89
|
|
|
{ |
90
|
|
|
return $this->config['label']; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Will prepend `field_` if missing. |
95
|
|
|
* @param string $key |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
|
|
public function setKey($key) |
99
|
|
|
{ |
100
|
|
|
if (!preg_match('/^field_/', $key)) { |
101
|
|
|
$key = 'field_'.$key; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->setConfig('key', $key); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Will set field required. |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
|
|
public function setRequired() |
112
|
|
|
{ |
113
|
|
|
return $this->setConfig('required', 1); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Will set field unrequired. |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
|
|
public function setUnrequired() |
121
|
|
|
{ |
122
|
|
|
return $this->setConfig('required', 0); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Will set field's instructions. |
127
|
|
|
* @param string $instructions |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
|
|
public function setInstructions($instructions) |
131
|
|
|
{ |
132
|
|
|
return $this->setConfig('instructions', $instructions); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Will set field's defaultValue. |
137
|
|
|
* @param string $defaultValue |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
|
|
public function setDefaultValue($defaultValue) |
141
|
|
|
{ |
142
|
|
|
return $this->setConfig('default_value', $defaultValue); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Add a conditional logic statement that will determine if the last added |
147
|
|
|
* field will display or not. You can add `or` or `and` calls after |
148
|
|
|
* to build complex logic. Any other function call will return you to the |
149
|
|
|
* parentContext. |
150
|
|
|
* @param string $name Dependent field name |
151
|
|
|
* (choice type: radio, checkbox, select, trueFalse) |
152
|
|
|
* @param string $operator ==, != |
153
|
|
|
* @param string $value 1 or choice value |
154
|
|
|
* @return ConditionalBuilder |
155
|
|
|
*/ |
156
|
|
|
public function conditional($name, $operator, $value) |
157
|
|
|
{ |
158
|
|
|
$conditionalBuilder = new ConditionalBuilder($name, $operator, $value); |
159
|
|
|
$conditionalBuilder->setParentContext($this); |
160
|
|
|
|
161
|
|
|
$this->setConfig('conditional_logic', $conditionalBuilder); |
162
|
|
|
|
163
|
|
|
return $conditionalBuilder; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Build the field configuration array |
168
|
|
|
* @return array Field configuration array |
169
|
|
|
*/ |
170
|
|
|
public function build() |
171
|
|
|
{ |
172
|
|
|
$config = array_merge([ |
173
|
|
|
'type' => $this->type, |
174
|
|
|
], $this->getConfig()); |
175
|
|
|
|
176
|
|
|
foreach ($config as $key => $value) { |
177
|
|
|
if ($value instanceof Builder) { |
178
|
|
|
$config[$key] = $value->build(); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $config; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Create a field label based on the field's name. Generates title case. |
187
|
|
|
* @param string $name |
188
|
|
|
* @return string label |
189
|
|
|
*/ |
190
|
|
|
protected function generateLabel($name) |
191
|
|
|
{ |
192
|
|
|
return ucwords(str_replace("_", " ", $name)); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Generates a snaked cased name. |
197
|
|
|
* @param string $name |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
protected function generateName($name) |
201
|
|
|
{ |
202
|
|
|
return strtolower(str_replace(" ", "_", $name)); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|