-
-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathMultiChoiceControl.php
More file actions
175 lines (143 loc) · 3.62 KB
/
MultiChoiceControl.php
File metadata and controls
175 lines (143 loc) · 3.62 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
164
165
166
167
168
169
170
171
172
173
174
175
<?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 multiple items selection.
*
* @property array $items
* @property-read array $selectedItems
*/
abstract class MultiChoiceControl 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 = array_keys(array_flip($this->getHttpData(Nette\Forms\Form::DATA_TEXT)));
if (is_array($this->disabled)) {
$this->value = array_diff($this->value, array_keys($this->disabled));
}
}
/**
* Sets selected items (by keys).
* @return static
* @internal
*/
public function setCurrentValue($values)
{
if (is_scalar($values) || $values === null) {
$values = (array) $values;
} elseif (!is_array($values)) {
throw new Nette\InvalidArgumentException(sprintf("Value must be array or null, %s given in field '%s'.", gettype($values), $this->name));
}
$flip = [];
foreach ($values as $value) {
if (!is_scalar($value) && !method_exists($value, '__toString')) {
throw new Nette\InvalidArgumentException(sprintf("Values must be scalar, %s given in field '%s'.", gettype($value), $this->name));
}
$flip[(string) $value] = true;
}
$values = array_keys($flip);
if ($this->checkAllowedValues && ($diff = array_diff($values, array_keys($this->items)))) {
$set = Nette\Utils\Strings::truncate(implode(', ', array_map(function ($s) { return var_export($s, true); }, array_keys($this->items))), 70, '...');
$vals = (count($diff) > 1 ? 's' : '') . " '" . implode("', '", $diff) . "'";
throw new Nette\InvalidArgumentException("Value$vals are out of allowed set [$set] in field '{$this->name}'.");
}
$this->value = $values;
return $this;
}
/**
* Returns selected keys.
*/
public function getValue(): array
{
return array_values(array_intersect($this->value, array_keys($this->items)));
}
/**
* Returns selected keys (not checked).
*/
public function getRawValue(): array
{
return $this->value;
}
/**
* Is any item selected?
*/
public function isFilled(): bool
{
return $this->getValue() !== [];
}
/**
* 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 values.
*/
public function getSelectedItems(): array
{
return array_intersect_key($this->items, array_flip($this->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);
$this->value = array_diff($this->value, $value);
return $this;
}
/**
* Returns HTML name of control.
*/
public function getHtmlName(): string
{
return parent::getHtmlName() . '[]';
}
/**
* @return static
*/
public function checkDefaultValue(bool $value = true)
{
$this->checkAllowedValues = $value;
return $this;
}
}