-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Form.php
145 lines (126 loc) · 3.91 KB
/
Form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
/**
* This file is part of the EaseHtml package
*
* https://github.com/VitexSoftware/php-ease-html
*
* (c) Vítězslav Dvořák <http://vitexsoftware.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Ease\Html;
/**
* @author Vítězslav Dvořák <info@vitexsoftware.cz>, Jana Viktorie Borbina <jana@borbina.com>
*/
class Form extends PairTag
{
/**
* Form goal.
*
* @var string URL form goal
*/
public string $formTarget = '';
/**
* sending method.
*
* @var string GET|POST
*/
public string $formMethod = '';
/**
* Sets up form name.
*/
public bool $setName = false;
/**
* Html Form Tag.
*
* @param array $properties tag properties f.e.
* ('enctype' => 'multipart/form-data')
* @param mixed $formContents inside form elements
*/
public function __construct(array $properties = [], $formContents = [])
{
if (!\array_key_exists('method', $properties)) {
$properties['method'] = 'POST';
}
parent::__construct('form', $properties, $formContents);
}
/**
* Try to find tag of the given name in inserted objects.
*
* @param string $searchFor searched elements name
* @param \Ease\Container $where object in where the search happens
*
* @return null|\Ease\Container
*/
public function &objectContentSearch(string $searchFor, $where = null)
{
if (null === $where) {
$where = &$this;
}
$itemFound = null;
if (isset($where->pageParts) && \is_array($where->pageParts) && \count($where->pageParts)) {
foreach ($where->pageParts as $pagePart) {
if (\is_object($pagePart)) {
if (method_exists($pagePart, 'getTagName')) {
if ($pagePart->getTagName() === $searchFor) {
return $pagePart;
}
} else {
$itemFound = $this->objectContentSearch(
$searchFor,
$pagePart,
);
if ($itemFound) {
return $itemFound;
}
}
}
}
}
return $itemFound;
}
/**
* Fills up inserted objects with data.
*/
public function fillUp(array $data): void
{
if (null === $data) {
$data = $this->getData();
}
self::fillMeUp($data, $this);
}
/**
* Goes through all inserted objects and if their names match the data keys, sets up a value.
*
* @param array $data associative data field
* @param \Ease\Container|mixed $form form to be filled
*/
public static function fillMeUp(array $data, &$form): void
{
if (!empty($form->pageParts)) {
foreach ($form->pageParts as $partName => $part) {
if (!empty($part->pageParts)) {
self::fillMeUp($data, $part);
}
if (\is_object($part)) {
if (
method_exists($part, 'setValue') && method_exists(
$part,
'getTagName',
)
) {
$tagName = $part->getTagName();
if (isset($data[$tagName])) {
$part->setValue((string) $data[$tagName], true);
}
}
if (method_exists($part, 'setValues')) {
$part->setValues($data);
}
}
}
}
}
}