Range::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
namespace kalanis\kw_table\form_nette\Controls;
4
5
6
use Nette\Forms\Container;
7
use Nette\Forms\Controls\TextInput;
8
use Nette\Utils\Html;
9
use Nette\Utils\DateTime;
10
11
12
/**
13
 * Class Range
14
 * @package kalanis\kw_table\form_nette\Controls
15
 * Set ranges
16
 */
17
class Range extends TextInput
18
{
19
    protected string $inputFormat = 'd.m.Y H:i:s';
20
    protected string $searchFormat = 'Y-m-d H:i:s';
21
22
    protected ?Html $start;
23
    protected ?Html $end;
24
    protected ?DateTime $startValue;
25
    protected ?DateTime $endValue;
26
27
    private $started = false;
28
29
    public function __construct(string $name, $label = null, $maxLength = null)
30
    {
31
        parent::__construct($label, $maxLength);
32
33
        $this->setControlHtml($name);
34
        $this->started = true;
35
    }
36
37
    protected function setControlHtml(string $name)
38
    {
39
        $span = Html::el('span');
40
        $divSince = Html::el('div', ['class' => 'input-group dateTimePickerRange']);
41
42
        $start = $this->start = Html::el('input', [
43
            'type'        => 'text',
44
            'name'        => $name . '[]',
45
            'placeholder' => _('From'),
46
            'id'          => $name . 'StartId',
47
            'class'       => 'form-control cleanable',
48
            'aria-label'  => _('Time from')
49
        ]);
50
        $end = $this->end = Html::el('input', [
51
            'type'        => 'text',
52
            'name'        => $name . '[]',
53
            'placeholder' => _('To'),
54
            'id'          => $name . 'StartId',
55
            'class'       => 'form-control cleanable',
56
            'aria-label'  => _('Time to')
57
        ]);
58
59
        $divTo = clone $divSince;
60
        $divSince->add($start);
61
        $divTo->add($end);
62
        $span->add($divSince);
63
        $span->add($divTo);
64
        $this->control = $span;
0 ignored issues
show
Bug introduced by
The property control is declared read-only in Nette\Forms\Controls\BaseControl.
Loading history...
65
    }
66
67
    public function setInputFormat(string $format)
68
    {
69
        $this->inputFormat = $format;
70
        return $this;
71
    }
72
73
    public function setSearchFormat(string $format)
74
    {
75
        $this->searchFormat = $format;
76
        return $this;
77
    }
78
79
    public function getValue()
80
    {
81
        return [
82
            0 => $this->startValue,
83
            1 => $this->endValue
84
        ];
85
    }
86
87
    public function setValue($value)
88
    {
89
        $startValue = $this->startValue;
90
        $endValue = $this->endValue;
91
92
        if (is_array($value)) {
93
            if (isset($value[0])) {
94
                $startValue = $value[0];
95
            }
96
            if (isset($value[1])) {
97
                $endValue = $value[1];
98
            }
99
        } else {
100
            $startValue = $value;
101
        }
102
103
        $this->startValue = $startValue;
104
        $this->endValue = $endValue;
105
        if ($this->started) {
106
            $this->start->addAttributes(['value' => $startValue]);
0 ignored issues
show
Bug introduced by
The method addAttributes() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
            $this->start->/** @scrutinizer ignore-call */ 
107
                          addAttributes(['value' => $startValue]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
            $this->end->addAttributes(['value' => $endValue]);
0 ignored issues
show
Bug introduced by
The method addAttributes() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
            $this->end->/** @scrutinizer ignore-call */ 
108
                        addAttributes(['value' => $endValue]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
        }
109
    }
110
111
    public static function register(?string $inputFormat = null, ?string $searchFormat = null)
112
    {
113
        Container::extensionMethod('addRange', function ($container, $name, $label = null, $maxLength = null) use ($inputFormat, $searchFormat) {
114
            $picker = $container[$name] = new Range($name, $label, $maxLength);
115
116
            if (null !== $inputFormat)
117
                $picker->setInputFormat($inputFormat);
118
            if (null !== $searchFormat)
119
                $picker->setSearchFormat($searchFormat);
120
121
            return $picker;
122
        });
123
    }
124
}
125