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
|
299 |
|
public function __construct(string $name, string $label = null) |
37
|
|
|
{ |
38
|
299 |
|
$this->setRequest(); |
39
|
299 |
|
$this->setName($name); |
40
|
|
|
|
41
|
299 |
|
if (!is_null($label)) { |
42
|
122 |
|
$this->setLabel($label); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @psalm-suppress PossiblyNullReference |
48
|
|
|
*/ |
49
|
89 |
|
public function setForm(?Form $form): void |
50
|
|
|
{ |
51
|
89 |
|
if ($form === null) { |
52
|
2 |
|
return; |
53
|
|
|
} |
54
|
87 |
|
$this->form = $form; |
55
|
87 |
|
$this->setDefault($this->getForm()->getDefaultsHandler()->getValue($this->getName())); |
56
|
|
|
|
57
|
87 |
|
if ($this instanceof Fillable) { |
58
|
19 |
|
foreach ($this->getElements() as $element) { |
59
|
2 |
|
$element->setDefault($this->getDefaultValue()); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
89 |
|
public function getForm(): ?Form |
65
|
|
|
{ |
66
|
89 |
|
return $this->form; |
67
|
|
|
} |
68
|
|
|
|
69
|
90 |
|
public function unsetForm(): void |
70
|
|
|
{ |
71
|
90 |
|
$this->form = null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return true|void |
76
|
|
|
*/ |
77
|
90 |
|
public function prepare() |
78
|
|
|
{ |
79
|
90 |
|
$this->unsetForm(); |
80
|
|
|
} |
81
|
|
|
|
82
|
84 |
|
public function getType(): string |
83
|
|
|
{ |
84
|
84 |
|
return $this->type; |
85
|
|
|
} |
86
|
|
|
|
87
|
305 |
|
protected function setName(string $name): ElementInterface |
88
|
|
|
{ |
89
|
305 |
|
$this->name = trim($name); |
90
|
305 |
|
$this->setAttributes( |
91
|
305 |
|
AttributeFactory::createFromArray([ |
92
|
305 |
|
'id' => $this->name, |
93
|
305 |
|
'name' => $this->name |
94
|
|
|
]) |
95
|
|
|
); |
96
|
|
|
|
97
|
305 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
196 |
|
public function getName(): string |
101
|
|
|
{ |
102
|
196 |
|
return $this->name; |
103
|
|
|
} |
104
|
|
|
|
105
|
123 |
|
public function setLabel(?string $title = null): ElementInterface |
106
|
|
|
{ |
107
|
123 |
|
$this->label = $title; |
108
|
123 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
45 |
|
public function getLabel(): ?string |
112
|
|
|
{ |
113
|
45 |
|
return $this->label; |
114
|
|
|
} |
115
|
|
|
|
116
|
87 |
|
protected function setDefault(mixed $value = null): self |
117
|
|
|
{ |
118
|
87 |
|
if (is_array($value)) { |
119
|
|
|
/** @var array<Closure|scalar|null> $value */ |
120
|
1 |
|
$this->setAttribute( |
121
|
1 |
|
AttributeFactory::create('value', $value[0]) |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
87 |
|
if (is_string($value) || is_numeric($value)) { |
126
|
|
|
// $this->setValue($value); |
127
|
10 |
|
$this->setAttribute( |
128
|
10 |
|
AttributeFactory::create('value', $value) |
129
|
|
|
); |
130
|
|
|
} |
131
|
87 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
16 |
|
public function baseHtml(): string |
135
|
|
|
{ |
136
|
16 |
|
return "<input type=\"{$this->getType()}\"{$this->getAttributesString()}>"; |
137
|
|
|
} |
138
|
|
|
|
139
|
90 |
|
public function isAllowSameNames(): bool |
140
|
|
|
{ |
141
|
90 |
|
return $this->allowSameNames; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|