PluginProbe
Packeta / 2.1
Packeta v2.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / forms / src / Forms / ControlGroup.php

ControlGroup.php in Packeta 2.1, at deps/nette/forms/src/Forms/ControlGroup.php

97 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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