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