Skip to content

Commit 9ca0ad6

Browse files
committed
ExpirationDateValidator
1 parent 71a2226 commit 9ca0ad6

12 files changed

+164
-68
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,13 @@ $request->validate(
9797
```
9898
#### Directly
9999
```php
100-
LVR\CreditCard\Cards\Card::isValidExpirationDate(
100+
LVR\CreditCard\Cards\ExpirationDateValidator(
101+
$expiration_year,
102+
$expiration_month
103+
)->isValid();
104+
105+
// Or static
106+
LVR\CreditCard\Cards\ExpirationDateValidator::validate(
101107
$expiration_year,
102108
$expiration_month
103109
);

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"illuminate/translation": "^5.5"
88
},
99
"require-dev": {
10-
"friendsofphp/php-cs-fixer": "~2.6",
10+
"php": ">=7.0.0",
11+
"friendsofphp/php-cs-fixer": "2.5.*",
1112
"phpunit/phpunit": "6.2.*",
1213
"orchestra/testbench": "^3.5"
1314
},

composer.lock

Lines changed: 16 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CardExpirationDate.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace LVR\CreditCard;
44

5-
use Illuminate\Support\Carbon;
6-
use LVR\CreditCard\Cards\Card;
5+
use Carbon\Carbon;
76
use Illuminate\Contracts\Validation\Rule;
87

98
class CardExpirationDate implements Rule
@@ -44,7 +43,8 @@ public function passes($attribute, $value)
4443
try {
4544
$date = Carbon::createFromFormat($this->format, $value);
4645

47-
return Card::isValidExpirationDate($date->year, $date->month);
46+
return (new ExpirationDateValidator($date->year, $date->month))
47+
->isValid();
4848
} catch (\InvalidArgumentException $ex) {
4949
$this->message = static::MSG_CARD_EXPIRATION_DATE_FORMAT_INVALID;
5050

src/CardExpirationMonth.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace LVR\CreditCard;
44

5-
use LVR\CreditCard\Cards\Card;
65
use Illuminate\Contracts\Validation\Rule;
76

87
class CardExpirationMonth implements Rule
@@ -36,7 +35,12 @@ public function __construct(string $year)
3635
*/
3736
public function passes($attribute, $value)
3837
{
39-
return Card::isValidExpirationDate($this->year, $value);
38+
try {
39+
return (new ExpirationDateValidator($this->year, $value))
40+
->isValid();
41+
} catch (\Exception $e) {
42+
return false;
43+
};
4044
}
4145

4246
/**

src/CardExpirationYear.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace LVR\CreditCard;
44

5-
use LVR\CreditCard\Cards\Card;
65
use Illuminate\Contracts\Validation\Rule;
76

87
class CardExpirationYear implements Rule
@@ -36,7 +35,12 @@ public function __construct(string $month)
3635
*/
3736
public function passes($attribute, $value)
3837
{
39-
return Card::isValidExpirationDate($value, $this->month);
38+
try {
39+
return (new ExpirationDateValidator($value, $this->month))
40+
->isValid();
41+
} catch (\Exception $e) {
42+
return false;
43+
};
4044
}
4145

4246
/**

src/Cards/Card.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -158,36 +158,6 @@ public static function isValidCvcLength($cvc, array $available_lengths = [3, 4])
158158
&& in_array(strlen($cvc), $available_lengths, true);
159159
}
160160

161-
/**
162-
* @param string $year
163-
* @param string $month
164-
*
165-
* @return bool
166-
*/
167-
public static function isValidExpirationDate(string $year, string $month)
168-
{
169-
if ($year == '' || $month == '') {
170-
return false;
171-
}
172-
173-
$month = str_pad($month, 2, '0', STR_PAD_LEFT);
174-
175-
if (! preg_match('/^20\d\d$/', $year)) {
176-
return false;
177-
}
178-
179-
if (! preg_match('/^(0[1-9]|1[0-2])$/', $month)) {
180-
return false;
181-
}
182-
183-
// past date
184-
if ($year < date('Y') || $year == date('Y') && $month < date('m')) {
185-
return false;
186-
}
187-
188-
return true;
189-
}
190-
191161
/**
192162
* @throws \LVR\CreditCard\Exceptions\CreditCardException
193163
*/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace LVR\CreditCard\Exceptions;
4+
5+
class CreditCardExpirationDateException extends CreditCardException
6+
{
7+
}

src/ExpirationDateValidator.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace LVR\CreditCard;
4+
5+
use Carbon\Carbon;
6+
use LVR\CreditCard\Exceptions\CreditCardExpirationDateException;
7+
8+
class ExpirationDateValidator
9+
{
10+
/**
11+
* @var string
12+
*/
13+
protected $year;
14+
15+
/**
16+
* @var string
17+
*/
18+
protected $month;
19+
20+
/**
21+
* ExpirationDateValidator constructor.
22+
*
23+
* @param string $year
24+
* @param string $month
25+
*
26+
* @throws \LVR\CreditCard\Exceptions\CreditCardExpirationDateException
27+
*/
28+
public function __construct(string $year, string $month)
29+
{
30+
if ($year == '' || $month == '') {
31+
throw new CreditCardExpirationDateException;
32+
}
33+
34+
$this->year = $year;
35+
$this->month = str_pad($month, 2, '0', STR_PAD_LEFT);
36+
}
37+
38+
/**
39+
* @param string $year
40+
* @param string $month
41+
*
42+
* @return mixed
43+
*/
44+
public static function validate(string $year, string $month)
45+
{
46+
return (new static($year, $month))->isValid();
47+
}
48+
49+
/**
50+
* @return bool
51+
*/
52+
public function isValid()
53+
{
54+
return $this->isValidYear()
55+
&& $this->isValidMonth()
56+
&& $this->isFeatureDate();
57+
}
58+
59+
/**
60+
* @return bool
61+
*/
62+
protected function isValidYear()
63+
{
64+
return (bool) preg_match('/^20\d\d$/', $this->year);
65+
}
66+
67+
/**
68+
* @return bool
69+
*/
70+
protected function isValidMonth()
71+
{
72+
return (bool) preg_match('/^(0[1-9]|1[0-2])$/', $this->month);
73+
}
74+
75+
/**
76+
* @return bool
77+
*/
78+
protected function isFeatureDate()
79+
{
80+
return Carbon::now()->startOfDay()->lte(
81+
Carbon::createFromFormat('Y-m', $this->year.'-'.$this->month)->endOfDay()
82+
);
83+
}
84+
}

tests/Unit/CardExpirationTest.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace LVR\CreditCard\Tests\Unit;
44

55
use LVR\CreditCard\Cards\Card;
6+
use LVR\CreditCard\Exceptions\CreditCardExpirationDateException;
7+
use LVR\CreditCard\ExpirationDateValidator;
68
use LVR\CreditCard\Tests\TestCase;
79
use LVR\CreditCard\CardExpirationDate;
810
use LVR\CreditCard\CardExpirationYear;
@@ -118,12 +120,31 @@ public function it_checks_expiration_date()
118120
/** @test **/
119121
public function it_can_be_called_directly()
120122
{
121-
$this->assertTrue(Card::isValidExpirationDate(date('Y'), date('m')));
122-
$this->assertFalse(Card::isValidExpirationDate('', ''));
123-
$this->assertFalse(Card::isValidExpirationDate('', date('m')));
124-
$this->assertFalse(Card::isValidExpirationDate(date('Y'), ''));
123+
$this->assertTrue(ExpirationDateValidator::validate(date('Y'), date('m')));
125124
}
126125

126+
/** @test **/
127+
public function it_throws_exception_if_year_is_empty()
128+
{
129+
$this->expectException(CreditCardExpirationDateException::class);
130+
$this->assertFalse(ExpirationDateValidator::validate('', date('m')));
131+
}
132+
133+
/** @test **/
134+
public function it_throws_exception_if_month_is_empty()
135+
{
136+
$this->expectException(CreditCardExpirationDateException::class);
137+
$this->assertFalse(ExpirationDateValidator::validate(date('y'), ''));
138+
}
139+
140+
/** @test **/
141+
public function it_throws_exception_if_year_and_month_is_empty()
142+
{
143+
$this->expectException(CreditCardExpirationDateException::class);
144+
$this->assertFalse(ExpirationDateValidator::validate('', ''));
145+
}
146+
147+
127148
/**
128149
* @param string $year
129150
*

0 commit comments

Comments
 (0)