|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace miBadger\ActiveRecord\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use miBadger\Query\Query; |
|
6
|
|
|
use miBadger\ActiveRecord\ColumnProperty; |
|
7
|
|
|
|
|
8
|
|
|
const TRAIT_DATEFIELDS_LAST_MODIFIED = "last_modified"; |
|
9
|
|
|
const TRAIT_DATEFIELDS_CREATED = "created"; |
|
10
|
|
|
|
|
11
|
|
|
trait Datefields |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var string The timestamp representing the moment this record was created */ |
|
14
|
|
|
protected $created; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string The timestamp representing the moment this record was last updated */ |
|
17
|
|
|
protected $lastModified; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* this method is required to be called in the constructor for each class that uses this trait. |
|
21
|
|
|
* It adds the datefields to the table definition and registers the callback hooks |
|
22
|
|
|
*/ |
|
23
|
3 |
|
protected function initDatefields() |
|
24
|
|
|
{ |
|
25
|
3 |
|
$this->extendTableDefinition(TRAIT_DATEFIELDS_CREATED, [ |
|
|
|
|
|
|
26
|
3 |
|
'value' => &$this->created, |
|
27
|
|
|
'validate' => null, |
|
28
|
3 |
|
'type' => 'DATETIME', |
|
29
|
3 |
|
'default' => 'CURRENT_TIMESTAMP', |
|
30
|
3 |
|
'properties' => ColumnProperty::NOT_NULL | ColumnProperty::IMMUTABLE |
|
31
|
|
|
]); |
|
32
|
|
|
|
|
33
|
3 |
|
$this->extendTableDefinition(TRAIT_DATEFIELDS_LAST_MODIFIED, [ |
|
|
|
|
|
|
34
|
3 |
|
'value' => &$this->lastModified, |
|
35
|
|
|
'validate' => null, |
|
36
|
3 |
|
'type' => 'DATETIME', |
|
37
|
3 |
|
'default' => 'CURRENT_TIMESTAMP', |
|
38
|
3 |
|
'properties' => ColumnProperty::NOT_NULL | ColumnProperty::IMMUTABLE |
|
39
|
|
|
]); |
|
40
|
|
|
|
|
41
|
3 |
|
$this->registerUpdateHook(TRAIT_DATEFIELDS_LAST_MODIFIED, 'DatefieldsUpdateHook'); |
|
|
|
|
|
|
42
|
3 |
|
$this->registerCreateHook(TRAIT_DATEFIELDS_LAST_MODIFIED, 'DatefieldsCreateHook'); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
3 |
|
$this->created = null; |
|
45
|
3 |
|
$this->lastModified = null; |
|
46
|
3 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The hook that gets called to set the timestamp whenever a new record is created |
|
50
|
|
|
*/ |
|
51
|
2 |
|
protected function DatefieldsCreateHook() |
|
52
|
|
|
{ |
|
53
|
|
|
// @TODO: Should this be split up to seperate hooks for "last_modified" and "created" for consistency? |
|
54
|
2 |
|
$this->created = (new \DateTime('now'))->format('Y-m-d H:i:s'); |
|
55
|
2 |
|
$this->lastModified = (new \DateTime('now'))->format('Y-m-d H:i:s'); |
|
56
|
2 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* The hook that gets called to set the timestamp whenever a record gets updated |
|
60
|
|
|
*/ |
|
61
|
1 |
|
protected function DatefieldsUpdateHook() |
|
62
|
|
|
{ |
|
63
|
1 |
|
$this->lastModified = (new \DateTime('now'))->format('Y-m-d H:i:s'); |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns the timestamp of last update for this record |
|
68
|
|
|
* @return DateTime |
|
|
|
|
|
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function getLastModifiedDate() |
|
71
|
|
|
{ |
|
72
|
1 |
|
return new \DateTime($this->lastModified); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Returns the timestamp of when this record was created |
|
77
|
|
|
* @return DateTime |
|
|
|
|
|
|
78
|
|
|
*/ |
|
79
|
2 |
|
public function getCreationDate() |
|
80
|
|
|
{ |
|
81
|
2 |
|
return new \DateTime($this->created); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.