Passed
Push — master ( ef6f5d...60f4bc )
by Alexander
02:52
created

Date   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 56
ccs 19
cts 19
cp 1
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A max() 0 5 1
A min() 0 5 1
A run() 0 17 4
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 "date" represents a control for setting the element’s value to
13
 * a string representing a date.
14
 *
15
 * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.date.html#input.date
16
 */
17
final class Date 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.date.html#input.date.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.date.html#input.date.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 31
    protected function run(): string
57
    {
58 31
        $attributes = $this->build($this->attributes);
59
60
        /** @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.date.html#input.date.attrs.value */
61 31
        $value = $attributes['value'] ?? $this->getAttributeValue();
62 31
        unset($attributes['value']);
63
64 31
        if (!is_string($value) && null !== $value) {
65 2
            throw new InvalidArgumentException('Date widget requires a string or null value.');
66
        }
67
68 29
        return Input::tag()
69 29
            ->type('date')
70 29
            ->attributes($attributes)
71 29
            ->value($value === '' ? null : $value)
72 29
            ->render();
73
    }
74
}
75