DateTimeField::getOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
namespace rtens\domin\delivery\web\fields;
3
4
use rtens\domin\Parameter;
5
use rtens\domin\delivery\web\Element;
6
use rtens\domin\delivery\web\HeadElements;
7
use rtens\domin\delivery\web\WebField;
8
use watoki\reflect\type\ClassType;
9
10
class DateTimeField implements WebField {
11
12
    /**
13
     * @param Parameter $parameter
14
     * @return bool
15
     */
16
    public function handles(Parameter $parameter) {
17
        $type = $parameter->getType();
18
        return
19
            $type instanceof ClassType
20
            && (new \ReflectionClass($type->getClass()))->implementsInterface(\DateTimeInterface::class);
21
    }
22
23
    /**
24
     * @param Parameter $parameter
25
     * @param string $serialized
26
     * @return \DateTime|\DateTimeImmutable|null
27
     */
28
    public function inflate(Parameter $parameter, $serialized) {
29
        /** @var ClassType $type */
30
        $type = $parameter->getType();
31
        $class = $type->getClass();
32
33
        return $serialized ? new $class($serialized) : null;
34
    }
35
36
    /**
37
     * @param Parameter $parameter
38
     * @param null|\DateTimeInterface $value
39
     * @return string
40
     */
41
    public function render(Parameter $parameter, $value) {
42
        return (string)new Element('div', [
43
            'class' => 'input-group date datetimepicker',
44
            'style' => 'width: 100%;'
45
        ], [
46
            new Element('span', [
47
                'class' => 'input-group-addon',
48
                'onclick' => "$(this).closest('.datetimepicker').datetimepicker(dateTimePickerSettings); $(this).siblings('.hidden').toggleClass('hidden'); $(this).remove(); return false;"
49
            ], [
50
                new Element('span', ['class' => 'glyphicon glyphicon-calendar', 'style' => 'opacity: 0.5'])
51
            ]),
52
            new Element('span', ['class' => 'input-group-addon hidden'], [
53
                new Element('span', ['class' => 'glyphicon glyphicon-calendar'])
54
            ]),
55
            new Element('input', array_merge([
56
                'type' => 'text',
57
                'name' => $parameter->getName(),
58
                'class' => 'form-control',
59
                'value' => $value ? $this->serialize($value) : null
60
            ], $parameter->isRequired() ? [
61
                'required' => 'required'
62
            ] : []))
63
        ]);
64
    }
65
66
    /**
67
     * @param Parameter $parameter
68
     * @return array|Element[]
69
     */
70
    public function headElements(Parameter $parameter) {
71
        return [
72
            HeadElements::jquery(),
73
            HeadElements::bootstrap(),
74
            HeadElements::bootstrapJs(),
75
            HeadElements::script('//cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js'),
76
            HeadElements::script('//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.14.30/js/bootstrap-datetimepicker.min.js'),
77
            HeadElements::style('//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.14.30/css/bootstrap-datetimepicker.min.css'),
78
            new Element('script', [], [
79
                'var dateTimePickerSettings = ' . json_encode($this->getOptions()) . ';'
80
            ])
81
        ];
82
    }
83
84
    /**
85
     * @return array Of options as documented on https://eonasdan.github.io/bootstrap-datetimepicker/Options/
86
     */
87
    protected function getOptions() {
88
        return [
89
            'format' => 'dddd, D MMMM YYYY, HH:mm:ss',
90
            'extraFormats' => ['YYYY-MM-DD HH:mm:ss'],
91
            'showTodayButton' => true,
92
            'showClear' => true
93
        ];
94
    }
95
96
    /**
97
     * @param $dateTime
98
     * @return string
99
     */
100
    protected function serialize(\DateTimeInterface $dateTime) {
101
        return $dateTime->format('Y-m-d H:i:s');
102
    }
103
}