1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://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 ControlGroup 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 ControlGroup provides a fluent interface
45: */
46: public function add()
47: {
48: foreach (func_get_args() as $num => $item) {
49: if ($item instanceof IControl) {
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 Nette\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 DefaultFormRenderer
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: *
85: * @param string key
86: * @param mixed value
87: * @return ControlGroup provides a fluent interface
88: */
89: public function setOption($key, $value)
90: {
91: if ($value === NULL) {
92: unset($this->options[$key]);
93:
94: } else {
95: $this->options[$key] = $value;
96: }
97: return $this;
98: }
99:
100:
101:
102: /**
103: * Returns user-specific option.
104: * @param string key
105: * @param mixed default value
106: * @return mixed
107: */
108: final public function getOption($key, $default = NULL)
109: {
110: return isset($this->options[$key]) ? $this->options[$key] : $default;
111: }
112:
113:
114:
115: /**
116: * Returns user-specific options.
117: * @return array
118: */
119: final public function getOptions()
120: {
121: return $this->options;
122: }
123:
124: }
125: