Passed
Branch develop (4c9db4)
by Steve
27:39
created

RepeaterBuilder::addFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace StoutLogic\AcfBuilder;
4
5
/**
6
 * Repeater field
7
 * Can add multiple fields as subfields to the repeater.
8
 * @method FieldBuilder addField(string $name, string $type, array $args = [])
9
 * @method FieldBuilder addChoiceField(string $name, string $type, array $args = [])
10
 * @method FieldBuilder addText(string $name, array $args = [])
11
 * @method FieldBuilder addTextarea(string $name, array $args = [])
12
 * @method FieldBuilder addNumber(string $name, array $args = [])
13
 * @method FieldBuilder addEmail(string $name, array $args = [])
14
 * @method FieldBuilder addUrl(string $name, array $args = [])
15
 * @method FieldBuilder addPassword(string $name, array $args = [])
16
 * @method FieldBuilder addWysiwyg(string $name, array $args = [])
17
 * @method FieldBuilder addOembed(string $name, array $args = [])
18
 * @method FieldBuilder addImage(string $name, array $args = [])
19
 * @method FieldBuilder addFile(string $name, array $args = [])
20
 * @method FieldBuilder addGallery(string $name, array $args = [])
21
 * @method FieldBuilder addTrueFalse(string $name, array $args = [])
22
 * @method FieldBuilder addSelect(string $name, array $args = [])
23
 * @method FieldBuilder addRadio(string $name, array $args = [])
24
 * @method FieldBuilder addCheckbox(string $name, array $args = [])
25
 * @method FieldBuilder addPostObject(string $name, array $args = [])
26
 * @method FieldBuilder addPostLink(string $name, array $args = [])
27
 * @method FieldBuilder addTaxonomy(string $name, array $args = [])
28
 * @method FieldBuilder addUser(string $name, array $args = [])
29
 * @method FieldBuilder addDatePicker(string $name, array $args = [])
30
 * @method FieldBuilder addTimePicker(string $name, array $args = [])
31
 * @method FieldBuilder addDateTimePicker(string $name, array $args = [])
32
 * @method FieldBuilder addColorPicker(string $name, array $args = [])
33
 * @method FieldBuilder addTab(string $label, array $args = [])
34
 * @method FieldBuilder addMessage(string $label, string $message, array $args = [])
35
 * @method FieldBuilder addRepeater(string $name, array $args = [])
36
 * @method FieldBuilder addFlexibleContent(string $name, array $args = [])
37
 */
38
class RepeaterBuilder extends FieldBuilder
39
{
40
    /**
41
     * Used to contain and add fields
42
     * @var FieldsBuilder
43
     */
44
    private $fieldsBuilder;
45
46
    /**
47
     * @param string $name Field name
48
     * @param string $type Field name
49
     * @param array $config Field configuration
50
     */
51
    public function __construct($name, $type = 'repeater', $config = [])
52
    {
53
        parent::__construct($name, $type, $config);
54
        $this->fieldsBuilder = new FieldsBuilder($name);
55
        $this->fieldsBuilder->setParentContext($this);
56
    }
57
58
    /**
59
     * Return a repeater field configuration array
60
     * @return array
61
     */
62
    public function build()
63
    {
64
        $config = parent::build();
65
        $fields = $this->fieldsBuilder->build();
66
        $config['sub_fields'] = $fields['fields'];
67
        return $config;
68
    }
69
70
    /**
71
     * Returns call chain to parentContext
72
     * @return Builder
73
     */
74
    public function endRepeater()
75
    {
76
        return $this->getParentContext();
77
    }
78
79
    /**
80
     * Add multiple fields either via an array or from another builder
81
     * @param mixed $fields array of fields or a FieldBuilder
82
     * @return $this
83
     */
84
    public function addFields($fields)
85
    {
86
        $this->fieldsBuilder->addFields($fields);
87
        return $this;
88
    }
89
90
    /**
91
     * Intercept missing methods, pass any methods that begin with add to the
92
     * internal fieldsBuilder
93
     * @param  string $method
94
     * @param  array $args
95
     * @return mixed
96
     */
97
    public function __call($method, $args)
98
    {
99
        if (preg_match('/^add.+/', $method) && method_exists($this->fieldsBuilder, $method)) {
100
            $field = $this->callAddFieldMethod($method, $args);
101
            $field->setParentContext($this);
102
            return $field;
103
        }
104
105
        return parent::__call($method, $args);
106
    }
107
108
    /**
109
     * Calls an add field method on the FieldsBuilder
110
     * @param string $method [description]
111
     * @param array $args
112
     * @return FieldBuilder
113
     */
114
    private function callAddFieldMethod($method, $args)
115
    {
116
        return call_user_func_array([$this->fieldsBuilder, $method], $args);
117
    }
118
}
119