Skip to content

Commit 904bc84

Browse files
committed
allow two digit representation of a year in year validator
1 parent 70dd79c commit 904bc84

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/ExpirationDateValidator.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,28 @@ class ExpirationDateValidator
2424
*/
2525
public function __construct(string $year, string $month)
2626
{
27-
$this->year = trim($year);
27+
$this->setYear($year);
2828
$this->month = trim($month);
2929
}
3030

31+
/**
32+
* @param $year
33+
*
34+
* @return $this
35+
*/
36+
protected function setYear($year)
37+
{
38+
$year = trim($year);
39+
40+
if (strlen($year) == 2) {
41+
$year = substr(date('Y'), 0, 2) . $year;
42+
}
43+
44+
$this->year = $year;
45+
46+
return $this;
47+
}
48+
3149
/**
3250
* @param string $year
3351
* @param string $month
@@ -62,8 +80,10 @@ protected function month()
6280
*/
6381
protected function isValidYear()
6482
{
83+
$test = '/^'.substr(date('Y'), 0, 2).'\d\d$/';
84+
6585
return $this->year != ''
66-
&& preg_match('/^20\d\d$/', $this->year);
86+
&& preg_match($test, $this->year);
6787
}
6888

6989
/**

tests/Unit/CardExpirationTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,21 @@ public function it_can_be_called_directly()
124124
$this->assertFalse(ExpirationDateValidator::validate(date('y'), ''));
125125
$this->assertFalse(ExpirationDateValidator::validate(' ', ' '));
126126
$this->assertTrue(ExpirationDateValidator::validate(date('Y'), date('m')));
127+
$this->assertTrue(ExpirationDateValidator::validate(date('y'), date('m')));
127128
}
128129

130+
/** @test **/
131+
public function it_validates_two_digits_year_format()
132+
{
133+
$validator = Validator::make(
134+
['expiration_year' => date('y')],
135+
['expiration_year' => ['required', new CardExpirationYear(date('m'))]]
136+
);
137+
138+
$this->assertTrue($validator->passes());
139+
}
140+
141+
129142
/**
130143
* @param string $year
131144
*

0 commit comments

Comments
 (0)