|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field\Base; |
|
6
|
|
|
|
|
7
|
|
|
use Stringable; |
|
8
|
|
|
use Yiisoft\Html\Tag\Button; |
|
9
|
|
|
|
|
10
|
|
|
abstract class AbstractButtonField extends AbstractSimpleField |
|
11
|
|
|
{ |
|
12
|
|
|
private ?Button $button = null; |
|
13
|
|
|
private array $attributes = []; |
|
14
|
|
|
private int|float|string|Stringable|null $content = null; |
|
15
|
|
|
private ?bool $encode = null; |
|
16
|
|
|
private bool $doubleEncode = true; |
|
17
|
|
|
|
|
18
|
2 |
|
final public function button(?Button $button): static |
|
19
|
|
|
{ |
|
20
|
2 |
|
$new = clone $this; |
|
21
|
2 |
|
$new->button = $button; |
|
22
|
2 |
|
return $new; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
1 |
|
final public function attributes(array $attributes): static |
|
26
|
|
|
{ |
|
27
|
1 |
|
$new = clone $this; |
|
28
|
1 |
|
$new->attributes = array_merge($this->attributes, $attributes); |
|
29
|
1 |
|
return $new; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
1 |
|
final public function replaceAttributes(array $attributes): static |
|
33
|
|
|
{ |
|
34
|
1 |
|
$new = clone $this; |
|
35
|
1 |
|
$new->attributes = $attributes; |
|
36
|
1 |
|
return $new; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
4 |
|
final public function disabled(?bool $disabled = true): static |
|
40
|
|
|
{ |
|
41
|
4 |
|
$new = clone $this; |
|
42
|
4 |
|
$new->attributes['disabled'] = $disabled; |
|
43
|
4 |
|
return $new; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Specifies the form element the button belongs to. The value of this attribute must be the ID attribute of a form |
|
48
|
|
|
* element in the same document. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string|null $id ID of a form. |
|
51
|
|
|
* |
|
52
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form |
|
53
|
|
|
*/ |
|
54
|
1 |
|
final public function form(?string $id): static |
|
55
|
|
|
{ |
|
56
|
1 |
|
$new = clone $this; |
|
57
|
1 |
|
$new->attributes['form'] = $id; |
|
58
|
1 |
|
return $new; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
7 |
|
final public function content( |
|
62
|
|
|
int|float|string|Stringable|null $content, |
|
63
|
|
|
?bool $encode = null, |
|
64
|
|
|
bool $doubleEncode = true |
|
65
|
|
|
): static { |
|
66
|
7 |
|
$new = clone $this; |
|
67
|
7 |
|
$new->content = $content; |
|
68
|
7 |
|
$new->encode = $encode; |
|
69
|
7 |
|
$new->doubleEncode = $doubleEncode; |
|
70
|
7 |
|
return $new; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
10 |
|
final protected function generateInput(): string |
|
74
|
|
|
{ |
|
75
|
10 |
|
$button = ($this->button ?? Button::tag()) |
|
76
|
10 |
|
->type($this->getType()); |
|
77
|
|
|
|
|
78
|
10 |
|
if (!empty($this->attributes)) { |
|
79
|
3 |
|
$button = $button->attributes($this->attributes); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
10 |
|
if ($this->content !== null) { |
|
83
|
6 |
|
$button = $button |
|
84
|
6 |
|
->content((string) $this->content) |
|
85
|
6 |
|
->encode($this->encode) |
|
86
|
6 |
|
->doubleEncode($this->doubleEncode); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
10 |
|
return $button->render(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
abstract protected function getType(): string; |
|
93
|
|
|
} |
|
94
|
|
|
|