Passed
Branch develop (d96422)
by Steve
03:03
created

RepeaterBuilder   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 7
Bugs 1 Features 1
Metric Value
wmc 10
c 7
b 1
f 1
lcom 1
cbo 2
dl 0
loc 94
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A build() 0 7 1
A endRepeater() 0 4 1
A addFields() 0 5 1
A __call() 0 10 3
A callAddFieldMethod() 0 4 1
A getDefaultButtonLabel() 0 4 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
    protected $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
        if (!array_key_exists('button_label', $config)) {
58
            $this->setConfig('button_label', $this->getDefaultButtonLabel());
59
        }
60
    }
61
62
    /**
63
     * Return a repeater field configuration array
64
     * @return array
65
     */
66
    public function build()
67
    {
68
        $config = parent::build();
69
        $fields = $this->fieldsBuilder->build();
70
        $config['sub_fields'] = $fields['fields'];
71
        return $config;
72
    }
73
74
    /**
75
     * Returns call chain to parentContext
76
     * @return Builder
77
     */
78
    public function endRepeater()
79
    {
80
        return $this->getParentContext();
81
    }
82
83
    /**
84
     * Add multiple fields either via an array or from another builder
85
     * @param mixed $fields array of fields or a FieldBuilder
86
     * @return $this
87
     */
88
    public function addFields($fields)
89
    {
90
        $this->fieldsBuilder->addFields($fields);
91
        return $this;
92
    }
93
94
    /**
95
     * Intercept missing methods, pass any methods that begin with add to the
96
     * internal fieldsBuilder
97
     * @param  string $method
98
     * @param  array $args
99
     * @return mixed
100
     */
101
    public function __call($method, $args)
102
    {
103
        if (preg_match('/^add.+/', $method) && method_exists($this->fieldsBuilder, $method)) {
104
            $field = $this->callAddFieldMethod($method, $args);
105
            $field->setParentContext($this);
106
            return $field;
107
        }
108
109
        return parent::__call($method, $args);
110
    }
111
112
    /**
113
     * Calls an add field method on the FieldsBuilder
114
     * @param string $method [description]
115
     * @param array $args
116
     * @return FieldBuilder
117
     */
118
    private function callAddFieldMethod($method, $args)
119
    {
120
        return call_user_func_array([$this->fieldsBuilder, $method], $args);
121
    }
122
123
    /**
124
     * Gerenates the default button label.
125
     * @return string
126
     */
127
    private function getDefaultButtonLabel()
128
    {
129
        return 'Add '.rtrim($this->getLabel(), 's');
130
    }
131
}
132