Completed
Push — master ( 7ba73a...cfde8a )
by Steve
29s
created

RepeaterBuilder::addFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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