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