DatePicker::setValue()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_forms\Controls;
4
5
6
class DatePicker extends AControl
7
{
8
    protected string $templateInput = '<input type="text" value="%1$s"%2$s />%3$s';
9
    protected string $dateFormat = 'Y-m-d';
10
    protected string $dateClass = 'datepicker';
11
12 5
    public function set(string $alias, ?string $originalValue = null, string $label = ''): self
13
    {
14 5
        $this->setEntry($alias, $originalValue, $label);
15 5
        $this->setAttribute('class', $this->dateClass);
16 5
        $this->setAttribute('id', $this->getKey());
17 5
        return $this;
18
    }
19
20 1
    public function setDateFormat(string $dateFormat): self
21
    {
22 1
        $this->dateFormat = $dateFormat;
23 1
        return $this;
24
    }
25
26 1
    public function getDateFormat(): string
27
    {
28 1
        return $this->dateFormat;
29
    }
30
31 1
    public function setValue($value): void
32
    {
33 1
        if (is_numeric($value)) {
34 1
            $this->value = $value;
35 1
        } elseif (!empty($value)) {
36 1
            $this->value = strtotime(strval($value));
37
        }
38 1
    }
39
40 2
    public function renderInput($attributes = null): string
41
    {
42 2
        $this->addAttributes($attributes);
43 2
        if (is_numeric($this->value)) {
44 1
            $value = date($this->getDateFormat(), intval($this->value));
45
        } else {
46 2
            $value = '';
47
        }
48 2
        $this->setAttribute('name', $this->getKey());
49 2
        return $this->wrapIt(sprintf($this->templateInput, $this->escaped(strval($value)), $this->renderAttributes(), $this->renderChildren()), $this->wrappersInput);
50
    }
51
}
52