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

DatePickerBase::isDateSelected()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Utils\DatePicker;
6
7
// Contexts.
8
use Drupal\TqExtension\Context\RawTqContext;
9
// Helpers.
10
use Behat\DebugExtension\Debugger;
11
use Behat\Mink\Element\NodeElement;
12
13
abstract class DatePickerBase
14
{
15
    use Debugger;
16
17
    /**
18
     * @var RawTqContext
19
     */
20
    protected $context;
21
    /**
22
     * @var NodeElement
23
     */
24
    protected $element;
25
    /**
26
     * @var string
27
     */
28
    protected $date = '';
29
30
    /**
31
     * @param RawTqContext $context
32
     *   Behat context.
33
     * @param string $selector
34
     *   Element selector.
35
     * @param string $date
36
     *   Human-readable date.
37
     */
38
    public function __construct(RawTqContext $context, $selector, $date)
39
    {
40
        $this->context = $context;
41
        $this->element = $this->context->element('*', $selector);
42
        $this->date = self::jsDate($date);
43
    }
44
45
    /**
46
     * @param string $javascript
47
     *   JS code for execution.
48
     *
49
     * @return mixed
50
     *   Result of JS execution.
51
     */
52
    protected function execute($javascript)
53
    {
54
        return $this->context->executeJs("return $javascript;");
55
    }
56
57
    /**
58
     * @param string $date
59
     *   The string to parse.
60
     *
61
     * @return string
62
     */
63
    protected static function jsDate($date)
64
    {
65
        return sprintf("new Date('%s')", date('c', strtotime($date)));
66
    }
67
68
    /**
69
     * @throws \Exception
70
     *   When date is not available for selection.
71
     *
72
     * @return static
73
     */
74
    abstract public function isDateAvailable();
75
76
    /**
77
     * @return static
78
     */
79
    abstract public function setDate();
80
81
    /**
82
     * @throws \Exception
83
     *   When date is not selected.
84
     *
85
     * @return static
86
     */
87
    abstract public function isDateSelected();
88
}
89