Passed
Pull Request — master (#160)
by Wilmer
10:36
created

DateTimeLocal::readonly()   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 0
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 Yiisoft\Form\Helper\HtmlForm;
9
use Yiisoft\Form\Widget\Attribute\GlobalAttributes;
10
use Yiisoft\Html\Tag\Input;
11
12
/*
13
 * The input element with a type attribute whose value is "datetime-local" represents a control for setting the
14
 * element’s value to a string representing a local date and time (with no timezone information).
15
 *
16
 * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.datetime-local.html#input.datetime-local
17
 */
18
final class DateTimeLocal extends AbstractWidget
19
{
20
    use GlobalAttributes;
21
22
    /**
23
     * The latest acceptable date.
24
     *
25
     * @param string|null $value
26
     *
27
     * @return static
28
     *
29
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.datetime-local.html#input.datetime-local.attrs.max
30
     */
31 3
    public function max(?string $value): self
32
    {
33 3
        $new = clone $this;
34 3
        $new->attributes['max'] = $value;
35 3
        return $new;
36
    }
37
38
    /**
39
     * The earliest acceptable date.
40
     *
41
     * @param string|null $value
42
     *
43
     * @return static
44
     *
45
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.datetime-local.html#input.datetime-local.attrs.min
46
     */
47 3
    public function min(?string $value): self
48
    {
49 3
        $new = clone $this;
50 3
        $new->attributes['min'] = $value;
51 3
        return $new;
52
    }
53
54
    /**
55
     * The readonly attribute is a boolean attribute that controls whether the user can edit the form control.
56
     * When specified, the element is not mutable.
57
     *
58
     * @return static
59
     *
60
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.datetime-local.html#input.datetime-local.attrs.readonly
61
     */
62 3
    public function readonly(): self
63
    {
64 3
        $new = clone $this;
65 3
        $new->attributes['readonly'] = true;
66 3
        return $new;
67
    }
68
69
    /**
70
     * Generates a datepicker tag together with a label for the given form attribute.
71
     *
72
     * @return string the generated checkbox tag.
73
     */
74 18
    protected function run(): string
75
    {
76 18
        $new = clone $this;
77
78
        /** @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.datetime-local.html#input.datetime-local.attrs.value */
79 18
        $value = HtmlForm::getAttributeValue($new->getFormModel(), $new->getAttribute());
80
81 18
        if (!is_string($value) && null !== $value) {
82 3
            throw new InvalidArgumentException('DateTimeLocal widget requires a string or null value.');
83
        }
84
85 15
        return Input::tag()
86 15
            ->type('datetime-local')
87 15
            ->attributes($new->attributes)
88 15
            ->id($new->getId())
89 15
            ->name($new->getName())
90 15
            ->value($value === '' ? null : $value)
91 15
            ->render();
92
    }
93
}
94