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