-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormElement.php
190 lines (156 loc) · 4.81 KB
/
FormElement.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace SunnyFlail\Forms\Form;
use SunnyFlail\HtmlAbstraction\Elements\ButtonElement;
use SunnyFlail\HtmlAbstraction\Traits\AttributeTrait;
use SunnyFlail\Forms\Interfaces\IFormElement;
use SunnyFlail\Forms\Interfaces\IFileField;
use SunnyFlail\Forms\Traits\MappableTrait;
use Psr\Http\Message\UploadedFileInterface;
use SunnyFlail\Forms\Traits\WrapperFieldTrait;
use SunnyFlail\Forms\Traits\ErrorTrait;
use SunnyFlail\HtmlAbstraction\Interfaces\IElement;
/**
* Abstraction over html forms with HTTP parameter resolving
*/
abstract class FormElement implements IFormElement
{
use AttributeTrait, MappableTrait, WrapperFieldTrait, ErrorTrait;
/**
* @var bool $valid Whether form's field values are valid
*/
protected ?bool $valid = null;
/**
* @var string $attributes Attributes for this form's html tag
*/
protected array $attributes = [];
/**
* @var string $formMethod HTTP method name this form will respond to
*/
protected string $formMethod = 'GET';
/**
* @var string $formName Name of the form
*/
protected string $formName;
/**
* @var string|null $formAction Action attribute of form
*/
protected ?string $formAction = null;
/**
* @var string $buttonText Text to be displayed inside submit button
*/
protected string $buttonText = 'Submit';
/**
* @var bool $useHtmlValidation Should this form use client-side browser form validation
*/
protected bool $useHtmlValidation = true;
/**
* @var bool $renderButton Should submit button be rendered
*/
protected bool $renderButton = true;
/**
* @var bool $withFiles If set to true sets the enctype (encoding type) to multipart/form-data
*/
protected bool $withFiles = false;
protected array $buttonAttributes = [];
protected array $buttonElements = [];
public function getName(): string
{
return $this->formName;
}
public function getFormMethod(): string
{
return $this->formMethod;
}
public function resolveForm(array $requestParams, UploadedFileInterface|array $uploadedFiles): bool
{
$valid = true;
foreach ($this->fields as $field) {
if ($field instanceof IFileField) {
$field->resolve($uploadedFiles);
continue;
}
$field->resolve($requestParams);
if (!$field->isValid() && $field->isRequired()) {
$valid = false;
}
}
return ($this->valid = $valid);
}
public function addError(string $error): IFormElement
{
$this->error = $error;
$this->valid = false;
return $this;
}
/**
* Returns a html string representation of form
*
* @return string
*/
public function __toString(): string
{
$button = '';
if ($this->renderButton) {
$button = $this->getSubmitButton();
}
$elements = implode('', [
...$this->topElements,
...array_values($this->fields),
...$this->middleElements,
$this->getErrorElement(),
$button,
...$this->bottomElements
]);
return '<form' . $this->getHTMLAttributes() . '>' . $elements . '</form>';
}
/**
* Returns an array representing this form
*
* @return array
*/
public function jsonSerialize()
{
$fields = $this->serializeFieldContainer($this);
$attributes = $this->getAttributes();
$id = array_assoc_shift('id', $attributes);
$method = array_assoc_shift('method', $attributes);
return [
'tagName' => 'FORM',
'id' => $id,
'valid' => $this->valid,
'method' => $method,
'attributes' => $attributes,
'action' => $this->formAction,
'fields' => $fields,
'error' => $this->error
];
}
public function getAttributes(): array
{
$attributes = $this->attributes;
if (!$this->useHtmlValidation) {
$attributes["novalidate"] = true;
}
if ($this->withFiles) {
$attributes['enctype'] = 'multipart/form-data';
}
$attributes['id'] = $attributes['id'] ?? 'form__' . $this->formName;
$attributes['method'] = $this->formMethod;
return $attributes;
}
public function getHTMLAttributes(): string
{
return $this->getAttributeString(
$this->getAttributes()
);
}
public function getSubmitButton(): IElement
{
return new ButtonElement(
type: "submit",
attributes: $this->buttonAttributes,
text: $this->buttonText,
nestedElements: $this->buttonElements
);
}
}