Passed
Pull Request — master (#155)
by Wilmer
11:04
created

Checkbox::value()   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 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Widget;
6
7
use InvalidArgumentException;
8
use Stringable;
9
use Yiisoft\Form\Helper\HtmlForm;
10
use Yiisoft\Form\Widget\Attribute\InputAttributes;
11
use Yiisoft\Form\Widget\Attribute\ModelAttributes;
12
use Yiisoft\Html\Tag\Input\Checkbox as CheckboxTag;
13
use Yiisoft\Widget\Widget;
14
15
/**
16
 * The input element with a type attribute whose value is "checkbox" represents a state or option that can be toggled.
17
 *
18
 * This method will generate the "checked" tag attribute according to the form attribute value.
19
 *
20
 * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.checkbox.html#input.checkbox
21
 */
22
final class Checkbox extends Widget
23
{
24
    use InputAttributes;
25
    use ModelAttributes;
26
27
    private bool $enclosedByLabel = true;
28
    private string $label = '';
29
    private array $labelAttributes = [];
30
    /** @var bool|float|int|string|Stringable|null */
31
    private $uncheckValue = '0';
32
    /** @var scalar|Stringable|null */
33
    private $value = '1';
34
35
    /**
36
     * If the widget should be enclosed by label.
37
     *
38
     * @param bool $value If the widget should be en closed by label.
39
     *
40
     * @return static
41
     */
42 12
    public function enclosedByLabel(bool $value = true): self
43
    {
44 12
        $new = clone $this;
45 12
        $new->enclosedByLabel = $value;
46 12
        return $new;
47
    }
48
49
    /**
50
     * Label displayed next to the checkbox.
51
     *
52
     * It will NOT be HTML-encoded, therefore you can pass in HTML code such as an image tag. If this is coming from
53
     * end users, you should {@see encode()} it to prevent XSS attacks.
54
     *
55
     * When this option is specified, the checkbox will be enclosed by a label tag.
56
     *
57
     * @param string $value
58
     *
59
     * @return static
60
     *
61
     * @link https://www.w3.org/TR/html52/sec-forms.html#the-label-element
62
     */
63 3
    public function label(string $value): self
64
    {
65 3
        $new = clone $this;
66 3
        $new->label = $value;
67 3
        return $new;
68
    }
69
70
    /**
71
     * HTML attributes for the label tag.
72
     *
73
     * Do not set this option unless you set the "label" attributes.
74
     *
75
     * @param array $attributes
76
     *
77
     * @return static
78
     *
79
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
80
     */
81 3
    public function labelAttributes(array $attributes = []): self
82
    {
83 3
        $new = clone $this;
84 3
        $new->labelAttributes = $attributes;
85 3
        return $new;
86
    }
87
88
    /**
89
     * @param bool|float|int|string|Stringable|null $value Value that corresponds to "unchecked" state of the input.
90
     *
91
     * @return static
92
     */
93 5
    public function uncheckValue($value): self
94
    {
95 5
        $new = clone $this;
96 5
        $new->uncheckValue = is_bool($value) ? (int) $value : $value;
97 5
        return $new;
98
    }
99
100
    /**
101
     * @return string the generated checkbox tag.
102
     */
103 18
    protected function run(): string
104
    {
105 18
        $new = clone $this;
106
107 18
        $checkbox = CheckboxTag::tag();
108 18
        $value = HtmlForm::getAttributeValue($new->getFormModel(), $new->attribute);
109
110 18
        if (is_iterable($value) || is_object($value)) {
111 2
            throw new InvalidArgumentException('Checkbox widget value can not be an iterable or an object.');
112
        }
113
114
        /** @var scalar|Stringable|null */
115 16
        $valueDefault = array_key_exists('value', $new->attributes) ? $new->attributes['value'] : $new->value;
116 16
        $valueDefault = is_bool($valueDefault) ? (int) $valueDefault : $valueDefault;
117
118 16
        if ($new->enclosedByLabel === true) {
119 14
            $label = $new->label !== '' ?
120 14
                $new->label : HtmlForm::getAttributeLabel($new->getFormModel(), $new->attribute);
121 14
            $checkbox = $checkbox->label($label, $new->labelAttributes);
122
        }
123
124
        return $checkbox
125 16
            ->checked("$value" === "$valueDefault")
126 16
            ->attributes($new->attributes)
127 16
            ->id($new->getId())
128 16
            ->name(HtmlForm::getInputName($new->getFormModel(), $new->attribute))
129 16
            ->uncheckValue($new->uncheckValue)
130 16
            ->value($valueDefault)
131 16
            ->render();
132
    }
133
}
134