1 | <?php |
||
2 | |||
3 | namespace SleepingOwl\Admin\Display\Column; |
||
4 | |||
5 | use Carbon\Carbon; |
||
6 | use Illuminate\Database\Eloquent\Model; |
||
7 | use SleepingOwl\Admin\Traits\DateFormat; |
||
8 | |||
9 | class DateTime extends NamedColumn |
||
10 | { |
||
11 | use DateFormat; |
||
12 | /** |
||
13 | * Datetime format. |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $format; |
||
17 | |||
18 | /** |
||
19 | * Datetime timezone. |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $timezone; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $view = 'column.datetime'; |
||
28 | |||
29 | /** |
||
30 | * @param Model $model |
||
31 | * |
||
32 | * @return $this |
||
33 | */ |
||
34 | public function setModel(Model $model) |
||
35 | { |
||
36 | parent::setModel($model); |
||
37 | $this->setHtmlAttribute('data-value', $this->getModelValue()); |
||
38 | |||
39 | return $this; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @return array |
||
44 | */ |
||
45 | public function toArray() |
||
46 | { |
||
47 | $value = $this->getModelValue(); |
||
48 | |||
49 | return parent::toArray() + [ |
||
50 | 'value' => $this->getFormatedDate($value), |
||
51 | 'originalValue' => $value, |
||
52 | ]; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param string $date |
||
57 | * |
||
58 | * @return null|string |
||
59 | */ |
||
60 | protected function getFormatedDate($date) |
||
61 | { |
||
62 | if (! is_null($date)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
63 | if (! ($date instanceof Carbon)) { |
||
0 ignored issues
–
show
|
|||
64 | $date = Carbon::parse($date); |
||
65 | } |
||
66 | |||
67 | $date = $date->timezone($this->getTimezone())->format($this->getFormat()); |
||
68 | } |
||
69 | |||
70 | return $date; |
||
71 | } |
||
72 | } |
||
73 |