PluginProbe
Packeta / 2.0.9
Packeta v2.0.9
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / forms / src / Forms / Controls / ChoiceControl.php

ChoiceControl.php in Packeta 2.0.9, at deps/nette/forms/src/Forms/Controls/ChoiceControl.php

126 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This file is part of the Nette Framework (https://nette.org)
5 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6 */
7 declare (strict_types=1);
8 namespace Packetery\Nette\Forms\Controls;
9
10 use Packetery\Nette;
11 /**
12 * Choice control that allows single item selection.
13 *
14 * @property array $items
15 * @property-read mixed $selectedItem
16 */
17 abstract class ChoiceControl extends BaseControl
18 {
19 /** @var bool */
20 private $checkDefaultValue = \true;
21 /** @var array */
22 private $items = [];
23 public function __construct($label = null, array $items = null)
24 {
25 parent::__construct($label);
26 if ($items !== null) {
27 $this->setItems($items);
28 }
29 }
30 public function loadHttpData() : void
31 {
32 $this->value = $this->getHttpData(\Packetery\Nette\Forms\Form::DATA_TEXT);
33 if ($this->value !== null) {
34 $this->value = \is_array($this->disabled) && isset($this->disabled[$this->value]) ? null : \key([$this->value => null]);
35 }
36 }
37 /**
38 * Sets selected item (by key).
39 * @param string|int|null $value
40 * @return static
41 * @internal
42 */
43 public function setValue($value)
44 {
45 if ($this->checkDefaultValue && $value !== null && !\array_key_exists((string) $value, $this->items)) {
46 $set = \Packetery\Nette\Utils\Strings::truncate(\implode(', ', \array_map(function ($s) {
47 return \var_export($s, \true);
48 }, \array_keys($this->items))), 70, '...');
49 throw new \Packetery\Nette\InvalidArgumentException("Value '{$value}' is out of allowed set [{$set}] in field '{$this->name}'.");
50 }
51 $this->value = $value === null ? null : \key([(string) $value => null]);
52 return $this;
53 }
54 /**
55 * Returns selected key.
56 * @return string|int|null
57 */
58 public function getValue()
59 {
60 return \array_key_exists($this->value, $this->items) ? $this->value : null;
61 }
62 /**
63 * Returns selected key (not checked).
64 * @return string|int
65 */
66 public function getRawValue()
67 {
68 return $this->value;
69 }
70 /**
71 * Is any item selected?
72 */
73 public function isFilled() : bool
74 {
75 return $this->getValue() !== null;
76 }
77 /**
78 * Sets items from which to choose.
79 * @return static
80 */
81 public function setItems(array $items, bool $useKeys = \true)
82 {
83 $this->items = $useKeys ? $items : \array_combine($items, $items);
84 return $this;
85 }
86 /**
87 * Returns items from which to choose.
88 */
89 public function getItems() : array
90 {
91 return $this->items;
92 }
93 /**
94 * Returns selected value.
95 * @return mixed
96 */
97 public function getSelectedItem()
98 {
99 $value = $this->getValue();
100 return $value === null ? null : $this->items[$value];
101 }
102 /**
103 * Disables or enables control or items.
104 * @param bool|array $value
105 * @return static
106 */
107 public function setDisabled($value = \true)
108 {
109 if (!\is_array($value)) {
110 return parent::setDisabled($value);
111 }
112 parent::setDisabled(\false);
113 $this->disabled = \array_fill_keys($value, \true);
114 if (isset($this->disabled[$this->value])) {
115 $this->value = null;
116 }
117 return $this;
118 }
119 /** @return static */
120 public function checkDefaultValue(bool $value = \true)
121 {
122 $this->checkDefaultValue = $value;
123 return $this;
124 }
125 }
126