-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
ChoiceControl.php
153 lines (126 loc) 路 2.87 KB
/
ChoiceControl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* This file is part of the Nette Framework (http://nette.org)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*/
namespace Nette\Forms\Controls;
use Nette;
/**
* Choice control that allows single item selection.
*
* @author David Grudl
*
* @property array $items
* @property-read mixed $selectedItem
* @property-read mixed $rawValue
*/
abstract class ChoiceControl extends BaseControl
{
/** @var array */
private $items = array();
public function __construct($label = NULL, array $items = NULL)
{
parent::__construct($label);
if ($items !== NULL) {
$this->setItems($items);
}
}
/**
* Loads HTTP data.
* @return void
*/
public function loadHttpData()
{
$this->value = $this->getHttpData(Nette\Forms\Form::DATA_TEXT);
if ($this->value !== NULL) {
if (is_array($this->disabled) && isset($this->disabled[$this->value])) {
$this->value = NULL;
} else {
$this->value = key(array($this->value => NULL));
}
}
}
/**
* Sets selected item (by key).
* @param scalar
* @return self
*/
public function setValue($value)
{
if ($value !== NULL && !array_key_exists((string) $value, $this->items)) {
$range = Nette\Utils\Strings::truncate(implode(', ', array_map(function($s) { return var_export($s, TRUE); }, array_keys($this->items))), 70, '...');
throw new Nette\InvalidArgumentException("Value '$value' is out of allowed range [$range] in field '{$this->name}'.");
}
$this->value = $value === NULL ? NULL : key(array((string) $value => NULL));
return $this;
}
/**
* Returns selected key.
* @return scalar
*/
public function getValue()
{
return array_key_exists($this->value, $this->items) ? $this->value : NULL;
}
/**
* Returns selected key (not checked).
* @return scalar
*/
public function getRawValue()
{
return $this->value;
}
/**
* Is any item selected?
* @return bool
*/
public function isFilled()
{
return $this->getValue() !== NULL;
}
/**
* Sets items from which to choose.
* @param array
* @param bool
* @return self
*/
public function setItems(array $items, $useKeys = TRUE)
{
$this->items = $useKeys ? $items : array_combine($items, $items);
return $this;
}
/**
* Returns items from which to choose.
* @return array
*/
public function getItems()
{
return $this->items;
}
/**
* Returns selected value.
* @return mixed
*/
public function getSelectedItem()
{
$value = $this->getValue();
return $value === NULL ? NULL : $this->items[$value];
}
/**
* Disables or enables control or items.
* @param bool|array
* @return self
*/
public function setDisabled($value = TRUE)
{
if (!is_array($value)) {
return parent::setDisabled($value);
}
parent::setDisabled(FALSE);
$this->disabled = array_fill_keys($value, TRUE);
if (isset($this->disabled[$this->value])) {
$this->value = NULL;
}
return $this;
}
}