1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Enjoys\Forms; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Enjoys\Forms\Interfaces\ElementInterface; |
9
|
|
|
use Enjoys\Forms\Interfaces\Fillable; |
10
|
|
|
use Enjoys\Forms\Traits\Attributes; |
11
|
|
|
use Enjoys\Forms\Traits\Request; |
12
|
|
|
|
13
|
|
|
abstract class Element implements ElementInterface |
14
|
|
|
{ |
15
|
|
|
use Attributes; |
16
|
|
|
use Request; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
20
|
|
|
*/ |
21
|
|
|
protected string $name; |
22
|
|
|
protected string $type = ''; |
23
|
|
|
|
24
|
|
|
protected ?string $label = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Флаг для обозначения обязательности заполнения этого элемента или нет |
28
|
|
|
*/ |
29
|
|
|
protected bool $required = false; |
30
|
|
|
|
31
|
|
|
protected ?Form $form = null; |
32
|
|
|
|
33
|
|
|
protected bool $allowSameNames = false; |
34
|
|
|
|
35
|
|
|
|
36
|
306 |
|
public function __construct(string $name, string $label = null) |
37
|
|
|
{ |
38
|
306 |
|
$this->setRequest(); |
39
|
306 |
|
$this->setName($name); |
40
|
|
|
|
41
|
306 |
|
if (!is_null($label)) { |
42
|
122 |
|
$this->setLabel($label); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @psalm-suppress PossiblyNullReference |
48
|
|
|
*/ |
49
|
93 |
|
public function setForm(?Form $form): void |
50
|
|
|
{ |
51
|
93 |
|
if ($form === null) { |
52
|
2 |
|
return; |
53
|
|
|
} |
54
|
91 |
|
$this->form = $form; |
55
|
91 |
|
$this->setDefault($this->getForm()->getDefaultsHandler()->getValue($this->getName())); |
56
|
|
|
|
57
|
91 |
|
if ($this instanceof Fillable) { |
58
|
19 |
|
foreach ($this->getElements() as $element) { |
59
|
2 |
|
$element->setDefault($this->getDefaultValue()); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
93 |
|
public function getForm(): ?Form |
65
|
|
|
{ |
66
|
93 |
|
return $this->form; |
67
|
|
|
} |
68
|
|
|
|
69
|
94 |
|
public function unsetForm(): void |
70
|
|
|
{ |
71
|
94 |
|
$this->form = null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
94 |
|
public function prepare(): bool |
76
|
|
|
{ |
77
|
94 |
|
$this->unsetForm(); |
78
|
94 |
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
84 |
|
public function getType(): string |
82
|
|
|
{ |
83
|
84 |
|
return $this->type; |
84
|
|
|
} |
85
|
|
|
|
86
|
312 |
|
protected function setName(string $name): ElementInterface |
87
|
|
|
{ |
88
|
312 |
|
$this->name = trim($name); |
89
|
312 |
|
$this->setAttributes( |
90
|
312 |
|
AttributeFactory::createFromArray([ |
91
|
312 |
|
'id' => $this->name, |
92
|
312 |
|
'name' => $this->name |
93
|
312 |
|
]) |
94
|
312 |
|
); |
95
|
|
|
|
96
|
312 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
203 |
|
public function getName(): string |
100
|
|
|
{ |
101
|
203 |
|
return $this->name; |
102
|
|
|
} |
103
|
|
|
|
104
|
123 |
|
public function setLabel(?string $title = null): ElementInterface |
105
|
|
|
{ |
106
|
123 |
|
$this->label = $title; |
107
|
123 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
45 |
|
public function getLabel(): ?string |
111
|
|
|
{ |
112
|
45 |
|
return $this->label; |
113
|
|
|
} |
114
|
|
|
|
115
|
91 |
|
protected function setDefault(mixed $value = null): self |
116
|
|
|
{ |
117
|
91 |
|
if (is_array($value)) { |
118
|
|
|
/** @var array<Closure|scalar|null> $value */ |
119
|
1 |
|
$this->setAttribute( |
120
|
1 |
|
AttributeFactory::create('value', $value[0]) |
121
|
1 |
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
91 |
|
if (is_string($value) || is_numeric($value)) { |
125
|
|
|
// $this->setValue($value); |
126
|
10 |
|
$this->setAttribute( |
127
|
10 |
|
AttributeFactory::create('value', $value) |
128
|
10 |
|
); |
129
|
|
|
} |
130
|
91 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
16 |
|
public function baseHtml(): string |
134
|
|
|
{ |
135
|
16 |
|
return "<input type=\"{$this->getType()}\"{$this->getAttributesString()}>"; |
136
|
|
|
} |
137
|
|
|
|
138
|
94 |
|
public function isAllowSameNames(): bool |
139
|
|
|
{ |
140
|
94 |
|
return $this->allowSameNames; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|