|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Form\Element; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
|
|
8
|
|
|
class DateRange extends Date |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $format = 'Y-m-d'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $defaultFrom; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $defaultTo; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @return string |
|
27
|
|
|
*/ |
|
28
|
|
View Code Duplication |
public function getDefaultFrom() |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
if (! $this->defaultFrom) { |
|
31
|
|
|
$this->defaultFrom = Carbon::now(); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return $this->defaultFrom instanceof \DateTime |
|
35
|
|
|
? $this->defaultFrom->format(config('sleeping_owl.dateFormat', $this->getPickerFormat())) |
|
36
|
|
|
: $this->defaultFrom; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $defaultFrom |
|
41
|
|
|
* |
|
42
|
|
|
* @return $this |
|
43
|
|
|
*/ |
|
44
|
|
|
public function setDefaultFrom($defaultFrom) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->defaultFrom = $defaultFrom; |
|
47
|
|
|
|
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
View Code Duplication |
public function getDefaultTo() |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
if (! $this->defaultTo) { |
|
57
|
|
|
$this->defaultTo = Carbon::now(); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $this->defaultTo instanceof \DateTime |
|
61
|
|
|
? $this->defaultTo->format(config('sleeping_owl.dateFormat', $this->getPickerFormat())) |
|
62
|
|
|
: $this->defaultTo; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $defaultTo |
|
67
|
|
|
* |
|
68
|
|
|
* @return $this |
|
69
|
|
|
*/ |
|
70
|
|
|
public function setDefaultTo($defaultTo) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->defaultTo = $defaultTo; |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param Model $model |
|
79
|
|
|
* @param string $attribute |
|
80
|
|
|
* @param mixed $value |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setValue(Model $model, $attribute, $value) |
|
83
|
|
|
{ |
|
84
|
|
|
$value = ! empty($value) ? array_map(function ($date) { |
|
85
|
|
|
return Carbon::createFromFormat($this->getPickerFormat(), $date); |
|
86
|
|
|
}, explode('::', $value)) : null; |
|
87
|
|
|
|
|
88
|
|
|
$model->setAttribute($attribute, $value); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
public function toArray() |
|
95
|
|
|
{ |
|
96
|
|
|
return parent::toArray() + [ |
|
97
|
|
|
'startDate' => $this->getDefaultFrom(), |
|
98
|
|
|
'endDate' => $this->getDefaultTo(), |
|
99
|
|
|
]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $value |
|
104
|
|
|
* |
|
105
|
|
|
* @return string|void |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function parseValue($value) |
|
108
|
|
|
{ |
|
109
|
|
|
dd($value); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.