|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace StoutLogic\AcfBuilder; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Group field |
|
7
|
|
|
* Can add multiple fields as subfields to the group. |
|
8
|
|
|
*/ |
|
9
|
|
|
class GroupBuilder extends FieldBuilder |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Used to contain and add fields |
|
13
|
|
|
* @var FieldsBuilder |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $fieldsBuilder; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param string $name Field name |
|
19
|
|
|
* @param string $type Field name |
|
20
|
|
|
* @param array $config Field configuration |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct($name, $type = 'group', $config = []) |
|
23
|
|
|
{ |
|
24
|
|
|
parent::__construct($name, $type, $config); |
|
25
|
|
|
$this->fieldsBuilder = new FieldsBuilder($name); |
|
26
|
|
|
$this->fieldsBuilder->setParentContext($this); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Add multiple fields either via an array or from another builder |
|
31
|
|
|
* @param array|FieldsBuilder $fields |
|
32
|
|
|
* @return $this |
|
33
|
|
|
*/ |
|
34
|
|
|
public function addFields($fields) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->fieldsBuilder->addFields($fields); |
|
37
|
|
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Return a group field configuration array |
|
42
|
|
|
* @return array |
|
43
|
|
|
*/ |
|
44
|
|
|
public function build() |
|
45
|
|
|
{ |
|
46
|
|
|
$config = parent::build(); |
|
47
|
|
|
$fields = $this->fieldsBuilder->build(); |
|
48
|
|
|
$config['sub_fields'] = $fields['fields']; |
|
49
|
|
|
return $config; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Returns call chain to parentContext |
|
54
|
|
|
* @return FieldBuilder |
|
55
|
|
|
*/ |
|
56
|
|
|
public function endGroup() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->getParentContext(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns call chain to parentContext |
|
63
|
|
|
* @return FieldBuilder |
|
64
|
|
|
*/ |
|
65
|
|
|
public function end() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->endGroup(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Intercept missing methods, pass any methods that begin with add to the |
|
72
|
|
|
* internal fieldsBuilder |
|
73
|
|
|
* @param string $method |
|
74
|
|
|
* @param array $args |
|
75
|
|
|
* @return mixed |
|
76
|
|
|
*/ |
|
77
|
|
|
public function __call($method, $args) |
|
78
|
|
|
{ |
|
79
|
|
|
if (preg_match('/^add.+/', $method) && method_exists($this->fieldsBuilder, $method)) { |
|
80
|
|
|
$field = $this->callAddFieldMethod($method, $args); |
|
81
|
|
|
$field->setParentContext($this); |
|
82
|
|
|
return $field; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return parent::__call($method, $args); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Calls an add field method on the FieldsBuilder |
|
90
|
|
|
* @param string $method [description] |
|
91
|
|
|
* @param array $args |
|
92
|
|
|
* @return FieldBuilder |
|
93
|
|
|
*/ |
|
94
|
|
|
private function callAddFieldMethod($method, $args) |
|
95
|
|
|
{ |
|
96
|
|
|
return call_user_func_array([$this->fieldsBuilder, $method], $args); |
|
97
|
|
|
} |
|
98
|
|
|
} |