Completed
Push — master ( 13bfeb...08a093 )
by Steve
02:13
created

RepeaterBuilder::__call()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 6
nc 2
nop 2
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
 */
11
class RepeaterBuilder extends GroupBuilder
12
{
13
    /**
14
     * Used to contain and add fields
15
     * @var FieldsBuilder
16
     */
17
    protected $fieldsBuilder;
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 = 'repeater', $config = [])
25
    {
26
        parent::__construct($name, $type, $config);
27
28
        if (!array_key_exists('button_label', $config)) {
29
            $this->setConfig('button_label', $this->getDefaultButtonLabel());
30
        }
31
    }
32
33
    /**
34
     * Return a repeater field configuration array
35
     * @return array
36
     */
37
    public function build()
38
    {
39
        $config = parent::build();
40
        if (array_key_exists('collapsed', $config)) {
41
            $fieldKey = $this->fieldsBuilder->getField($config['collapsed'])->getKey();
42
            $fieldKey = preg_replace('/^field_/', '', $fieldKey);
43
            $config['collapsed'] = $this->getName() . '_' . $fieldKey;
44
        }
45
        return $config;
46
    }
47
48
    /**
49
     * Returns call chain to parentContext
50
     * @return Builder
51
     */
52
    public function endRepeater()
53
    {
54
        return $this->getParentContext();
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function end()
61
    {
62
        return $this->endRepeater();
63
    }
64
65
    /**
66
     * Gerenates the default button label.
67
     * @return string
68
     */
69
    private function getDefaultButtonLabel()
70
    {
71
        return 'Add ' . Inflector::singularize($this->getLabel());
72
    }
73
}
74