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:
8: namespace Nette\Forms;
9:
10: use Nette;
11:
12:
13: /**
14: * A user group of form controls.
15: */
16: class ControlGroup
17: {
18: use Nette\SmartObject;
19:
20: /** @var \SplObjectStorage */
21: protected $controls;
22:
23: /** @var array user options */
24: private $options = [];
25:
26:
27: public function __construct()
28: {
29: $this->controls = new \SplObjectStorage;
30: }
31:
32:
33: /**
34: * @return static
35: */
36: public function add(...$items)
37: {
38: foreach ($items as $item) {
39: if ($item instanceof IControl) {
40: $this->controls->attach($item);
41:
42: } elseif ($item instanceof Container) {
43: foreach ($item->getComponents() as $component) {
44: $this->add($component);
45: }
46: } elseif ($item instanceof \Traversable || is_array($item)) {
47: $this->add(...$item);
48:
49: } else {
50: $type = is_object($item) ? get_class($item) : gettype($item);
51: throw new Nette\InvalidArgumentException("IControl or Container items expected, $type given.");
52: }
53: }
54: return $this;
55: }
56:
57:
58: /**
59: * @return void
60: */
61: public function remove(IControl $control)
62: {
63: $this->controls->detach($control);
64: }
65:
66:
67: /**
68: * @return void
69: */
70: public function removeOrphans()
71: {
72: foreach ($this->controls as $control) {
73: if (!$control->getForm(false)) {
74: $this->controls->detach($control);
75: }
76: }
77: }
78:
79:
80: /**
81: * @return IControl[]
82: */
83: public function getControls()
84: {
85: return iterator_to_array($this->controls);
86: }
87:
88:
89: /**
90: * Sets user-specific option.
91: * Options recognized by DefaultFormRenderer
92: * - 'label' - textual or IHtmlString object label
93: * - 'visual' - indicates visual group
94: * - 'container' - container as Html object
95: * - 'description' - textual or IHtmlString object description
96: * - 'embedNext' - describes how render next group
97: *
98: * @param string
99: * @param mixed
100: * @return static
101: */
102: public function setOption($key, $value)
103: {
104: if ($value === null) {
105: unset($this->options[$key]);
106:
107: } else {
108: $this->options[$key] = $value;
109: }
110: return $this;
111: }
112:
113:
114: /**
115: * Returns user-specific option.
116: * @param string
117: * @param mixed
118: * @return mixed
119: */
120: public function getOption($key, $default = null)
121: {
122: return isset($this->options[$key]) ? $this->options[$key] : $default;
123: }
124:
125:
126: /**
127: * Returns user-specific options.
128: * @return array
129: */
130: public function getOptions()
131: {
132: return $this->options;
133: }
134: }
135: