| 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 multiple items selection. |
| 13 |
* |
| 14 |
* @property array $items |
| 15 |
* @property-read array $selectedItems |
| 16 |
*/ |
| 17 |
abstract class MultiChoiceControl 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 = \array_keys(\array_flip($this->getHttpData(\Packetery\Nette\Forms\Form::DATA_TEXT))); |
| 33 |
if (\is_array($this->disabled)) { |
| 34 |
$this->value = \array_diff($this->value, \array_keys($this->disabled)); |
| 35 |
} |
| 36 |
} |
| 37 |
/** |
| 38 |
* Sets selected items (by keys). |
| 39 |
* @return static |
| 40 |
* @internal |
| 41 |
*/ |
| 42 |
public function setValue($values) |
| 43 |
{ |
| 44 |
if (\is_scalar($values) || $values === null) { |
| 45 |
$values = (array) $values; |
| 46 |
} elseif (!\is_array($values)) { |
| 47 |
throw new \Packetery\Nette\InvalidArgumentException(\sprintf("Value must be array or null, %s given in field '%s'.", \gettype($values), $this->name)); |
| 48 |
} |
| 49 |
$flip = []; |
| 50 |
foreach ($values as $value) { |
| 51 |
if (!\is_scalar($value) && !(\is_object($value) && \method_exists($value, '__toString'))) { |
| 52 |
throw new \Packetery\Nette\InvalidArgumentException(\sprintf("Values must be scalar, %s given in field '%s'.", \gettype($value), $this->name)); |
| 53 |
} |
| 54 |
$flip[(string) $value] = \true; |
| 55 |
} |
| 56 |
$values = \array_keys($flip); |
| 57 |
if ($this->checkDefaultValue && ($diff = \array_diff($values, \array_keys($this->items)))) { |
| 58 |
$set = \Packetery\Nette\Utils\Strings::truncate(\implode(', ', \array_map(function ($s) { |
| 59 |
return \var_export($s, \true); |
| 60 |
}, \array_keys($this->items))), 70, '...'); |
| 61 |
$vals = (\count($diff) > 1 ? 's' : '') . " '" . \implode("', '", $diff) . "'"; |
| 62 |
throw new \Packetery\Nette\InvalidArgumentException("Value{$vals} are out of allowed set [{$set}] in field '{$this->name}'."); |
| 63 |
} |
| 64 |
$this->value = $values; |
| 65 |
return $this; |
| 66 |
} |
| 67 |
/** |
| 68 |
* Returns selected keys. |
| 69 |
*/ |
| 70 |
public function getValue() : array |
| 71 |
{ |
| 72 |
return \array_values(\array_intersect($this->value, \array_keys($this->items))); |
| 73 |
} |
| 74 |
/** |
| 75 |
* Returns selected keys (not checked). |
| 76 |
*/ |
| 77 |
public function getRawValue() : array |
| 78 |
{ |
| 79 |
return $this->value; |
| 80 |
} |
| 81 |
/** |
| 82 |
* Is any item selected? |
| 83 |
*/ |
| 84 |
public function isFilled() : bool |
| 85 |
{ |
| 86 |
return $this->getValue() !== []; |
| 87 |
} |
| 88 |
/** |
| 89 |
* Sets items from which to choose. |
| 90 |
* @return static |
| 91 |
*/ |
| 92 |
public function setItems(array $items, bool $useKeys = \true) |
| 93 |
{ |
| 94 |
$this->items = $useKeys ? $items : \array_combine($items, $items); |
| 95 |
return $this; |
| 96 |
} |
| 97 |
/** |
| 98 |
* Returns items from which to choose. |
| 99 |
*/ |
| 100 |
public function getItems() : array |
| 101 |
{ |
| 102 |
return $this->items; |
| 103 |
} |
| 104 |
/** |
| 105 |
* Returns selected values. |
| 106 |
*/ |
| 107 |
public function getSelectedItems() : array |
| 108 |
{ |
| 109 |
return \array_intersect_key($this->items, \array_flip($this->value)); |
| 110 |
} |
| 111 |
/** |
| 112 |
* Disables or enables control or items. |
| 113 |
* @param bool|array $value |
| 114 |
* @return static |
| 115 |
*/ |
| 116 |
public function setDisabled($value = \true) |
| 117 |
{ |
| 118 |
if (!\is_array($value)) { |
| 119 |
return parent::setDisabled($value); |
| 120 |
} |
| 121 |
parent::setDisabled(\false); |
| 122 |
$this->disabled = \array_fill_keys($value, \true); |
| 123 |
$this->value = \array_diff($this->value, $value); |
| 124 |
return $this; |
| 125 |
} |
| 126 |
/** |
| 127 |
* Returns HTML name of control. |
| 128 |
*/ |
| 129 |
public function getHtmlName() : string |
| 130 |
{ |
| 131 |
return parent::getHtmlName() . '[]'; |
| 132 |
} |
| 133 |
/** @return static */ |
| 134 |
public function checkDefaultValue(bool $value = \true) |
| 135 |
{ |
| 136 |
$this->checkDefaultValue = $value; |
| 137 |
return $this; |
| 138 |
} |
| 139 |
} |
| 140 |
|