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