Completed
Pull Request — master (#4)
by James Ekow Abaka
03:06 queued 36s
created

SelectionList::option()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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) ?? [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array_combine($keys, $this->options) ?? array() can also be of type false. However, the property $options is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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))
0 ignored issues
show
Bug introduced by
It seems like $options can also be of type array; however, parameter $object of is_a() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

143
        if(is_a(/** @scrutinizer ignore-type */ $options, Variable::class))
Loading history...
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