| 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; |
| 9 |
|
| 10 |
use Packetery\Nette; |
| 11 |
/** |
| 12 |
* A user group of form controls. |
| 13 |
*/ |
| 14 |
class ControlGroup |
| 15 |
{ |
| 16 |
use \Packetery\Nette\SmartObject; |
| 17 |
/** @var \SplObjectStorage */ |
| 18 |
protected $controls; |
| 19 |
/** @var array user options */ |
| 20 |
private $options = []; |
| 21 |
public function __construct() |
| 22 |
{ |
| 23 |
$this->controls = new \SplObjectStorage(); |
| 24 |
} |
| 25 |
/** @return static */ |
| 26 |
public function add(...$items) |
| 27 |
{ |
| 28 |
foreach ($items as $item) { |
| 29 |
if ($item instanceof Control) { |
| 30 |
$this->controls->attach($item); |
| 31 |
} elseif ($item instanceof Container) { |
| 32 |
foreach ($item->getComponents() as $component) { |
| 33 |
$this->add($component); |
| 34 |
} |
| 35 |
} elseif (\is_iterable($item)) { |
| 36 |
$this->add(...$item); |
| 37 |
} else { |
| 38 |
$type = \is_object($item) ? \get_class($item) : \gettype($item); |
| 39 |
throw new \Packetery\Nette\InvalidArgumentException("Control or Container items expected, {$type} given."); |
| 40 |
} |
| 41 |
} |
| 42 |
return $this; |
| 43 |
} |
| 44 |
public function remove(Control $control) : void |
| 45 |
{ |
| 46 |
$this->controls->detach($control); |
| 47 |
} |
| 48 |
public function removeOrphans() : void |
| 49 |
{ |
| 50 |
foreach ($this->controls as $control) { |
| 51 |
if (!$control->getForm(\false)) { |
| 52 |
$this->controls->detach($control); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
/** @return Control[] */ |
| 57 |
public function getControls() : array |
| 58 |
{ |
| 59 |
return \iterator_to_array($this->controls); |
| 60 |
} |
| 61 |
/** |
| 62 |
* Sets user-specific option. |
| 63 |
* Options recognized by DefaultFormRenderer |
| 64 |
* - 'label' - textual or \Packetery\Nette\HtmlStringable object label |
| 65 |
* - 'visual' - indicates visual group |
| 66 |
* - 'container' - container as Html object |
| 67 |
* - 'description' - textual or \Packetery\Nette\HtmlStringable object description |
| 68 |
* - 'embedNext' - describes how render next group |
| 69 |
* |
| 70 |
* @return static |
| 71 |
*/ |
| 72 |
public function setOption(string $key, $value) |
| 73 |
{ |
| 74 |
if ($value === null) { |
| 75 |
unset($this->options[$key]); |
| 76 |
} else { |
| 77 |
$this->options[$key] = $value; |
| 78 |
} |
| 79 |
return $this; |
| 80 |
} |
| 81 |
/** |
| 82 |
* Returns user-specific option. |
| 83 |
* @return mixed |
| 84 |
*/ |
| 85 |
public function getOption(string $key, $default = null) |
| 86 |
{ |
| 87 |
return $this->options[$key] ?? $default; |
| 88 |
} |
| 89 |
/** |
| 90 |
* Returns user-specific options. |
| 91 |
*/ |
| 92 |
public function getOptions() : array |
| 93 |
{ |
| 94 |
return $this->options; |
| 95 |
} |
| 96 |
} |
| 97 |
|