|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace StoutLogic\AcfBuilder; |
|
4
|
|
|
|
|
5
|
|
|
class FlexibleContentBuilder extends Builder |
|
6
|
|
|
{ |
|
7
|
|
|
private $config = []; |
|
8
|
|
|
private $layouts = []; |
|
9
|
|
|
|
|
10
|
|
|
private $name; |
|
11
|
|
|
|
|
12
|
|
|
public function __construct($name, $args = []) |
|
13
|
|
|
{ |
|
14
|
|
|
$this->name = $name; |
|
15
|
|
|
$this->config = array_merge( |
|
16
|
|
|
[ |
|
17
|
|
|
'key' => $name, |
|
18
|
|
|
'name' => $name, |
|
19
|
|
|
'label' => $this->generateLabel($name), |
|
20
|
|
|
'type' => 'flexible_content', |
|
21
|
|
|
], |
|
22
|
|
|
$args |
|
23
|
|
|
); |
|
24
|
|
|
|
|
25
|
|
|
if (!isset($this->config['button'])) { |
|
26
|
|
|
$this->config['button'] = 'Add '.rtrim($this->config['label'], 's'); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function build() |
|
31
|
|
|
{ |
|
32
|
|
|
return array_merge($this->config, [ |
|
33
|
|
|
'layouts' => $this->buildLayouts(), |
|
34
|
|
|
]); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
private function buildLayouts() |
|
38
|
|
|
{ |
|
39
|
|
|
return array_map(function($layout) { |
|
40
|
|
|
$layout = ($layout instanceof Builder) ? $layout->build() : $layout; |
|
41
|
|
|
return $this->transformLayout($layout); |
|
42
|
|
|
}, $this->getLayouts()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private function transformLayout($layout) |
|
46
|
|
|
{ |
|
47
|
|
|
$layoutTransform = new Transform\FlexibleContentLayout($this); |
|
48
|
|
|
$namespaceTransform = new Transform\NamespaceFieldKey($this); |
|
49
|
|
|
|
|
50
|
|
|
return |
|
51
|
|
|
$namespaceTransform->transform( |
|
52
|
|
|
$layoutTransform->transform($layout) |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getName() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->name; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function addLayout($layout, $args = []) |
|
62
|
|
|
{ |
|
63
|
|
|
if ($layout instanceof Builder) { |
|
64
|
|
|
$layout = clone $layout; |
|
65
|
|
|
} else { |
|
66
|
|
|
$layout = new FieldsBuilder($layout, $args); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$layout->setGroupConfig('name', $layout->getName()); |
|
|
|
|
|
|
70
|
|
|
$layout->setGroupConfig('display', 'block'); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
foreach ($args as $key => $value) { |
|
73
|
|
|
$layout->setGroupConfig($key, $value); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$layout->setParentContext($this); |
|
77
|
|
|
$this->pushLayout($layout); |
|
78
|
|
|
|
|
79
|
|
|
return $layout; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function endFlexibleContent() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->getParentContext(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function pushLayout($layout) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->layouts[] = $layout; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getLayouts() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->layouts; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function generateLabel($name) |
|
98
|
|
|
{ |
|
99
|
|
|
return ucwords(str_replace("_", " ", $name)); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: