Passed
Pull Request — master (#192)
by Sergei
03:06
created

AbstractButtonField::attributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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