Completed
Pull Request — master (#16)
by Sergii
07:37
created

Native::isDateSelected()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Utils\DatePicker;
6
7
class Native extends DatePickerBase
8
{
9
    /**
10
     * @var string
11
     */
12
    private $format = '';
13
    /**
14
     * @var string
15
     */
16
    private $time = '';
17
    /**
18
     * @var string
19
     */
20
    private $formatted = '';
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function initialize()
26
    {
27
        $this->format = $this->jQuery("data('drupalDateFormat')");
28
29
        if (empty($this->format)) {
30
            throw new \RuntimeException('Unknown date format.');
31
        }
32
33
        $this->time = strtotime($this->date);
0 ignored issues
show
Documentation Bug introduced by
The property $time was declared of type string, but strtotime($this->date) is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
34
        $this->formatted = date($this->format, $this->time);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function isDateAvailable()
41
    {
42
        $ranges = [];
43
44
        foreach (['min', 'max'] as $range) {
45
            $value = $this->element->getAttribute($range);
46
            // If no range was set then use the original date as its value.
47
            $ranges[$range] = null === $value ? $this->time : strtotime($value);
48
        }
49
50
        if ($this->time < $ranges['min'] || $this->time > $ranges['max']) {
51
            throw new \Exception(sprintf('The "%s" is not available for choosing.', $this->date));
52
        }
53
54
        return $this;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function setDate()
61
    {
62
        $this->jQuery("val('$this->formatted')");
63
64
        return $this;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function isDateSelected()
71
    {
72
        $value = $this->jQuery('val()');
73
74
        self::debug(['Comparing "%s" with "%s".'], [$value, $this->formatted]);
75
76
        if ($value !== $this->formatted) {
77
            throw new \Exception(sprintf('DatePicker contains the "%s" but should "%s".', $value, $this->formatted));
78
        }
79
80
        return $this;
81
    }
82
}
83