1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: *
6: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
7: *
8: * For the full copyright and license information, please view
9: * the file license.txt that was distributed with this source code.
10: */
11:
12: namespace Nette\Forms;
13:
14: use Nette;
15:
16:
17:
18: /**
19: * A user group of form controls.
20: *
21: * @author David Grudl
22: *
23: * @property-read array $controls
24: * @property-read array $options
25: */
26: class FormGroup extends Nette\Object
27: {
28: /** @var \SplObjectStorage */
29: protected $controls;
30:
31: /** @var array user options */
32: private $options = array();
33:
34:
35:
36: public function __construct()
37: {
38: $this->controls = new \SplObjectStorage;
39: }
40:
41:
42:
43: /**
44: * @return FormGroup provides a fluent interface
45: */
46: public function add()
47: {
48: foreach (func_get_args() as $num => $item) {
49: if ($item instanceof IFormControl) {
50: $this->controls->attach($item);
51:
52: } elseif ($item instanceof \Traversable || is_array($item)) {
53: foreach ($item as $control) {
54: $this->controls->attach($control);
55: }
56:
57: } else {
58: throw new \InvalidArgumentException("Only IFormControl items are allowed, the #$num parameter is invalid.");
59: }
60: }
61: return $this;
62: }
63:
64:
65:
66: /**
67: * @return array IFormControl
68: */
69: public function getControls()
70: {
71: return iterator_to_array($this->controls);
72: }
73:
74:
75:
76: /**
77: * Sets user-specific option.
78: * Options recognized by ConventionalRenderer
79: * - 'label' - textual or Html object label
80: * - 'visual' - indicates visual group
81: * - 'container' - container as Html object
82: * - 'description' - textual or Html object description
83: * - 'embedNext' - describes how render next group
84: * @param string key
85: * @param mixed value
86: * @return FormGroup provides a fluent interface
87: */
88: public function setOption($key, $value)
89: {
90: if ($value === NULL) {
91: unset($this->options[$key]);
92:
93: } else {
94: $this->options[$key] = $value;
95: }
96: return $this;
97: }
98:
99:
100:
101: /**
102: * Returns user-specific option.
103: * @param string key
104: * @param mixed default value
105: * @return mixed
106: */
107: final public function getOption($key, $default = NULL)
108: {
109: return isset($this->options[$key]) ? $this->options[$key] : $default;
110: }
111:
112:
113:
114: /**
115: * Returns user-specific options.
116: * @return array
117: */
118: final public function getOptions()
119: {
120: return $this->options;
121: }
122:
123: }
124: