1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gearbox\DateTime; |
4
|
|
|
|
5
|
|
|
use DateTimeImmutable as PhpDateTimeImmutable; |
6
|
|
|
use DateTimeZone; |
7
|
|
|
|
8
|
|
|
class DateImmutable |
9
|
|
|
{ |
10
|
|
|
/** @var PhpDateTimeImmutable */ |
11
|
|
|
private $phpDateTime; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* May be called optionally with a date string like '2019-10-28' |
16
|
|
|
* |
17
|
|
|
* @throws Exception |
18
|
|
|
*/ |
19
|
|
|
public function __construct(string $dateString = null, DateTimeZone $timezone = null) |
20
|
|
|
{ |
21
|
|
|
// Default is today |
22
|
|
|
if (is_null($dateString)) { |
23
|
|
|
$dateString = date('Y-m-d'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
try { |
27
|
|
|
$this->phpDateTime = new PhpDateTimeImmutable($dateString . ' 12:00:00', $timezone); |
28
|
|
|
} catch (\Exception $exception) { |
29
|
|
|
throw new Exception('Cannot construct instance of ' . __CLASS__ . ' with parameter "' . $dateString . '". Problem: ' . $exception->getMessage()); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
|
35
|
|
|
public function isSameDay(DateImmutable $date): bool |
36
|
|
|
{ |
37
|
|
|
return $date->getDayAsNumber() == $this->getDayAsNumber(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
|
42
|
|
|
public function isSameMonth(DateImmutable $date): bool |
43
|
|
|
{ |
44
|
|
|
return $date->getMonthAsNumber() == $this->getMonthAsNumber(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
|
49
|
|
|
public function isSameYear(DateImmutable $date): bool |
50
|
|
|
{ |
51
|
|
|
return $date->getYearAsNumber() == $this->getYearAsNumber(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
|
56
|
|
|
public function equals(DateImmutable $date): bool |
57
|
|
|
{ |
58
|
|
|
return $this->isSameDay($date) && $this->isSameMonth($date) && $this->isSameYear($date); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
|
63
|
|
|
public function isLastDayOfMonth(): bool |
64
|
|
|
{ |
65
|
|
|
$month = new MonthImmutable($this->phpDateTime->format('Y-m')); |
66
|
|
|
|
67
|
|
|
return $month->getLastDay()->equals($this); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
|
72
|
|
|
public function getDayAsNumber(): int |
73
|
|
|
{ |
74
|
|
|
return (int) $this->phpDateTime->format('d'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
|
79
|
|
|
public function getMonthAsNumber(): int |
80
|
|
|
{ |
81
|
|
|
return (int) $this->phpDateTime->format('m'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
|
86
|
|
|
public function getYearAsNumber(): int |
87
|
|
|
{ |
88
|
|
|
return (int) $this->phpDateTime->format('Y'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
|
93
|
|
|
public function __clone() |
94
|
|
|
{ |
95
|
|
|
// Force a clone of phpDateTime, otherwise the cloned DateTime object |
96
|
|
|
// would point to the same phpDateTime instance. |
97
|
|
|
$this->phpDateTime = clone $this->phpDateTime; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.