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