Completed
Push — master ( 385f57...dd4a08 )
by wen
03:01
created

Date::getTimezone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
use Carbon\Carbon;
6
7
class Date extends Input
8
{
9
    protected $type = 'date';
10
11
    /**
12
     * Date Picker format
13
     *
14
     * @var string
15
     */
16
    protected $pickerFormat = 'yyyy-MM-dd';
17
18
    /**
19
     * Datetime timezone.
20
     *
21
     * @var string
22
     */
23
    protected $timezone;
24
25
    protected $editable = false;
26
27
    public function getFormat()
28
    {
29
        return $this->convertPickerFormat();
30
    }
31
32
    /*public function setFormat($value)
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% 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...
33
    {
34
        $this->format = $value;
35
36
        return $this;
37
    }*/
38
39
    protected function convertPickerFormat()
40
    {
41
        return strtr($this->getPickerFormat(), [
42
            'yyyy' => 'Y',
43
            //'yy'   => 'y',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
44
            'MM'   => 'm',
45
            //'M'    => 'n',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
46
            'dd'   => 'd',
47
            //'d'    => 'j',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
48
            'HH'   => 'H',
49
            //'H'    => 'G',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
50
            'mm'   => 'i',
51
            'ss'   => 's',
52
        ]);
53
    }
54
55
    protected function getPickerFormat()
56
    {
57
        return $this->pickerFormat;
58
    }
59
60
    /**
61
     * @param string $value
62
     *
63
     * @return $this
64
     */
65
    public function setPickerFormat($value)
66
    {
67
        $this->pickerFormat = $value;
68
69
        return $this;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    protected function getTimezone()
76
    {
77
        if ($this->timezone) {
78
            return $this->timezone;
79
        }
80
81
        return config('app.timezone');
82
    }
83
84
    /**
85
     * @param string $timezone
86
     *
87
     * @return $this
88
     */
89
    public function setTimezone($timezone)
90
    {
91
        $this->timezone = $timezone;
92
93
        return $this;
94
    }
95
96
    protected function isEditable()
97
    {
98
        return $this->editable;
99
    }
100
101
    /**
102
     * Allow edit
103
     *
104
     * @return $this
105
     */
106
    public function enableEdit()
107
    {
108
        $this->editable = true;
109
110
        return $this;
111
    }
112
113
    public function toArray()
114
    {
115
        return parent::toArray() + [
116
                //'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...
117
                'pickerFormat' => $this->getPickerFormat(),
118
                'editable' => $this->isEditable(),
119
            ];
120
    }
121
122
    protected function getDefaultValue()
123
    {
124
        return Carbon::create()
125
            ->timezone($this->getTimezone())
126
            ->format($this->getFormat());
127
    }
128
129
    public function getValue()
130
    {
131
        $value = parent::getValue();
132
        if (empty($value)) {
133
            return '';
134
        }
135
        if (!($value instanceof Carbon)) {
136
            $value = Carbon::parse($value);
137
        }
138
139
        $value->timezone($this->getTimezone());
140
141
        return $value->format($this->getFormat());
142
    }
143
144
    protected function prepareValue($value)
145
    {
146
        return strtotime($value);
147
    }
148
}
149