1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Larium\CreditCard; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
|
9
|
|
|
use function intval; |
10
|
|
|
use function str_pad; |
11
|
|
|
use function substr; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ExpiryDate class provides help for handling credit card expiration date. |
15
|
|
|
* |
16
|
|
|
* @author Andreas Kollaros <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
final class ExpiryDate |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Expiration year. |
22
|
|
|
* Two or four digit of expiration year. |
23
|
|
|
* Will be converted to four digit if two digit input provided. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $year; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Expiration month. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $month; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $month |
38
|
|
|
* @param string $year |
39
|
|
|
*/ |
40
|
30 |
|
public function __construct(string $month, string $year) |
41
|
|
|
{ |
42
|
30 |
|
$pad = "20"; # Since expiration would be in future, assuming start millenium is 2000. |
43
|
|
|
# Please correct this accordingly in next millenium :P. |
44
|
30 |
|
$this->year = str_pad($year, 4, $pad, STR_PAD_LEFT); |
45
|
30 |
|
$this->month = $month; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Whether date is in past or not. |
50
|
|
|
* |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
17 |
|
public function isExpired(): bool |
54
|
|
|
{ |
55
|
17 |
|
return (new DateTime()) > $this->getExpiration(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns expiration date |
60
|
|
|
* |
61
|
|
|
* @return DateTime |
62
|
|
|
*/ |
63
|
17 |
|
public function getExpiration(): DateTime |
64
|
|
|
{ |
65
|
17 |
|
$dateTime = new DateTime(); |
66
|
|
|
|
67
|
17 |
|
return $dateTime |
68
|
17 |
|
->setDate(intval($this->year), intval($this->month), intval($this->getMonthDays())) |
69
|
17 |
|
->setTime(23, 59, 59); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns four digit year. |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
1 |
|
public function getYear(): string |
78
|
|
|
{ |
79
|
1 |
|
return $this->year; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns two digit year. |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
1 |
|
public function getTwoDigitYear(): string |
88
|
|
|
{ |
89
|
1 |
|
return substr($this->year, 2, 2); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns month. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
16 |
|
public function getMonth(): string |
98
|
|
|
{ |
99
|
16 |
|
return $this->month; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Return two digit month. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
1 |
|
public function getTwoDigitMonth(): string |
108
|
|
|
{ |
109
|
1 |
|
return str_pad($this->month, 2, '0', STR_PAD_LEFT); |
110
|
|
|
} |
111
|
|
|
|
112
|
17 |
|
private function getMonthDays(): string |
113
|
|
|
{ |
114
|
17 |
|
$dateTime = new DateTime("{$this->year}-{$this->month}-01"); |
115
|
17 |
|
return $dateTime->format('t'); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|