Test Failed
Push — master ( c207a3...da2a21 )
by Steve
09:23 queued 12s
created

FieldBuilder::setCustomKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 addRange(string $name, array $args = [])
36
 * @method FieldBuilder addMessage(string $label, string $message, array $args = [])
37
 * @method GroupBuilder addGroup(string $name, array $args = [])
38
 * @method GroupBuilder endGroup()
39
 * @method RepeaterBuilder addRepeater(string $name, array $args = [])
40
 * @method Builder endRepeater()
41
 * @method FlexibleContentBuilder addFlexibleContent(string $name, array $args = [])
42
 * @method FieldsBuilder addLayout(string|FieldsBuilder $layout, array $args = [])
43
 * @method LocationBuilder setLocation(string $param, string $operator, string $value)
44
 */
45
class FieldBuilder extends ParentDelegationBuilder implements NamedBuilder
46
{
47
    /**
48
     * Field Type
49
     * @var string
50
     */
51
    private $type;
52
53
    /**
54
     * Additional Field Configuration
55
     * @var array
56
     */
57
    private $config;
58
59
    /**
60
     * @param string $name Field Name, conventionally 'snake_case'.
61
     * @param string $type Field Type.
62
     * @param array $config Additional Field Configuration.
63
     */
64
    public function __construct($name, $type, $config = [])
65
    {
66
        $this->config = [
67
            'name' => $name,
68
            'label' => $this->generateLabel($name),
69
        ];
70
71
        $this->type = $type;
72
        $this->setKey($name);
73
        $this->updateConfig($config);
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    public function getConfig()
80
    {
81
        return $this->config;
82
    }
83
84
    /**
85
     * Set a config key -> value pair
86
     * @param string $key
87
     * @param mixed $value
88
     * @return $this
89
     */
90
    public function setConfig($key, $value)
91
    {
92
        return $this->updateConfig([$key => $value]);
93
    }
94
95
    /**
96
     * Update multiple config values using and array of key -> value pairs.
97
     * @param  array $config
98
     * @return $this
99
     */
100
    public function updateConfig($config)
101
    {
102
        $this->config = array_merge($this->config, $config);
103
        return $this;
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function getName()
110
    {
111
        return $this->config['name'];
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    public function getKey()
118
    {
119
        return $this->config['key'];
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    public function getLabel()
126
    {
127
        return $this->config['label'];
128
    }
129
130
    /**
131
     * Will prepend `field_` if missing.
132
     * @param string $key
133
     * @return $this
134
     */
135
    public function setKey($key)
136
    {
137
        if (!preg_match('/^field_/', $key)) {
138
            $key = 'field_'.$key;
139
        }
140
141
        return $this->setConfig('key', $key);
142
    }
143
144
    public function setCustomKey($key)
145
    {
146
        return $this
147
            ->setConfig('key', $key)
148
            ->setConfig('_has_custom_key', true);
149
    }
150
151
    /**
152
     * @return bool
153
     */
154
    public function hasCustomKey()
155
    {
156
        return array_key_exists('_has_custom_key', $this->config) && $this->config['_has_custom_key'];
157
    }
158
159
160
    /**
161
     * Will set field required.
162
     * @return $this
163
     */
164
    public function setRequired()
165
    {
166
        return $this->setConfig('required', 1);
167
    }
168
169
    /**
170
     * Will set field unrequired.
171
     * @return $this
172
     */
173
    public function setUnrequired()
174
    {
175
        return $this->setConfig('required', 0);
176
    }
177
178
    /**
179
     * Will set field's label.
180
     * @param string $label
181
     * @return $this
182
     */
183
    public function setLabel($label)
184
    {
185
        return $this->setConfig('label', $label);
186
    }
187
188
    /**
189
     * Will set field's instructions.
190
     * @param string $instructions
191
     * @return $this
192
     */
193
    public function setInstructions($instructions)
194
    {
195
        return $this->setConfig('instructions', $instructions);
196
    }
197
198
    /**
199
     * Will set field's defaultValue.
200
     * @param string $defaultValue
201
     * @return $this
202
     */
203
    public function setDefaultValue($defaultValue)
204
    {
205
        return $this->setConfig('default_value', $defaultValue);
206
    }
207
208
    /**
209
     * Add a conditional logic statement that will determine if the last added
210
     * field will display or not. You can add `or` or `and` calls after
211
     * to build complex logic. Any other function call will return you to the
212
     * parentContext.
213
     * @param  string $name Dependent field name
214
     *                      (choice type: radio, checkbox, select, trueFalse)
215
     * @param  string $operator ==, !=
216
     * @param  string $value    1 or choice value
217
     * @return ConditionalBuilder
218
     */
219
    public function conditional($name, $operator, $value)
220
    {
221
        $conditionalBuilder = new ConditionalBuilder($name, $operator, $value);
222
        $conditionalBuilder->setParentContext($this);
223
224
        $this->setConfig('conditional_logic', $conditionalBuilder);
225
226
        return $conditionalBuilder;
227
    }
228
229
    /**
230
     * Set Wrapper container tag attributes
231
     *
232
     * @param array $config
233
     *
234
     * @return FieldBuilder
235
     */
236
    public function setWrapper($config)
237
    {
238
        return $this->setConfig('wrapper', $config);
239
    }
240
241
    /**
242
     * Get Wrapper container tag attributes
243
     *
244
     * @return array|mixed
245
     */
246
    public function getWrapper()
247
    {
248
        if (isset($this->config['wrapper'])) {
249
            return $this->config['wrapper'];
250
        }
251
252
        return [];
253
    }
254
255
    /**
256
     * Set width of a Wrapper container
257
     *
258
     * @param string $width Width of a container in % or px.
259
     *
260
     * @return FieldBuilder
261
     */
262
    public function setWidth($width)
263
    {
264
        $wrapper = $this->getWrapper();
265
        $wrapper['width'] = $width;
266
267
        return $this->setWrapper($wrapper);
268
    }
269
270
    /**
271
     * Set specified Attr of a Wrapper container
272
     *
273
     * @param string $name Attribute name, ex. 'class'.
274
     * @param string|null $value Attribute value, ex. 'my-class'.
275
     *
276
     * @return FieldBuilder
277
     */
278
    public function setAttr($name, $value = null)
279
    {
280
        $wrapper = $this->getWrapper();
281
282
        // set attribute.
283
        $wrapper[$name] = $value;
284
285
        return $this->setWrapper($wrapper);
286
    }
287
288
    /**
289
     * Set Class and/or ID attribute of a Wrapper container
290
     * use CSS-like selector string to specify css or id
291
     * example: #my-id.foo-class.bar-class
292
     *
293
     * @param string $css_selector
294
     *
295
     * @return FieldBuilder
296
     */
297
    public function setSelector($css_selector)
298
    {
299
        // if # is the first sign - we start with ID.
300
        if (0 === strpos($css_selector, '#')) {
301
            $css_selector .= '.'; // prevent empty second part.
302
            list($id, $class) = explode('.', $css_selector, 2);
303
        } else {
304
            $css_selector .= '#'; // prevent empty second part.
305
            list($class, $id) = explode('#', $css_selector, 2);
306
        }
307
308
        $id = trim($id, '#');
309
        $class = trim($class, '.');
310
311
        if (! empty($id)) {
312
            $this->setAttr('id', $id);
313
        }
314
315
        if (! empty($class)) {
316
            $class = str_replace('.', ' ', $class);
317
            $this->setAttr('class', $class);
318
        }
319
320
        return $this;
321
    }
322
323
    /**
324
     * Build the field configuration array
325
     * @return array Field configuration array
326
     */
327
    public function build()
328
    {
329
        $config = array_merge([
330
            'type' => $this->type,
331
        ], $this->getConfig());
332
333
        foreach ($config as $key => $value) {
334
            if ($value instanceof Builder) {
335
                $config[$key] = $value->build();
336
            }
337
        }
338
339
        return $config;
340
    }
341
342
    /**
343
     * Create a field label based on the field's name. Generates title case.
344
     * @param  string $name
345
     * @return string label
346
     */
347
    protected function generateLabel($name)
348
    {
349
        return ucwords(str_replace("_", " ", $name));
350
    }
351
352
    /**
353
     * Generates a snaked cased name.
354
     * @param  string $name
355
     * @return string
356
     */
357
    protected function generateName($name)
358
    {
359
        return strtolower(str_replace(" ", "_", $name));
360
    }
361
}
362