-
-
Save bshaffer/384001 to your computer and use it in GitHub Desktop.
sfValidatorCCExpirationDate.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* sfValidatorCCExpirationDate validates the expiration date for a credit card. | |
* | |
* @author Brent Shaffer <[email protected]> | |
*/ | |
class sfValidatorCCExpirationDate extends sfValidatorDate | |
{ | |
/** | |
* Converts an array representing a date to a timestamp. | |
* | |
* The array can contains the following keys: year, month, day, hour, minute, second | |
* | |
* @param array $value An array of date elements | |
* | |
* @return int A timestamp | |
*/ | |
protected function convertDateArrayToString($value) | |
{ | |
// all elements must be empty or a number | |
foreach (array('year', 'month', 'day', 'hour', 'minute', 'second') as $key) | |
{ | |
if (isset($value[$key]) && !preg_match('#^\d+$#', $value[$key]) && !empty($value[$key])) | |
{ | |
throw new sfValidatorError($this, 'invalid', array('value' => $value)); | |
} | |
} | |
// if one date value is empty, all others must be empty too | |
$empties = | |
(!isset($value['year']) || !$value['year'] ? 1 : 0) + | |
(!isset($value['month']) || !$value['month'] ? 1 : 0) | |
; | |
if ($empties > 0 && $empties < 2) | |
{ | |
throw new sfValidatorError($this, 'invalid', array('value' => $value)); | |
} | |
else if (2 == $empties) | |
{ | |
return $this->getEmptyValue(); | |
} | |
if (!checkdate(intval($value['month']), '01', intval($value['year']))) | |
{ | |
throw new sfValidatorError($this, 'invalid', array('value' => $value)); | |
} | |
if ($this->getOption('with_time')) | |
{ | |
// if second is set, minute and hour must be set | |
// if minute is set, hour must be set | |
if ( | |
$this->isValueSet($value, 'second') && (!$this->isValueSet($value, 'minute') || !$this->isValueSet($value, 'hour')) | |
|| | |
$this->isValueSet($value, 'minute') && !$this->isValueSet($value, 'hour') | |
) | |
{ | |
throw new sfValidatorError($this, 'invalid', array('value' => $value)); | |
} | |
$clean = mktime( | |
0, 0, 0, | |
intval($value['month']), | |
1, | |
intval($value['year']) | |
); | |
} | |
else | |
{ | |
$clean = mktime(0, 0, 0, intval($value['month']), 1, intval($value['year'])); | |
} | |
if (false === $clean) | |
{ | |
throw new sfValidatorError($this, 'invalid', array('value' => var_export($value, true))); | |
} | |
return $clean; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment