1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field; |
6
|
|
|
|
7
|
|
|
use Stringable; |
8
|
|
|
use Yiisoft\Form\Field\Base\FieldContentTrait; |
9
|
|
|
use Yiisoft\Form\Field\Base\PartsField; |
10
|
|
|
use Yiisoft\Html\Tag\Fieldset as FieldsetTag; |
11
|
|
|
use Yiisoft\Html\Tag\Legend; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-elements.html#the-fieldset-element |
15
|
|
|
*/ |
16
|
|
|
final class Fieldset extends PartsField |
17
|
|
|
{ |
18
|
|
|
use FieldContentTrait; |
19
|
|
|
|
20
|
|
|
private FieldsetTag $tag; |
21
|
|
|
|
22
|
13 |
|
public function __construct() |
23
|
|
|
{ |
24
|
13 |
|
$this->tag = FieldsetTag::tag(); |
25
|
|
|
} |
26
|
|
|
|
27
|
3 |
|
public function legend(string|Stringable|null $content, array $attributes = []): self |
28
|
|
|
{ |
29
|
3 |
|
$new = clone $this; |
30
|
3 |
|
$new->tag = $this->tag->legend($content, $attributes); |
31
|
3 |
|
return $new; |
32
|
|
|
} |
33
|
|
|
|
34
|
2 |
|
public function legendTag(?Legend $legend): self |
35
|
|
|
{ |
36
|
2 |
|
$new = clone $this; |
37
|
2 |
|
$new->tag = $this->tag->legendTag($legend); |
38
|
2 |
|
return $new; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-elements.html#attr-fieldset-disabled |
43
|
|
|
* |
44
|
|
|
* @param bool $disabled Whether fieldset is disabled. |
45
|
|
|
*/ |
46
|
2 |
|
public function disabled(bool $disabled = true): self |
47
|
|
|
{ |
48
|
2 |
|
$new = clone $this; |
49
|
2 |
|
$new->tag = $this->tag->disabled($disabled); |
50
|
2 |
|
return $new; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form |
55
|
|
|
*/ |
56
|
2 |
|
public function form(?string $formId): self |
57
|
|
|
{ |
58
|
2 |
|
$new = clone $this; |
59
|
2 |
|
$new->tag = $this->tag->form($formId); |
60
|
2 |
|
return $new; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-name |
65
|
|
|
*/ |
66
|
2 |
|
public function name(?string $name): self |
67
|
|
|
{ |
68
|
2 |
|
$new = clone $this; |
69
|
2 |
|
$new->tag = $this->tag->name($name); |
70
|
2 |
|
return $new; |
71
|
|
|
} |
72
|
|
|
|
73
|
9 |
|
protected function generateInput(): string |
74
|
|
|
{ |
75
|
9 |
|
return $this->generateBeginInput() |
76
|
|
|
. "\n" |
77
|
9 |
|
. $this->generateEndInput(); |
78
|
|
|
} |
79
|
|
|
|
80
|
11 |
|
protected function generateBeginInput(): string |
81
|
|
|
{ |
82
|
11 |
|
return $this->tag->open(); |
83
|
|
|
} |
84
|
|
|
|
85
|
11 |
|
protected function generateEndInput(): string |
86
|
|
|
{ |
87
|
11 |
|
$content = $this->renderContent(); |
88
|
|
|
|
89
|
11 |
|
return ($content !== '' ? $content . "\n" : '') |
90
|
11 |
|
. $this->tag->close(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|