depage-forms v1.4.1
html forms made easy
Loading...
Searching...
No Matches
Container.php
Go to the documentation of this file.
1<?php
2
10
12
15
59abstract class Container extends Element
60{
64 protected $elements = [];
68 protected $elementsAndHtml = [];
72 protected $form;
81 public function __construct(string $name, array $parameters, object $form)
82 {
83 $this->form = $form;
84
85 parent::__construct($name, $parameters, $form);
86 }
87
104 public function __call(string $function, array $arguments): mixed
105 {
106 if (substr($function, 0, 3) === 'add') {
107 $type = str_replace('add', '', $function);
108
109 foreach ($this->form->getNamespaces() as $namespace) {
110 $class = $namespace . '\\' . $type;
111 if (class_exists($class)) {
112 $name = isset($arguments[0]) ? $arguments[0] : '';
113 $parameters = isset($arguments[1]) ? $arguments[1] : [];
114 return $this->addElement($class, $name, $parameters);
115 }
116 }
117
119 } else {
120 return parent::__call($function, $arguments);
121 }
122 }
123
137 protected function addElement(string $type, string $name, array $parameters): object
138 {
139 $parameters['log'] = $this->log;
140
141 $newElement = new $type($name, $parameters, $this->form);
142
143 $this->elements[] = $newElement;
144 $this->elementsAndHtml[] = $newElement;
145
146 if ($newElement instanceof container) {
147 $newElement->addChildElements();
148 }
149
150 return $newElement;
151 }
152
165 public function addChildElements(): void {}
175 public function getElements(bool $includeFieldsets = false): array
176 {
177 $allElements = [];
178 foreach ($this->elements as $element) {
179 if ($element instanceof Container) {
180 if ($includeFieldsets) {
181 $allElements[] = $element;
182 }
183 $allElements = array_merge($allElements, $element->getElements($includeFieldsets));
184 } else {
185 $allElements[] = $element;
186 }
187 }
188
189 return $allElements;
190 }
191
200 public function getElement(string $name, bool $includeFieldsets = false): object|bool
201 {
202 foreach ($this->getElements($includeFieldsets) as $element) {
203 if ($name === $element->getName()) {
204 return $element;
205 }
206 }
207
208 return false;
209 }
210
219 public function addHtml(string $html): object
220 {
221 $htmlElement = new Elements\Html($html);
222
223 $this->elementsAndHtml[] = $htmlElement;
224
225 return $htmlElement;
226 }
227
232 public function addStepNav(array $parameter = []): object
233 {
234 $htmlElement = new Elements\Stepnav($parameter, $this->form);
235
236 $this->elementsAndHtml[] = $htmlElement;
237
238 return $htmlElement;
239 }
240
249 public function setRequired(bool $required = true): void
250 {
251 $required = (bool) $required;
252
253 foreach ($this->elements as $element) {
254 $element->setRequired($required);
255 }
256 }
257
265 public function validate(): bool
266 {
267 if (!$this->validated) {
268 $this->validated = true;
269
270 $this->valid = true;
271 foreach ($this->elements as $element) {
272 $valid = $element->validate();
273 $this->valid = $this->valid && $valid;
274 }
275 }
276
277 return $this->valid;
278 }
279
284 public function clearValue(): void
285 {
286 foreach ($this->getElements(true) as $element) {
287 $element->clearValue();
288 }
289 }
290}
291
292/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
container element base class
Definition Container.php:60
addChildElements()
Sub-element generator hook.
validate()
Validates container and its contents.
$form
Parent form object reference.
Definition Container.php:72
__call(string $function, array $arguments)
HTML escaping and add subelements.
clearValue()
Deletes values of all child elements.
addHtml(string $html)
Adds a new custom HTML element to the container.
__construct(string $name, array $parameters, object $form)
container class constructor
Definition Container.php:81
addStepNav(array $parameter=[])
Adds automatic step navigation to output.
$elementsAndHtml
Input element, fieldset and custom HTML object references.
Definition Container.php:68
getElement(string $name, bool $includeFieldsets=false)
Gets subelement by name.
$elements
References to input elements and fieldsets.
Definition Container.php:64
setRequired(bool $required=true)
Sets required-attribute.
getElements(bool $includeFieldsets=false)
Returns containers subelements.
addElement(string $type, string $name, array $parameters)
Generates sub-elements.
behold: the über-class
Definition Element.php:30
$valid
Contains element validation status/result.
Definition Element.php:39
$class
CSS class of the container element.
Definition Element.php:88
$log
Log object reference.
Definition Element.php:49
Can be used to insert custom HTML between rendered HTML elements.
Definition Html.php:36
Can be used to insert a step navigation.
Definition Stepnav.php:19
thrown when attemting to instantiate an inexistent element class
Abstract element classes.
Definition Container.php:11
Classes for HTML input-elements.
Definition Address.php:10