DateRange   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
eloc 75
c 1
b 0
f 0
dl 0
loc 124
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setSearchFormat() 0 4 1
A __construct() 0 5 1
A setInputFormat() 0 4 1
A getValue() 0 18 3
B setValue() 0 28 11
A setControlHtml() 0 28 1
A register() 0 11 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 DateRange
14
 * @package kalanis\kw_table\form_nette\Controls
15
 * Set ranges of dates
16
 */
17
class DateRange 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
    protected $started = false;
28
29
    public function __construct(string $name, $label = null, $maxLength = null)
30
    {
31
        parent::__construct($label, $maxLength);
32
        $this->setControlHtml($name);
33
        $this->started = true;
34
    }
35
36
    protected function setControlHtml(string $name)
37
    {
38
        $span = Html::el('span');
39
        $divSince = Html::el('div', ['class' => 'input-group dateTimePickerRange']);
40
        $divTo = clone $divSince;
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 listingDateTimePicker',
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 listingDateTimePicker',
56
            'aria-label'  => _('Time to')
57
        ]);
58
59
        $divSince->add($start);
60
        $divTo->add($end);
61
        $span->add($divSince);
62
        $span->add($divTo);
63
        $this->control = $span;
0 ignored issues
show
Bug introduced by
The property control is declared read-only in Nette\Forms\Controls\BaseControl.
Loading history...
64
    }
65
66
    public function setInputFormat(string $format)
67
    {
68
        $this->inputFormat = $format;
69
        return $this;
70
    }
71
72
    public function setSearchFormat(string $format)
73
    {
74
        $this->searchFormat = $format;
75
        return $this;
76
    }
77
78
    public function getValue()
79
    {
80
        if (empty($this->startValue)) {
81
            $startValue = new \DateTime();
82
            $startValue->setDate(1, 1, 1);
83
        } else {
84
            $startValue = $this->startValue;
85
        };
86
        if (empty($this->endValue)) {
87
            $endValue = new \DateTime();
88
            $endValue->modify('+1000year');
89
        } else {
90
            $endValue = $this->endValue;
91
        }
92
93
        return [
94
            0 => $startValue->format($this->searchFormat),
95
            1 => $endValue->format($this->searchFormat)
96
        ];
97
    }
98
99
    public function setValue($value)
100
    {
101
        $startValue = $this->startValue;
102
        $endValue = $this->endValue;
103
104
        if (is_array($value)) {
105
            if (isset($value[0])) {
106
                $startValue = $value[0];
107
            }
108
            if (isset($value[1])) {
109
                $endValue = $value[1];
110
            }
111
        } else {
112
            $startValue = $value;
113
        }
114
115
        if (!empty($startValue) && !($startValue instanceof \DateTime)) {
116
            $startValue = new \DateTime($startValue);
117
        }
118
        if (!empty($endValue) && !($endValue instanceof \DateTime)) {
119
            $endValue = new \DateTime($endValue);
120
        }
121
122
        $this->startValue = $startValue;
123
        $this->endValue = $endValue;
124
        if ($this->started) {
125
            $this->start->addAttributes(['value' => (!empty($startValue) ? $startValue->format($this->inputFormat) : null)]);
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

125
            $this->start->/** @scrutinizer ignore-call */ 
126
                          addAttributes(['value' => (!empty($startValue) ? $startValue->format($this->inputFormat) : null)]);

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...
126
            $this->end->addAttributes(['value' => (!empty($endValue) ? $endValue->format($this->inputFormat) : null)]);
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

126
            $this->end->/** @scrutinizer ignore-call */ 
127
                        addAttributes(['value' => (!empty($endValue) ? $endValue->format($this->inputFormat) : null)]);

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...
127
        }
128
    }
129
130
    public static function register(?string $inputFormat = null, ?string $searchFormat = null)
131
    {
132
        Container::extensionMethod('addTbDateRange', function ($container, $name, $label = null, $maxLength = null) use ($inputFormat, $searchFormat) {
133
            $picker = $container[$name] = new DateRange($name, $label, $maxLength);
134
135
            if (null !== $inputFormat)
136
                $picker->setInputFormat($inputFormat);
137
            if (null !== $searchFormat)
138
                $picker->setSearchFormat($searchFormat);
139
140
            return $picker;
141
        });
142
    }
143
}
144