1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model as Eloquent; |
6
|
|
|
use Illuminate\Support\Facades\Config; |
7
|
|
|
use Carbon\Carbon; |
8
|
|
|
use DateTimeInterface; |
9
|
|
|
use Jenssegers\Date\Date; |
10
|
|
|
use DateTime; |
11
|
|
|
|
12
|
|
|
abstract class BaseModel extends Eloquent |
13
|
|
|
{ |
14
|
|
|
// Override date functions to return Jenssegers Data instead of Carbon |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Get a fresh timestamp for the model. |
18
|
|
|
* |
19
|
|
|
* @return Date |
20
|
|
|
*/ |
21
|
|
|
public function freshTimestamp() |
22
|
|
|
{ |
23
|
|
|
return new Date; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Return a timestamp as DateTime object. |
28
|
|
|
* |
29
|
|
|
* @param mixed $value |
30
|
|
|
* @return Date |
31
|
|
|
*/ |
32
|
|
|
protected function asDateTime($value) |
33
|
|
|
{ |
34
|
|
|
$timezone = Config::get('app.timezone'); |
35
|
|
|
|
36
|
|
|
// If this value is already a Carbon instance, we shall just return it as is. |
37
|
|
|
// This prevents us having to re-instantiate a Carbon instance when we know |
38
|
|
|
// it already is one, which wouldn't be fulfilled by the DateTime check. |
39
|
|
|
if ($value instanceof Carbon) { |
40
|
|
|
return Date::parse($value, $timezone); |
41
|
|
|
} |
42
|
|
|
if ($value instanceof Date) { |
43
|
|
|
return $value; |
44
|
|
|
} |
45
|
|
|
// If the value is already a DateTime instance, we will just skip the rest of |
46
|
|
|
// these checks since they will be a waste of time, and hinder performance |
47
|
|
|
// when checking the field. We will just return the DateTime right away. |
48
|
|
|
if ($value instanceof DateTimeInterface) { |
49
|
|
|
return new Date( |
50
|
|
|
// $value->format('Y-m-d H:i:s.u'), $value->getTimeZone() |
51
|
|
|
$value->format('Y-m-d H:i:s.u'), |
52
|
|
|
$timezone |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
// If this value is an integer, we will assume it is a UNIX timestamp's value |
56
|
|
|
// and format a Carbon object from this timestamp. This allows flexibility |
57
|
|
|
// when defining your date fields as they might be UNIX timestamps here. |
58
|
|
|
if (is_numeric($value)) { |
59
|
|
|
return Date::createFromTimestamp($value, $timezone); |
60
|
|
|
} |
61
|
|
|
// If the value is in simply year, month, day format, we will instantiate the |
62
|
|
|
// Carbon instances from that format. Again, this provides for simple date |
63
|
|
|
// fields on the database, while still supporting Carbonized conversion. |
64
|
|
|
if (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $value)) { |
65
|
|
|
return Date::createFromFormat('Y-m-d', $value, $timezone)->startOfDay(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// If the date follows the api configured date format, use that. |
69
|
|
|
$apiFormat = Config::get('app.api_datetime_format'); |
70
|
|
|
$date = DateTime::createFromFormat($apiFormat, $value); |
71
|
|
|
if ($date && $date->format($apiFormat) == $value) { |
72
|
|
|
return $date; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Finally, we will just assume this date is in the format used by default on |
76
|
|
|
// the database connection and use that format to create the Carbon object |
77
|
|
|
// that is returned back out to the developers after we convert it here. |
78
|
|
|
return Date::createFromFormat($this->getDateFormat(), $value, $timezone); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* // Ensure that models serialized using toArray() or toJson() use the api-specific date format |
83
|
|
|
* |
84
|
|
|
* @param DateTimeInterface $date |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
protected function serializeDate(DateTimeInterface $date) |
88
|
|
|
{ |
89
|
|
|
return $date->format(Config::get('app.api_datetime_format')); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|