Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'i18n-date' of https://github.com/cgmartin/zf2 into hotf…
Browse files Browse the repository at this point in the history
…ix/validator-date-i18n

Conflicts:
	library/Zend/Validator/Date.php
	tests/Zend/Validator/DateTest.php
  • Loading branch information
weierophinney committed Jun 29, 2012
2 parents 6d50117 + 83146f0 commit cec42fc
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 293 deletions.
154 changes: 30 additions & 124 deletions src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
namespace Zend\Validator;

use Traversable;
use Zend\Date as ZendDate;
use Zend\Locale\Format;
use Zend\Locale\Locale;
use Zend\Stdlib\ArrayUtils;
use DateTime;

/**
* @category Zend
Expand All @@ -44,7 +41,7 @@ class Date extends AbstractValidator
* @var array
*/
protected $messageTemplates = array(
self::INVALID => "Invalid type given. String, integer, array or Zend_Date expected",
self::INVALID => "Invalid type given. String, integer, array or DateTime expected",
self::INVALID_DATE => "'%value%' does not appear to be a valid date",
self::FALSEFORMAT => "'%value%' does not fit the date format '%format%'",
);
Expand All @@ -63,13 +60,6 @@ class Date extends AbstractValidator
*/
protected $format;

/**
* Optional locale
*
* @var string|\Zend\Locale\Locale|null
*/
protected $locale;

/**
* Sets validator options
*
Expand All @@ -78,52 +68,22 @@ class Date extends AbstractValidator
public function __construct($options = array())
{
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
$options = iterator_to_array($options);
} elseif (!is_array($options)) {
$options = func_get_args();
$temp['format'] = array_shift($options);
if (!empty($options)) {
$temp['locale'] = array_shift($options);
}

$options = $temp;
}

if (array_key_exists('format', $options)) {
$this->setFormat($options['format']);
}

if (array_key_exists('locale', $options)) {
$this->setLocale($options['locale']);
}

parent::__construct($options);
}

/**
* Returns the locale option
*
* @return string|Locale|null
*/
public function getLocale()
{
return $this->locale;
}

/**
* Sets the locale option
*
* @param string|Locale $locale
* @return Date provides a fluent interface
*/
public function setLocale($locale = null)
{
$this->locale = Locale::findLocale($locale);
return $this;
}

/**
* Returns the locale option
* Returns the format option
*
* @return string|null
*/
Expand All @@ -146,42 +106,51 @@ public function setFormat($format = null)

/**
* Returns true if $value is a valid date of the format YYYY-MM-DD
* If optional $format or $locale is set the date format is checked
* according to Zend_Date, see Zend_Date::isDate()
* If optional $format is set the date format is checked
* according to DateTime
*
* @param string|array|ZendDate\Date $value
* @param string|array|int|DateTime $value
* @return boolean
*/
public function isValid($value)
{
if (!is_string($value)
&& !is_int($value)
&& !is_float($value)
&& !is_array($value)
&& !($value instanceof ZendDate\Date)
&& !is_int($value)
&& !($value instanceof DateTime)
) {
$this->error(self::INVALID);
return false;
}

$this->setValue($value);

if (($this->format !== null)
|| ($this->locale !== null)
|| is_array($value)
|| $value instanceof ZendDate\Date
$format = $this->getFormat();

if ($value instanceof DateTime) {
return true;
} elseif (is_int($value)
|| (is_string($value) && null !== $format)
) {
if (!ZendDate\Date::isDate($value, $this->format, $this->locale)) {
if ($this->checkFormat($value) === false) {
$this->error(self::FALSEFORMAT);
} else {
$this->error(self::INVALID_DATE);
}
$date = (is_int($value))
? date_create("@$value") // from timestamp
: DateTime::createFromFormat($format, $value);

// Invalid dates can show up as warnings (ie. "2007-02-99")
// and still return a DateTime object
$errors = DateTime::getLastErrors();

if (false === $date || $errors['warning_count'] > 0) {
$this->error(self::INVALID_DATE);
return false;
}
} else {
if (is_array($value)) {
$value = implode('-', $value);
}

if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
$this->format = 'yyyy-MM-dd';
$this->format = 'Y-m-d';
$this->error(self::FALSEFORMAT);
$this->format = null;
return false;
Expand All @@ -197,67 +166,4 @@ public function isValid($value)

return true;
}

/**
* Check if the given date fits the given format
*
* @param string $value Date to check
* @return boolean False when date does not fit the format
*/
private function checkFormat($value)
{
try {
$parsed = Format::getDate((string) $value, array(
'date_format' => $this->format,
'format_type' => 'iso',
'fix_date' => false,
));
if (isset($parsed['year'])
&& ((strpos(strtoupper($this->format), 'YY') !== false)
&& (strpos(strtoupper($this->format), 'YYYY') === false))
) {
$parsed['year'] = ZendDate\Date::getFullYear($parsed['year']);
}
} catch (\Exception $e) {
// Date can not be parsed
return false;
}

if (((strpos($this->format, 'Y') !== false) || (strpos($this->format, 'y') !== false))
&& (!isset($parsed['year']))
) {
// Year expected but not found
return false;
}

if ((strpos($this->format, 'M') !== false) && (!isset($parsed['month']))) {
// Month expected but not found
return false;
}

if ((strpos($this->format, 'd') !== false) && (!isset($parsed['day']))) {
// Day expected but not found
return false;
}

if (((strpos($this->format, 'H') !== false) || (strpos($this->format, 'h') !== false))
&& (!isset($parsed['hour']))
) {
// Hour expected but not found
return false;
}

if ((strpos($this->format, 'm') !== false) && (!isset($parsed['minute']))) {
// Minute expected but not found
return false;
}

if ((strpos($this->format, 's') !== false) && (!isset($parsed['second']))) {
// Second expected but not found
return false;
}

// Date fits the format
return true;
}
}
Loading

0 comments on commit cec42fc

Please sign in to comment.