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