DatePicker   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 44
ccs 23
cts 23
cp 1
rs 10
c 1
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setValue() 0 6 3
A set() 0 6 1
A setDateFormat() 0 4 1
A getDateFormat() 0 3 1
A renderInput() 0 10 2
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