-
-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathChoiceControl.php
More file actions
163 lines (133 loc) · 3.06 KB
/
ChoiceControl.php
File metadata and controls
163 lines (133 loc) · 3.06 KB
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
154
155
156
157
158
159
160
161
162
163
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Forms\Controls;
use Nette;
/**
* Choice control that allows single item selection.
*
* @property array $items
* @property-read mixed $selectedItem
*/
abstract class ChoiceControl extends BaseControl
{
/**
* @var bool
* @deprecated use checkDefaultValue()
*/
public $checkAllowedValues = true;
/** @var array */
private $items = [];
public function __construct($label = null, array $items = null)
{
parent::__construct($label);
if ($items !== null) {
$this->setItems($items);
}
}
/**
* Loads HTTP data.
*/
public function loadHttpData(): void
{
$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([$this->value => null]);
}
}
}
/**
* Sets selected item (by key).
* @param string|int
* @return static
* @internal
*/
public function setCurrentValue($value)
{
if ($this->checkAllowedValues && $value !== null && !array_key_exists((string) $value, $this->items)) {
$set = 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 set [$set] in field '{$this->name}'.");
}
$this->value = $value === null ? null : key([(string) $value => null]);
return $this;
}
/**
* Returns selected key.
* @return string|int
*/
public function getValue()
{
return array_key_exists($this->value, $this->items) ? $this->value : null;
}
/**
* Returns selected key (not checked).
* @return string|int
*/
public function getRawValue()
{
return $this->value;
}
/**
* Is any item selected?
*/
public function isFilled(): bool
{
return $this->getValue() !== null;
}
/**
* Sets items from which to choose.
* @return static
*/
public function setItems(array $items, bool $useKeys = true)
{
$this->items = $useKeys ? $items : array_combine($items, $items);
return $this;
}
/**
* Returns items from which to choose.
*/
public function getItems(): array
{
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 static
*/
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;
}
/**
* @return static
*/
public function checkDefaultValue(bool $value = true)
{
$this->checkAllowedValues = $value;
return $this;
}
}