Completed
Push — dev ( a486ee...7113e4 )
by James Ekow Abaka
03:55
created

SelectionList::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
/**
3
 * Selection Lists for forms
4
 * 
5
 * Ntentan Framework
6
 * Copyright (c) 2008-2012 James Ekow Abaka Ainooson
7
 * 
8
 * Permission is hereby granted, free of charge, to any person obtaining
9
 * a copy of this software and associated documentation files (the
10
 * "Software"), to deal in the Software without restriction, including
11
 * without limitation the rights to use, copy, modify, merge, publish,
12
 * distribute, sublicense, and/or sell copies of the Software, and to
13
 * permit persons to whom the Software is furnished to do so, subject to
14
 * the following conditions:
15
 * 
16
 * The above copyright notice and this permission notice shall be
17
 * included in all copies or substantial portions of the Software.
18
 * 
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
26
 * 
27
 * @author James Ainooson <[email protected]>
28
 * @copyright Copyright 2010 James Ekow Abaka Ainooson
29
 * @license MIT
30
 */
31
32
33
namespace ntentan\honam\engines\php\helpers\form;
34
35
use ntentan\honam\engines\php\Variable;
36
37
/**
38
 * A selection list class for the forms helper. This class renders an HTML
39
 * select form object with its associated options.
40
 */
41
class SelectionList extends Field
42
{
43
    /**
44
     * An array of options to display with this selection list
45
     * @var array
46
     */
47
    protected $options = array();
48
49
    /**
50
     * When set true, this selection list would allow multiple selections
51
     * @var boolean
52
     */
53
    protected $multiple;
54
    
55
    protected $default;
56
57
    /**
58
     * Constructs a new selection list. This constructor could be invoked through
59
     * the form helper's $this->form->get_* method as $this->form->get_selection_list().
60
     *
61
     * @param string $label The label for the selection list
62
     * @param string $name The name of the selection list
63
     * @param string $description A brief description for the selection list
64
     */
65 1
    public function __construct($label="", $name="", $description="")
66
    {
67 1
        Field::__construct($name);
68 1
        Element::__construct($label, $description);
69 1
    }
70
71
    /**
72
     * Sets whether multiple selections are allowed. This method automatically
73
     * appends the array symbol '[]' to the name of the selection list object.
74
     * @param boolean $multiple
75
     * @return SelectionList
76
     */
77 1
    public function setMultiple($multiple)
78
    {
79 1
        $this->name.="[]";
80 1
        $this->multiple = $multiple;
81 1
        return $this;
82
    }
83
84
    /**
85
     * Add an option to the selection list.
86
     * @param string $label
87
     * @param string $value
88
     * @return SelectionList
89
     */
90 1
    public function addOption($label="", $value="")
91
    {
92 1
        if($value==="") $value=$label;
93 1
        $this->options[$value] = $label;
94 1
        return $this;
95
    }
96
97
    /**
98
     * An alias for SelectionList::addOption
99
     * @param string $label
100
     * @param string $value
101
     * @return SelectionList
102
     */
103 1
    public function option($label='', $value='')
104
    {
105 1
        $this->addOption($label, $value);
106 1
        return $this;
107
    }
108
    
109 1
    public function initial($default)
110
    {
111 1
        $this->default = $default;
112 1
        return $this;
113
    }
114
115 1
    public function render()
116
    {
117 1
        $keys = array_keys($this->options);
118 1
        array_unshift($keys, '');
119 1
        array_unshift($this->options, $this->default);
120 1
        $this->options = array_combine($keys, $this->options);
121
        
122 1
        $this->setAttribute('name', $this->name);
123
        
124 1
        if($this->multiple)
125
        {
126 1
            $this->setAttribute('multiple', 'multiple');
127
        }
128 1
        $this->setAttribute('class', "select {$this->getCSSClasses()}");
129
        
130 1
        return $this->templateRenderer->render('select_element.tpl.php', ['element' => $this]);
131
    }
132
133
    /**
134
     * Set the options using a key value pair datastructure represented in the form of
135
     * a structured array.
136
     *
137
     * @param array|Variable $options An array of options
138
     * 
139
     * @return SelectionList
140
     */
141 1
    public function setOptions($options = [])
142
    {
143 1
        if(is_a($options, Variable::class))
144
        {
145
            $options = $options->unescape();
146
        }
147 1
        $this->options += $options;
148 1
        return $this;
149
    }
150
151
    /**
152
     * Return the array of options
153
     * @return array
154
     */
155 1
    public function getOptions()
156
    {
157 1
        return $this->options;
158
    }
159
}
160