Skip to content

Commit 8153ed7

Browse files
committed
Let PHP normalize two digit years. Closes #27
1 parent ef23fe7 commit 8153ed7

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

src/Omnipay/Common/CreditCard.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ protected function setParameter($key, $value)
109109
return $this;
110110
}
111111

112+
protected function setYearParameter($key, $value)
113+
{
114+
// normalize year to four digits
115+
if (null === $value || '' === $value) {
116+
$value = null;
117+
} else {
118+
$value = (int) gmdate('Y', gmmktime(0, 0, 0, 1, 1, (int) $value));
119+
}
120+
121+
return $this->setParameter($key, $value);
122+
}
123+
112124
/**
113125
* Validate this credit card. If the card is invalid, InvalidCreditCardException is thrown.
114126
*
@@ -213,7 +225,7 @@ public function getExpiryYear()
213225

214226
public function setExpiryYear($value)
215227
{
216-
return $this->setParameter('expiryYear', $value ? Helper::normalizeYear($value) : $value);
228+
return $this->setYearParameter('expiryYear', $value);
217229
}
218230

219231
/**
@@ -243,7 +255,7 @@ public function getStartYear()
243255

244256
public function setStartYear($value)
245257
{
246-
return $this->setParameter('startYear', $value ? Helper::normalizeYear($value) : $value);
258+
return $this->setYearParameter('startYear', $value);
247259
}
248260

249261
/**

src/Omnipay/Common/Helper.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,6 @@ function ($match) {
3030
);
3131
}
3232

33-
/**
34-
* Normalize a year to four digits
35-
*/
36-
public static function normalizeYear($year)
37-
{
38-
$year = (int) $year;
39-
if ($year < 100) {
40-
$year += 2000;
41-
}
42-
43-
return $year;
44-
}
45-
4633
/**
4734
* Validate a card number according to the Luhn algorithm.
4835
*

0 commit comments

Comments
 (0)