depage-forms
v2.0.0
html forms made easy
Toggle main menu visibility
Loading...
Searching...
No Matches
Container.php
Go to the documentation of this file.
1
<?php
2
10
11
namespace
Depage\HtmlForm\Abstracts
;
12
13
use
Depage\HtmlForm\Elements
;
14
use
Depage\HtmlForm\Exceptions
;
15
59
abstract
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
118
throw
new
Exceptions\UnknownElementTypeException
($type);
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 : */
Depage\HtmlForm\Abstracts\Container
container element base class
Definition
Container.php:60
Depage\HtmlForm\Abstracts\Container\addChildElements
addChildElements()
Sub-element generator hook.
Definition
Container.php:165
Depage\HtmlForm\Abstracts\Container\validate
validate()
Validates container and its contents.
Definition
Container.php:265
Depage\HtmlForm\Abstracts\Container\$form
$form
Parent form object reference.
Definition
Container.php:72
Depage\HtmlForm\Abstracts\Container\__call
__call(string $function, array $arguments)
HTML escaping and add subelements.
Definition
Container.php:104
Depage\HtmlForm\Abstracts\Container\clearValue
clearValue()
Deletes values of all child elements.
Definition
Container.php:284
Depage\HtmlForm\Abstracts\Container\addHtml
addHtml(string $html)
Adds a new custom HTML element to the container.
Definition
Container.php:219
Depage\HtmlForm\Abstracts\Container\__construct
__construct(string $name, array $parameters, object $form)
container class constructor
Definition
Container.php:81
Depage\HtmlForm\Abstracts\Container\addStepNav
addStepNav(array $parameter=[])
Adds automatic step navigation to output.
Definition
Container.php:232
Depage\HtmlForm\Abstracts\Container\$elementsAndHtml
$elementsAndHtml
Input element, fieldset and custom HTML object references.
Definition
Container.php:68
Depage\HtmlForm\Abstracts\Container\getElement
getElement(string $name, bool $includeFieldsets=false)
Gets subelement by name.
Definition
Container.php:200
Depage\HtmlForm\Abstracts\Container\$elements
$elements
References to input elements and fieldsets.
Definition
Container.php:64
Depage\HtmlForm\Abstracts\Container\setRequired
setRequired(bool $required=true)
Sets required-attribute.
Definition
Container.php:249
Depage\HtmlForm\Abstracts\Container\getElements
getElements(bool $includeFieldsets=false)
Returns containers subelements.
Definition
Container.php:175
Depage\HtmlForm\Abstracts\Container\addElement
addElement(string $type, string $name, array $parameters)
Generates sub-elements.
Definition
Container.php:137
Depage\HtmlForm\Abstracts\Element
behold: the über-class
Definition
Element.php:32
Depage\HtmlForm\Abstracts\Element\$valid
$valid
Contains element validation status/result.
Definition
Element.php:41
Depage\HtmlForm\Abstracts\Element\$class
$class
CSS class of the container element.
Definition
Element.php:90
Depage\HtmlForm\Abstracts\Element\$log
$log
Log object reference.
Definition
Element.php:51
Depage\HtmlForm\Abstracts\Element\$name
$name
Element name.
Definition
Element.php:36
Depage\HtmlForm\Elements\Html
Can be used to insert custom HTML between rendered HTML elements.
Definition
Html.php:38
Depage\HtmlForm\Elements\Stepnav
Can be used to insert a step navigation.
Definition
Stepnav.php:19
Depage\HtmlForm\Exceptions\UnknownElementTypeException
thrown when attemting to instantiate an inexistent element class
Definition
UnknownElementTypeException.php:17
Depage\HtmlForm\Abstracts
Abstract element classes.
Definition
Container.php:11
Depage\HtmlForm\Elements
Classes for HTML input-elements.
Definition
Address.php:10
Depage\HtmlForm\Exceptions
HtmlForm exceptions.
Definition
DuplicateElementNameException.php:11
Abstracts
Container.php
Generated on
for depage-forms by
doxygen
1.18.0
For an overview of all docs, go to
docs.depage.net