Date   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 16
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 157
ccs 0
cts 74
cp 0
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormat() 0 4 1
A convertPickerFormat() 0 11 1
A getPickerFormat() 0 4 1
A setPickerFormat() 0 6 1
A getTimezone() 0 8 2
A setTimezone() 0 6 1
A isEditable() 0 4 1
A enableEdit() 0 6 1
A toArray() 0 8 1
A asDateTime() 0 19 4
A fromDateTime() 0 6 2
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
use Carbon\Carbon;
6
7
/**
8
 * Form Element Date
9
 *
10
 * @package Sco\Admin\Form\Elements
11
 * @see http://element.eleme.io/#/en-US/component/date-picker
12
 */
13
class Date extends Input
14
{
15
    protected $type = 'date';
16
17
    protected $cast = 'date';
18
19
    /**
20
     * Date Picker format
21
     *
22
     * @var string
23
     */
24
    protected $pickerFormat = 'yyyy-MM-dd';
25
26
    /**
27
     * Datetime timezone.
28
     *
29
     * @var string
30
     */
31
    protected $timezone;
32
33
    protected $editable = false;
34
35
    /**
36
     * @return string
37
     */
38
    public function getFormat()
39
    {
40
        return $this->convertPickerFormat();
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    protected function convertPickerFormat()
47
    {
48
        return strtr($this->getPickerFormat(), [
49
            'yyyy' => 'Y',
50
            'MM'   => 'm',
51
            'dd'   => 'd',
52
            'HH'   => 'H',
53
            'mm'   => 'i',
54
            'ss'   => 's',
55
        ]);
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    protected function getPickerFormat()
62
    {
63
        return $this->pickerFormat;
64
    }
65
66
    /**
67
     * @param string $value
68
     *
69
     * @return $this
70
     */
71
    public function setPickerFormat(string $value)
72
    {
73
        $this->pickerFormat = $value;
74
75
        return $this;
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    protected function getTimezone()
82
    {
83
        if ($this->timezone) {
84
            return $this->timezone;
85
        }
86
87
        return config('app.timezone');
88
    }
89
90
    /**
91
     * @param string $timezone
92
     *
93
     * @return $this
94
     */
95
    public function setTimezone(string $timezone)
96
    {
97
        $this->timezone = $timezone;
98
99
        return $this;
100
    }
101
102
    /**
103
     * @return bool
104
     */
105
    protected function isEditable()
106
    {
107
        return $this->editable;
108
    }
109
110
    /**
111
     * Allow edit
112
     *
113
     * @return $this
114
     */
115
    public function enableEdit()
116
    {
117
        $this->editable = true;
118
119
        return $this;
120
    }
121
122
    public function toArray()
123
    {
124
        return parent::toArray() + [
125
                //'format' => $this->getFormat(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
126
                'pickerFormat' => $this->getPickerFormat(),
127
                'editable'     => $this->isEditable(),
128
            ];
129
    }
130
131
    /**
132
     * Return a timestamp as DateTime object.
133
     *
134
     * @param  mixed $value
135
     * @return Carbon
136
     */
137
    protected function asDateTime($value)
138
    {
139
        if ($value instanceof Carbon) {
140
            return $value->timezone($this->getTimezone());
141
        }
142
143
        if ($value instanceof \DateTimeInterface) {
144
            return new Carbon(
145
                $value->format('Y-m-d H:i:s.u'),
146
                $value->getTimezone()
147
            );
148
        }
149
150
        if (is_numeric($value)) {
151
            return Carbon::createFromTimestamp($value)->timezone($this->getTimezone());
152
        }
153
154
        return Carbon::createFromFormat($this->getFormat(), $value);
155
    }
156
157
    /**
158
     * Convert a DateTime to a storable string.
159
     *
160
     * @param  \DateTime|int $value
161
     * @return string
162
     */
163
    public function fromDateTime($value)
164
    {
165
        return empty($value) ? $value : $this->asDateTime($value)
166
            ->timezone($this->getTimezone())
167
            ->format($this->getFormat());
168
    }
169
}
170