Passed
Pull Request — master (#192)
by Alexander
05:47 queued 02:55
created

Fieldset::form()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 1
b 0
f 0
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 3
    public function __construct()
23
    {
24 3
        $this->tag = FieldsetTag::tag();
25
    }
26
27 1
    public function legend(string|Stringable|null $content, array $attributes = []): self
28
    {
29 1
        $new = clone $this;
30 1
        $new->tag = $this->tag->legend($content, $attributes);
31 1
        return $new;
32
    }
33
34
    public function legendTag(?Legend $legend): self
35
    {
36
        $new = clone $this;
37
        $new->tag = $this->tag->legendTag($legend);
38
        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
    public function disabled(bool $disabled = true): self
47
    {
48
        $new = clone $this;
49
        $new->tag = $this->tag->disabled($disabled);
50
        return $new;
51
    }
52
53
    /**
54
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
55
     */
56
    public function form(?string $formId): self
57
    {
58
        $new = clone $this;
59
        $new->tag = $this->tag->form($formId);
60
        return $new;
61
    }
62
63
    /**
64
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-name
65
     */
66
    public function name(?string $name): self
67
    {
68
        $new = clone $this;
69
        $new->tag = $this->tag->name($name);
70
        return $new;
71
    }
72
73 2
    protected function generateInput(): string
74
    {
75 2
        return $this->generateBeginInput()
76
            . "\n"
77 2
            . $this->generateEndInput();
78
    }
79
80 3
    protected function generateBeginInput(): string
81
    {
82 3
        return $this->tag->open();
83
    }
84
85 3
    protected function generateEndInput(): string
86
    {
87 3
        $content = $this->renderContent();
88
89 3
        return ($content !== '' ? $content . "\n" : '')
90 3
            . $this->tag->close();
91
    }
92
}
93