Skip to content

Commit

Permalink
localizedTime()
Browse files Browse the repository at this point in the history
Date and/or time string validation that uses `I18n::Time` to parse the date. This means parsing is locale dependent.
  • Loading branch information
ypnos-web committed Feb 24, 2016
1 parent 262ec6e commit 31c21bf
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Validation/Validation.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\Validation;

use Cake\I18n\Time;
use Cake\Utility\Text;
use DateTime;
use LogicException;
Expand Down Expand Up @@ -456,6 +457,33 @@ public static function time($check)
return static::_check($check, '%^((0?[1-9]|1[012])(:[0-5]\d){0,2} ?([AP]M|[ap]m))$|^([01]\d|2[0-3])(:[0-5]\d){0,2}$%');
}

/**
* Date and/or time string validation.
* Uses `I18n::Time` to parse the date. This means parsing is locale dependent.
*
* @param string|\DateTime $check a date string or object (will always pass)
* @param string $type Parser type, one out of 'date', 'time', and 'datetime'
* @param string|int $format any format accepted by IntlDateFormatter
* @return bool Success
* @throws \InvalidArgumentException when unsupported $type given
* @see \Cake\I18N\Time::parseDate(), \Cake\I18N\Time::parseTime(), \Cake\I18N\Time::parseDateTime()
*/
public static function localizedTime($check, $type = 'datetime', $format = null)
{
if ($check instanceof DateTime) {
return true;
}
static $methods = [
'date' => 'parseDate',
'time' => 'parseTime',
'datetime' => 'parseDateTime',
];
if (empty($methods[$type])) {
throw new \InvalidArgumentException('Unsupported parser type given.');
}
return (!is_null(Time::{$methods[$type]}($check, $format)));
}

/**
* Boolean validation, determines if value passed is a boolean integer or true/false.
*
Expand Down

0 comments on commit 31c21bf

Please sign in to comment.