Skip to content

Commit 7a8d426

Browse files
committed
ChoiceControl, MultiChoiceControl: extracted getItem() method
1 parent 48df021 commit 7a8d426

4 files changed

Lines changed: 30 additions & 20 deletions

File tree

src/Forms/Controls/CheckboxList.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ public function getLabel($caption = null): Html
8585
public function getControlPart($key = null): Html
8686
{
8787
$key = key([(string) $key => null]);
88-
if (!array_key_exists($key, $this->getItems())) {
89-
throw new Nette\InvalidArgumentException("Item '$key' does not exist in field '{$this->getName()}'.");
90-
}
91-
88+
$this->getItem($key);
9289
return parent::getControl()->addAttributes([
9390
'id' => $this->getHtmlId() . '-' . $key,
9491
'checked' => in_array($key, (array) $this->value, strict: true),
@@ -109,12 +106,8 @@ public function getLabelPart($key = null): Html
109106
}
110107

111108
$key = key([(string) $key => null]);
112-
if (!array_key_exists($key, $this->getItems())) {
113-
throw new Nette\InvalidArgumentException("Item '$key' does not exist in field '{$this->getName()}'.");
114-
}
115-
116109
$itemLabel = clone $this->itemLabel;
117-
return $itemLabel->setText($this->translate($this->getItems()[$key]))->for($this->getHtmlId() . '-' . $key);
110+
return $itemLabel->setText($this->translate($this->getItem($key)))->for($this->getHtmlId() . '-' . $key);
118111
}
119112

120113

src/Forms/Controls/ChoiceControl.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,16 @@ public function checkDefaultValue(bool $value = true): static
153153
$this->checkDefaultValue = $value;
154154
return $this;
155155
}
156+
157+
158+
/**
159+
* Returns the item label for the given key, or throws an exception if the key does not exist.
160+
*/
161+
protected function getItem(string|int $key): mixed
162+
{
163+
$key = key([(string) $key => null]);
164+
return array_key_exists($key, $this->items)
165+
? $this->items[$key]
166+
: throw new Nette\InvalidArgumentException("Item '$key' does not exist in field '{$this->getName()}'.");
167+
}
156168
}

src/Forms/Controls/MultiChoiceControl.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Nette\Forms\Controls;
99

1010
use Nette;
11-
use function array_combine, array_diff, array_fill_keys, array_flip, array_keys, array_map, count, get_debug_type, implode, is_array, is_scalar, sprintf, var_export;
11+
use function array_combine, array_diff, array_fill_keys, array_flip, array_key_exists, array_keys, array_map, count, get_debug_type, implode, is_array, is_scalar, key, sprintf, var_export;
1212

1313

1414
/**
@@ -168,4 +168,16 @@ public function checkDefaultValue(bool $value = true): static
168168
$this->checkDefaultValue = $value;
169169
return $this;
170170
}
171+
172+
173+
/**
174+
* Returns the item label for the given key, or throws an exception if the key does not exist.
175+
*/
176+
protected function getItem(string|int $key): mixed
177+
{
178+
$key = key([(string) $key => null]);
179+
return array_key_exists($key, $this->items)
180+
? $this->items[$key]
181+
: throw new Nette\InvalidArgumentException("Item '$key' does not exist in field '{$this->getName()}'.");
182+
}
171183
}

src/Forms/Controls/RadioList.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Nette;
1111
use Nette\Utils\Html;
1212
use Stringable;
13-
use function array_key_exists, array_key_first, array_merge, func_num_args, in_array, is_array, key;
13+
use function array_key_first, array_merge, func_num_args, in_array, is_array, key;
1414

1515

1616
/**
@@ -82,10 +82,7 @@ public function getLabel($caption = null): Html
8282
public function getControlPart($key = null): Html
8383
{
8484
$key = key([(string) $key => null]);
85-
if (!array_key_exists($key, $this->getItems())) {
86-
throw new Nette\InvalidArgumentException("Item '$key' does not exist in field '{$this->getName()}'.");
87-
}
88-
85+
$this->getItem($key);
8986
return parent::getControl()->addAttributes([
9087
'id' => $this->getHtmlId() . '-' . $key,
9188
'checked' => in_array($key, (array) $this->value, strict: true),
@@ -105,12 +102,8 @@ public function getLabelPart($key = null): Html
105102
}
106103

107104
$key = key([(string) $key => null]);
108-
if (!array_key_exists($key, $this->getItems())) {
109-
throw new Nette\InvalidArgumentException("Item '$key' does not exist in field '{$this->getName()}'.");
110-
}
111-
112105
$itemLabel = clone $this->itemLabel;
113-
return $itemLabel->setText($this->translate($this->getItems()[$key]))->for($this->getHtmlId() . '-' . $key);
106+
return $itemLabel->setText($this->translate($this->getItem($key)))->for($this->getHtmlId() . '-' . $key);
114107
}
115108

116109

0 commit comments

Comments
 (0)