Generic
2 days ago
Options
2 days ago
ArrayMapper.php
2 days ago
Option.php
2 days ago
OptionGroup.php
2 days ago
Options.php
2 days ago
Paginated.php
2 days ago
Response.php
2 days ago
Options.php
43 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Helper\Select; |
| 6 | |
| 7 | use AC\ArrayIterator; |
| 8 | use LogicException; |
| 9 | |
| 10 | /** |
| 11 | * @property $array Option[]|OptionGroup[] |
| 12 | */ |
| 13 | class Options extends ArrayIterator |
| 14 | { |
| 15 | public function __construct(array $options) |
| 16 | { |
| 17 | parent::__construct($options); |
| 18 | |
| 19 | $this->validate(); |
| 20 | } |
| 21 | |
| 22 | private function validate(): void |
| 23 | { |
| 24 | foreach ($this as $option) { |
| 25 | if (! $option instanceof Option && ! $option instanceof OptionGroup) { |
| 26 | throw new LogicException('Only Option and OptionGroup objects allowed.'); |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | public static function create_from_array(array $array): self |
| 32 | { |
| 33 | $options = []; |
| 34 | |
| 35 | foreach ($array as $key => $value) { |
| 36 | $options[] = new Option((string)$key, (string)$value); |
| 37 | } |
| 38 | |
| 39 | return new self($options); |
| 40 | } |
| 41 | |
| 42 | } |
| 43 |