Test Failed
Push — master ( c207a3...da2a21 )
by Steve
09:23 queued 12s
created

FlexibleContentBuilder::addLayouts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace StoutLogic\AcfBuilder;
4
5
/**
6
 * Create a configuration array for an ACF Flexible Content field.
7
 * A flexible content field can have many different `layouts` which are
8
 * groups of fields.
9
 */
10
class FlexibleContentBuilder extends FieldBuilder
11
{
12
    use Traits\CanSingularize;
13
14
    /**
15
     * @var array
16
     */
17
    private $layouts = [];
18
19
    /**
20
     * @param string $name Field name
21
     * @param string $type Field name
22
     * @param array $config Field configuration
23
     */
24
    public function __construct($name, $type = 'flexible_content', $config = [])
25
    {
26
        parent::__construct($name, $type, $config);
27
28
        if (!isset($config['button_label'])) {
29
            $this->setConfig('button_label', $this->getDefaultButtonLabel());
30
        }
31
    }
32
33
    /**
34
     * Return a configuration array
35
     * @return array
36
     */
37
    public function build()
38
    {
39
        return array_merge(parent::build(), [
40
            'layouts' => $this->buildLayouts(),
41
        ]);
42
    }
43
44
    /**
45
     * Return a configuration array for each layout
46
     * @return array
47
     */
48
    private function buildLayouts()
49
    {
50
        return array_map(function ($layout) {
51
            $layout = ($layout instanceof Builder) ? $layout->build() : $layout;
52
            return $this->transformLayout($layout);
53
        }, $this->getLayouts());
54
    }
55
56
    /**
57
     * Apply transformations to a layout
58
     * @param  array $layout Layout configuration array
59
     * @return array Transformed layout configuration array
60
     */
61
    private function transformLayout($layout)
62
    {
63
        $layoutTransform = new Transform\FlexibleContentLayout($this);
64
        $namespaceTransform = new Transform\NamespaceFieldKey($this);
65
66
        return
67
            $namespaceTransform->transform(
68
                $layoutTransform->transform($layout)
69
            );
70
    }
71
72
    /**
73
     * Add a layout, which is a FieldsBuilder. `addLayout` can be chained to add
74
     * multiple layouts to the Flexible Content field.
75
     * @param string|FieldsBuilder $layout layout name.
76
     * Alternatively supply a FieldsBuilder to reuse existing fields. The name
77
     * will be inferred from the FieldsBuilder's name.
78
     * @param array $args filed configuration
79
     * @return FieldsBuilder
80
     */
81
    public function addLayout($layout, $args = [])
82
    {
83
        if ($layout instanceof FieldsBuilder) {
84
            $layout = clone $layout;
85
        } else {
86
            $layout = new FieldsBuilder($layout, $args);
87
        }
88
89
        $layout = $this->initializeLayout($layout, $args);
90
        $this->pushLayout($layout);
91
92
        return $layout;
93
    }
94
95
    /**
96
     * Add multiple layouts either via an array or from another builder
97
     * @param FieldsBuilder|array $layouts
98
     * @return $this
99
     */
100
    public function addLayouts($layouts)
101
    {
102
        foreach ($layouts as $layout) {
0 ignored issues
show
Bug introduced by
The expression $layouts of type object<StoutLogic\AcfBuilder\FieldsBuilder>|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
103
            $this->addLayout($layout);
104
        }
105
        return $this;
106
    }
107
108
    /**
109
     * Configures the layout FieldsBuilder
110
     * @param  FieldsBuilder $layout
111
     * @param  array         $args FieldGroup Configuration
112
     * @return FieldsBuilder Configured Layout
113
     */
114
    protected function initializeLayout(FieldsBuilder $layout, $args = [])
115
    {
116
        $layout->setGroupConfig('name', $layout->getName());
117
        $layout->setGroupConfig('display', 'block');
118
119
        foreach ($args as $key => $value) {
120
            $layout->setGroupConfig($key, $value);
121
        }
122
123
        $layout->setParentContext($this);
124
125
        return $layout;
126
    }
127
128
    /**
129
     * End the current Flexible Content field, return to parent context
130
     * @return Builder
131
     */
132
    public function endFlexibleContent()
133
    {
134
        return $this->getParentContext();
135
    }
136
137
    /**
138
     * Add layout to internal array
139
     * @param  FieldsBuilder $layout
140
     * @return void
141
     */
142
    protected function pushLayout($layout)
143
    {
144
        $this->layouts[] = $layout;
145
    }
146
147
    /**
148
     * @return FieldsBuilder[]
149
     */
150
    public function getLayouts()
151
    {
152
        return $this->layouts;
153
    }
154
155
    /**
156
     * Gerenates the default button label.
157
     * @return string
158
     */
159
    private function getDefaultButtonLabel()
160
    {
161
        return 'Add ' . $this->singularize($this->getLabel());
162
    }
163
}
164