Passed
Push — main ( 4f550a...48e0a0 )
by James Ekow Abaka
11:16
created

Select::initial()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace ntentan\honam\engines\php\helpers\form;
3
4
use ntentan\honam\engines\php\Variable;
5
6
/**
7
 * A selection list class for the forms helper. This class renders an HTML
8
 * select form object with its associated options.
9
 */
10
class Select extends Field
11
{
12
    /**
13
     * An array of options to display with this selection list
14
     * @var array
15
     */
16
    protected $options = array();
17
18
    /**
19
     * When set true, this selection list would allow multiple selections
20
     * @var boolean
21
     */
22
    protected $multiple;
23
    
24
    protected $default;
25
26
    /**
27
     * Constructs a new selection list. This constructor could be invoked through
28
     * the form helper's $this->form->get_* method as $this->form->get_selection_list().
29
     *
30
     * @param string $label The label for the selection list
31
     * @param string $name The name of the selection list
32
     * @param string $description A brief description for the selection list
33
     */
34
    public function __construct($label="", $name="", $options=[])
35
    {
36
        Field::__construct($name);
37
        Element::__construct($label);
38
        $this->setOptions($options);
39
    }
40
41
    /**
42
     * Sets whether multiple selections are allowed. This method automatically
43
     * appends the array symbol '[]' to the name of the selection list object.
44
     * @param boolean $multiple
45
     * @return SelectionList
0 ignored issues
show
Bug introduced by
The type ntentan\honam\engines\ph...pers\form\SelectionList was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
46
     */
47
    public function setMultiple($multiple)
48
    {
49
        $this->name.="[]";
50
        $this->multiple = $multiple;
51
        return $this;
52
    }
53
54
    /**
55
     * Add an option to the selection list.
56
     * @param string $label
57
     * @param string $value
58
     * @return SelectionList
59
     */
60
    public function addOption($label="", $value="")
61
    {
62
        if($value==="") $value=$label;
63
        $this->options[$value] = $label;
64
        return $this;
65
    }
66
67
    /**
68
     * An alias for SelectionList::addOption
69
     * @param string $label
70
     * @param string $value
71
     * @return SelectionList
72
     */
73
    public function option($label='', $value='')
74
    {
75
        $this->addOption($label, $value);
76
        return $this;
77
    }
78
    
79
    public function initial($default)
80
    {
81
        $this->default = $default;
82
        return $this;
83
    }
84
85
    public function render()
86
    {
87
        $keys = array_keys($this->options);
88
        array_unshift($keys, '');
89
        array_unshift($this->options, $this->default);
90
        $this->options = array_combine($keys, $this->options) ?? [];
91
        
92
        $this->setAttribute('name', $this->name);
93
        
94
        if($this->multiple)
95
        {
96
            $this->setAttribute('multiple', 'multiple');
97
        }
98
        
99
        return $this->templateRenderer->render('select_element.tpl.php', ['element' => $this]);
100
    }
101
102
    /**
103
     * Set the options using a key value pair datastructure represented in the form of
104
     * a structured array.
105
     *
106
     * @param array|Variable $options An array of options
107
     * 
108
     * @return SelectionList
109
     */
110
    public function setOptions($options = [])
111
    {
112
        if(is_a($options, Variable::class))
113
        {
114
            $options = $options->unescape();
115
        }
116
        $this->options += $options;
117
        return $this;
118
    }
119
120
    /**
121
     * Return the array of options
122
     * @return array
123
     */
124
    public function getOptions()
125
    {
126
        return $this->options;
127
    }
128
}
129