-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileUploadField.php
104 lines (92 loc) · 3.16 KB
/
FileUploadField.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
<?php
namespace SunnyFlail\Forms\Fields;
use SunnyFlail\Forms\Interfaces\IWrapperField;
use SunnyFlail\HtmlAbstraction\Elements\ContainerElement;
use SunnyFlail\HtmlAbstraction\Elements\FileElement;
use SunnyFlail\Forms\Interfaces\IFileConstraint;
use SunnyFlail\Forms\Interfaces\IInputField;
use SunnyFlail\Forms\Interfaces\IFileField;
use SunnyFlail\Forms\Traits\WrapperFieldTrait;
use SunnyFlail\Forms\Traits\FileUploadFieldTrait;
use SunnyFlail\Forms\Traits\LabeledElementTrait;
use SunnyFlail\HtmlAbstraction\Interfaces\IElement;
final class FileUploadField implements IInputField, IFileField, IWrapperField
{
use FileUploadFieldTrait, LabeledElementTrait, WrapperFieldTrait;
/**
* @param IFileConstraint[] $constraints
*/
public function __construct(
string $name,
bool $required = true,
bool $multiple = true,
array $constraints = [],
array $topElements = [],
array $middleElements = [],
array $bottomElements = [],
array $errorMessages = [],
protected array $containerAttributes = [],
array $labelAttributes = [],
?string $labelText = null,
protected array $inputAttributes = [],
protected bool $terminateOnError = false
) {
$this->valid = null;
$this->error = null;
$this->value = null;
$this->name = $name;
$this->required = $required;
$this->multiple = $multiple;
$this->labelText = $labelText;
$this->constraints = $constraints;
$this->topElements = $topElements;
$this->middleElements = $middleElements;
$this->bottomElements = $bottomElements;
$this->labelAttributes = $labelAttributes;
$this->errorMessages = $errorMessages;
}
public function __toString(): string
{
return $this->getContainerElement()->__toString();
}
public function getContainerElement(): IElement|array
{
return new ContainerElement(
attributes: $this->containerAttributes,
nestedElements: [
...$this->topElements,
$this->getLabelElement(),
...$this->middleElements,
$this->getInputElement(),
...$this->bottomElements,
$this->getErrorElement()
]
);
}
public function getInputElement(): IElement|array
{
$attributes = $this->inputAttributes;
$attributes['required'] = $this->required;
return new FileElement(
name: $this->getFullName(),
id: $this->getInputId(),
multiple: $this->multiple,
attributes: $this->inputAttributes
);
}
public function jsonSerialize()
{
$attributes = $this->inputAttributes;
$attributes['type'] = 'file';
return [
'tagName' => 'INPUT',
'id' => $this->getInputId(),
'multiple' => $this->multiple,
'required' => $this->required,
'valid' => $this->valid,
'error' => $this->error,
'label' => $this->labelText ?? $this->name,
'attributes' => $attributes
];
}
}