1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LVR\CreditCard; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
|
7
|
|
|
class ExpirationDateValidator |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
protected $year; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $month; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* ExpirationDateValidator constructor. |
21
|
|
|
* |
22
|
|
|
* @param string $year |
23
|
|
|
* @param string $month |
24
|
|
|
*/ |
25
|
5 |
|
public function __construct(string $year, string $month) |
26
|
|
|
{ |
27
|
5 |
|
$this->setYear($year); |
28
|
5 |
|
$this->month = trim($month); |
29
|
5 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $year |
33
|
|
|
* @param string $month |
34
|
|
|
* |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
1 |
|
public static function validate(string $year, string $month) |
38
|
|
|
{ |
39
|
1 |
|
return (new static($year, $month))->isValid(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
5 |
|
public function isValid() |
46
|
|
|
{ |
47
|
5 |
|
return $this->isValidYear() |
48
|
5 |
|
&& $this->isValidMonth() |
49
|
5 |
|
&& $this->isFeatureDate(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param $year |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
5 |
|
protected function setYear($year) |
58
|
|
|
{ |
59
|
5 |
|
$year = trim($year); |
60
|
|
|
|
61
|
5 |
|
if (strlen($year) == 2) { |
62
|
2 |
|
$year = substr(date('Y'), 0, 2).$year; |
63
|
|
|
} |
64
|
|
|
|
65
|
5 |
|
$this->year = $year; |
66
|
|
|
|
67
|
5 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
5 |
|
protected function month() |
74
|
|
|
{ |
75
|
5 |
|
return str_pad($this->month, 2, '0', STR_PAD_LEFT); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
5 |
|
protected function isValidYear() |
82
|
|
|
{ |
83
|
5 |
|
$test = '/^'.substr(date('Y'), 0, 2).'\d\d$/'; |
84
|
|
|
|
85
|
5 |
|
return $this->year != '' |
86
|
5 |
|
&& preg_match($test, $this->year); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
5 |
|
protected function isValidMonth() |
93
|
|
|
{ |
94
|
5 |
|
return $this->month != '' |
95
|
5 |
|
&& $this->month() != '00' |
96
|
5 |
|
&& preg_match('/^(0[1-9]|1[0-2])$/', $this->month()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
5 |
|
protected function isFeatureDate() |
103
|
|
|
{ |
104
|
5 |
|
return Carbon::now()->startOfDay()->lte( |
105
|
5 |
|
Carbon::createFromFormat('Y-m', $this->year.'-'.$this->month())->endOfDay() |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|