Passed
Push — master ( 43d1b6...68e370 )
by Steve
01:15
created

FieldBuilder::getWrapper()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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
    /**
141
     * Will set field required.
142
     * @return $this
143
     */
144
    public function setRequired()
145
    {
146
        return $this->setConfig('required', 1);
147
    }
148
149
    /**
150
     * Will set field unrequired.
151
     * @return $this
152
     */
153
    public function setUnrequired()
154
    {
155
        return $this->setConfig('required', 0);
156
    }
157
158
    /**
159
     * Will set field's instructions.
160
     * @param string $instructions
161
     * @return $this
162
     */
163
    public function setInstructions($instructions)
164
    {
165
        return $this->setConfig('instructions', $instructions);
166
    }
167
168
    /**
169
     * Will set field's defaultValue.
170
     * @param string $defaultValue
171
     * @return $this
172
     */
173
    public function setDefaultValue($defaultValue)
174
    {
175
        return $this->setConfig('default_value', $defaultValue);
176
    }
177
178
    /**
179
     * Add a conditional logic statement that will determine if the last added
180
     * field will display or not. You can add `or` or `and` calls after
181
     * to build complex logic. Any other function call will return you to the
182
     * parentContext.
183
     * @param  string $name Dependent field name
184
     *                      (choice type: radio, checkbox, select, trueFalse)
185
     * @param  string $operator ==, !=
186
     * @param  string $value    1 or choice value
187
     * @return ConditionalBuilder
188
     */
189
    public function conditional($name, $operator, $value)
190
    {
191
        $conditionalBuilder = new ConditionalBuilder($name, $operator, $value);
192
        $conditionalBuilder->setParentContext($this);
193
194
        $this->setConfig('conditional_logic', $conditionalBuilder);
195
196
        return $conditionalBuilder;
197
    }
198
199
    /**
200
     * Set Wrapper container tag attributes
201
     *
202
     * @param array $config
203
     *
204
     * @return FieldBuilder
205
     */
206
    public function setWrapper($config)
207
    {
208
        return $this->setConfig('wrapper', $config);
209
    }
210
211
    /**
212
     * Get Wrapper container tag attributes
213
     *
214
     * @return array|mixed
215
     */
216
    public function getWrapper()
217
    {
218
        if (isset($this->config['wrapper'])) {
219
            return $this->config['wrapper'];
220
        }
221
222
        return [];
223
    }
224
225
    /**
226
     * Set width of a Wrapper container
227
     *
228
     * @param string $width Width of a container in % or px.
229
     *
230
     * @return FieldBuilder
231
     */
232
    public function setWidth($width)
233
    {
234
        $wrapper = $this->getWrapper();
235
        $wrapper['width'] = $width;
236
237
        return $this->setWrapper($wrapper);
238
    }
239
240
    /**
241
     * Set specified Attr of a Wrapper container
242
     *
243
     * @param string $name Attribute name, ex. 'class'.
244
     * @param string|null $value Attribute value, ex. 'my-class'.
245
     *
246
     * @return FieldBuilder
247
     */
248
    public function setAttr($name, $value = null)
249
    {
250
        $wrapper = $this->getWrapper();
251
        
252
        // set attribute.
253
        $wrapper[$name] = $value;
254
255
        return $this->setWrapper($wrapper);
256
    }
257
258
    /**
259
     * Set Class and/or ID attribute of a Wrapper container
260
     * use CSS-like selector string to specify css or id
261
     * example: #my-id.foo-class.bar-class
262
     *
263
     * @param string $css_selector
264
     *
265
     * @return FieldBuilder
266
     */
267
    public function setSelector($css_selector)
268
    {
269
        // if # is the first sign - we start with ID.
270
        if (0 === strpos($css_selector, '#')) {
271
            $css_selector .= '.'; // prevent empty second part.
272
            list($id, $class) = explode('.', $css_selector, 2);
273
        } else {
274
            $css_selector .= '#'; // prevent empty second part.
275
            list($class, $id) = explode('#', $css_selector, 2);
276
        }
277
278
        $id = trim($id, '#');
279
        $class = trim($class, '.');
280
281
        if (! empty($id)) {
282
            $this->setAttr('id', $id);
283
        }
284
285
        if (! empty($class)) {
286
            $class = str_replace('.', ' ', $class);
287
            $this->setAttr('class', $class);
288
        }
289
290
        return $this;
291
    }
292
293
    /**
294
     * Build the field configuration array
295
     * @return array Field configuration array
296
     */
297
    public function build()
298
    {
299
        $config = array_merge([
300
            'type' => $this->type,
301
        ], $this->getConfig());
302
303
        foreach ($config as $key => $value) {
304
            if ($value instanceof Builder) {
305
                $config[$key] = $value->build();
306
            }
307
        }
308
309
        return $config;
310
    }
311
312
    /**
313
     * Create a field label based on the field's name. Generates title case.
314
     * @param  string $name
315
     * @return string label
316
     */
317
    protected function generateLabel($name)
318
    {
319
        return ucwords(str_replace("_", " ", $name));
320
    }
321
322
    /**
323
     * Generates a snaked cased name.
324
     * @param  string $name
325
     * @return string
326
     */
327
    protected function generateName($name)
328
    {
329
        return strtolower(str_replace(" ", "_", $name));
330
    }
331
}
332