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
|
|
|
|
11
|
|
|
abstract class BaseModel extends Eloquent { |
12
|
|
|
//Override date functions to return Jenssegers Data instead of Carbon |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Get a fresh timestamp for the model. |
16
|
|
|
* |
17
|
|
|
* @return Date |
18
|
|
|
*/ |
19
|
55 |
|
public function freshTimestamp() |
20
|
|
|
{ |
21
|
55 |
|
return new Date; |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Return a timestamp as DateTime object. |
26
|
|
|
* |
27
|
|
|
* @param mixed $value |
28
|
|
|
* @return Date |
29
|
|
|
*/ |
30
|
55 |
|
protected function asDateTime($value) |
31
|
|
|
{ |
32
|
55 |
|
$timezone = Config::get('app.timezone'); |
33
|
|
|
|
34
|
|
|
// If this value is already a Carbon instance, we shall just return it as is. |
35
|
|
|
// This prevents us having to re-instantiate a Carbon instance when we know |
36
|
|
|
// it already is one, which wouldn't be fulfilled by the DateTime check. |
37
|
55 |
|
if ($value instanceof Carbon) { |
38
|
55 |
|
return Date::parse($value, $timezone); |
|
|
|
|
39
|
|
|
} |
40
|
55 |
|
if ($value instanceof Date) { |
41
|
|
|
return $value; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
// If the value is already a DateTime instance, we will just skip the rest of |
44
|
|
|
// these checks since they will be a waste of time, and hinder performance |
45
|
|
|
// when checking the field. We will just return the DateTime right away. |
46
|
55 |
|
if ($value instanceof DateTimeInterface) { |
47
|
27 |
|
return new Date( |
|
|
|
|
48
|
|
|
//$value->format('Y-m-d H:i:s.u'), $value->getTimeZone() |
49
|
27 |
|
$value->format('Y-m-d H:i:s.u'), $timezone |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
// If this value is an integer, we will assume it is a UNIX timestamp's value |
53
|
|
|
// and format a Carbon object from this timestamp. This allows flexibility |
54
|
|
|
// when defining your date fields as they might be UNIX timestamps here. |
55
|
55 |
|
if (is_numeric($value)) { |
56
|
|
|
return Date::createFromTimestamp($value, $timezone); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
// If the value is in simply year, month, day format, we will instantiate the |
59
|
|
|
// Carbon instances from that format. Again, this provides for simple date |
60
|
|
|
// fields on the database, while still supporting Carbonized conversion. |
61
|
55 |
|
if (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $value)) { |
62
|
3 |
|
return Date::createFromFormat('Y-m-d', $value, $timezone)->startOfDay(); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
// Finally, we will just assume this date is in the format used by default on |
65
|
|
|
// the database connection and use that format to create the Carbon object |
66
|
|
|
// that is returned back out to the developers after we convert it here. |
67
|
55 |
|
return Date::createFromFormat($this->getDateFormat(), $value, $timezone); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.