Completed
Pull Request — master (#23)
by Steve
02:25
created

ChoiceFieldBuilder::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 3
1
<?php
2
3
namespace StoutLogic\AcfBuilder;
4
5
/**
6
 * Builds configurations for an ACF Field
7
 */
8
class ChoiceFieldBuilder extends FieldBuilder
9
{
10
    /**
11
     * @var array
12
     */
13
    private $choices = [];
14
15
    /**
16
     * @param string $name Field Name, conventionally 'snake_case'.
17
     * @param string $type Field Type.
18
     * @param array $config Additional Field Configuration.
19
     */
20
    public function __construct($name, $type, $config = [])
21
    {
22
        if (array_key_exists('choices', $config)) {
23
            $this->setChoices($config['choices']);
24
            unset($config['choices']);
25
        }
26
        parent::__construct($name, $type, $config);
27
    }
28
29
    /**
30
     * Add a choice with optional label. If label not supplied, choice value
31
     * will be used.
32
     * @param string $choice choice value
33
     * @param string $label  label that appears
34
     * @return $this
35
     */
36
    public function addChoice($choice, $label = null)
37
    {
38
        $label ?: $label = $choice;
39
        $this->choices[$choice] = $label;
40
        return $this;
41
    }
42
43
    /**
44
     * Add multiple choices. Also accepts multiple arguments, one for each choice.
45
     * @param array $choices Can be an array of key values ['choice' => 'label']
46
     * @return $this
47
     */
48
    public function addChoices($choices)
49
    {
50
        if (func_num_args() > 1) {
51
            $choices = func_get_args();
52
        }
53
54
        foreach ($choices as $choice) {
55
            $label = $choice;
56
            if (is_array($choice)) {
57
                $values = each($choice);
58
                $choice = $values['key'];
59
                $label = $values['value'];
60
            }
61
62
            $this->addChoice($choice, $label);
63
        }
64
65
        return $this;
66
    }
67
68
    /**
69
     * Discards existing choices and adds multiple choices.
70
     * Also accepts multiple arguments, one for each choice.
71
     * @param array $choices Can be an array of key values ['choice' => 'label']
72
     * @return $this
73
     */
74
    public function setChoices($choices)
75
    {
76
        if (func_num_args() > 1) {
77
            $choices = func_get_args();
78
        }
79
80
        $this->choices = [];
81
        return $this->addChoices($choices);
82
    }
83
84
    /**
85
     * @return array
86
     */
87
    private function getChoices()
88
    {
89
        return $this->choices;
90
    }
91
92
    /**
93
     * Build the field configuration array
94
     * @return array Field configuration array
95
     */
96
    public function build()
97
    {
98
        return array_merge([
99
            'choices' => $this->getChoices()
100
        ], parent::build());
101
    }
102
}
103