1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Object; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
use DateTimeInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
trait TimestampableTrait |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Object creation date (set automatically on save) |
17
|
|
|
* @var DateTimeInterface |
18
|
|
|
*/ |
19
|
|
|
private $created; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Object last modified date (set automatically on save and update) |
23
|
|
|
* @var DateTimeInterface |
24
|
|
|
*/ |
25
|
|
|
private $lastModified; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \DateTimeInterface|string|null $created The object's creation timestamp. |
29
|
|
|
* @throws InvalidArgumentException If the provided date/time is invalid. |
30
|
|
|
* @return self |
31
|
|
|
*/ |
32
|
|
View Code Duplication |
public function setCreated($created) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
if ($created === null) { |
35
|
|
|
$this->created = null; |
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
if (is_string($created)) { |
39
|
|
|
$created = new DateTime($created); |
40
|
|
|
} |
41
|
|
|
if (!($created instanceof DateTimeInterface)) { |
42
|
|
|
throw new InvalidArgumentException( |
43
|
|
|
'Invalid "Created" value. Must be a date/time string or a DateTime object.' |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
$this->created = $created; |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return DateTimeInterface|null |
52
|
|
|
*/ |
53
|
|
|
public function getCreated() |
54
|
|
|
{ |
55
|
|
|
return $this->created; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param \DateTimeInterface|string|null $lastModified The object's last modified timestamp. |
60
|
|
|
* @throws InvalidArgumentException If the provided date/time is invalid. |
61
|
|
|
* @return self |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function setLastModified($lastModified) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
if ($lastModified === null) { |
66
|
|
|
$this->lastModified = null; |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
if (is_string($lastModified)) { |
70
|
|
|
$lastModified = new DateTime($lastModified); |
71
|
|
|
} |
72
|
|
|
if (!($lastModified instanceof DateTimeInterface)) { |
73
|
|
|
throw new InvalidArgumentException( |
74
|
|
|
'Invalid "Last Modified" value. Must be a date/time string or a DateTime object.' |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
$this->lastModified = $lastModified; |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return DateTimeInterface|null |
83
|
|
|
*/ |
84
|
|
|
public function getLastModified() |
85
|
|
|
{ |
86
|
|
|
return $this->lastModified; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.