Passed
Pull Request — master (#160)
by Wilmer
03:13
created

DateTime::min()   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 Yiisoft\Form\Widget\Attribute\InputAttributes;
9
use Yiisoft\Html\Tag\Input;
10
11
/**
12
 * The input element with a type attribute whose value is "datetime" represents a control for setting the element’s
13
 * value to a string representing a global date and time (with timezone information).
14
 *
15
 * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.datetime.html#input.datetime
16
 */
17
final class DateTime extends InputAttributes
18
{
19
    /**
20
     * The latest acceptable date.
21
     *
22
     * @param string|null $value
23
     *
24
     * @return static
25
     *
26
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.datetime.html#input.datetime.attrs.max
27
     */
28 3
    public function max(?string $value): self
29
    {
30 3
        $new = clone $this;
31 3
        $new->attributes['max'] = $value;
32 3
        return $new;
33
    }
34
35
    /**
36
     * The earliest acceptable date.
37
     *
38
     * @param string|null $value
39
     *
40
     * @return static
41
     *
42
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.datetime.html#input.datetime.attrs.min
43
     */
44 3
    public function min(?string $value): self
45
    {
46 3
        $new = clone $this;
47 3
        $new->attributes['min'] = $value;
48 3
        return $new;
49
    }
50
51
    /**
52
     * Generates a datepicker tag together with a label for the given form attribute.
53
     *
54
     * @return string the generated checkbox tag.
55
     */
56 32
    protected function run(): string
57
    {
58 32
        $attributes = $this->build($this->attributes);
59
60
        /** @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.datetime.html#input.datetime.attrs.value */
61 32
        $value = $attributes['value'] ?? $this->getAttributeValue();
62 32
        unset($attributes['value']);
63
64 32
        if (!is_string($value) && null !== $value) {
65 2
            throw new InvalidArgumentException('DateTime widget requires a string or null value.');
66
        }
67
68 30
        return Input::tag()
69 30
            ->type('datetime')
70 30
            ->attributes($attributes)
71 30
            ->value($value === '' ? null : $value)
72 30
            ->render();
73
    }
74
}
75