فهرست منبع

[ZF-7630, ZF-6457] Zend_Validate_Date/Zend_Date:

- allowed normalized array for validation
- allowed Zend_Date for validation

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@17695 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas 16 سال پیش
والد
کامیت
a417629e9a
3فایلهای تغییر یافته به همراه43 افزوده شده و 17 حذف شده
  1. 17 12
      library/Zend/Date.php
  2. 6 4
      library/Zend/Validate/Date.php
  3. 20 1
      tests/Zend/Validate/DateTest.php

+ 17 - 12
library/Zend/Date.php

@@ -4550,14 +4550,15 @@ class Zend_Date extends Zend_Date_DateObject
      * If no format is given the standard dateformat for the actual locale is used.
      * f.e. 30.February.2007 will return false if format is 'dd.MMMM.YYYY'
      *
-     * @param  string             $date   Date to parse for correctness
-     * @param  string             $format (Optional) Format for parsing the date string
-     * @param  string|Zend_Locale $locale (Optional) Locale for parsing date parts
-     * @return boolean            True when all date parts are correct
+     * @param  string|array|Zend_Date $date   Date to parse for correctness
+     * @param  string                 $format (Optional) Format for parsing the date string
+     * @param  string|Zend_Locale     $locale (Optional) Locale for parsing date parts
+     * @return boolean                True when all date parts are correct
      */
     public static function isDate($date, $format = null, $locale = null)
     {
-        if (!is_string($date) and !is_numeric($date) and !($date instanceof Zend_Date)) {
+        if (!is_string($date) && !is_numeric($date) && !($date instanceof Zend_Date) &&
+            !is_array($date)) {
             return false;
         }
 
@@ -4575,13 +4576,17 @@ class Zend_Date extends Zend_Date_DateObject
         }
 
         $format = self::_getLocalizedToken($format, $locale);
-        try {
-            $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale,
-                                                  'date_format' => $format, 'format_type' => 'iso',
-                                                  'fix_date' => false));
-        } catch (Zend_Locale_Exception $e) {
-            // Date can not be parsed
-            return false;
+        if (!is_array($date)) {
+            try {
+                $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale,
+                                                      'date_format' => $format, 'format_type' => 'iso',
+                                                      'fix_date' => false));
+            } catch (Zend_Locale_Exception $e) {
+                // Date can not be parsed
+                return false;
+            }
+        } else {
+            $parsed = $date;
         }
 
         if (((strpos($format, 'Y') !== false) or (strpos($format, 'y') !== false)) and

+ 6 - 4
library/Zend/Validate/Date.php

@@ -43,7 +43,7 @@ class Zend_Validate_Date extends Zend_Validate_Abstract
      * @var array
      */
     protected $_messageTemplates = array(
-        self::INVALID        => "Invalid type given, value should be string, or integer",
+        self::INVALID        => "Invalid type given, value should be string, integer, array or Zend_Date",
         self::NOT_YYYY_MM_DD => "'%value%' is not of the format YYYY-MM-DD",
         self::INVALID_DATE   => "'%value%' does not appear to be a valid date",
         self::FALSEFORMAT    => "'%value%' does not fit given date format"
@@ -137,19 +137,21 @@ class Zend_Validate_Date extends Zend_Validate_Abstract
      * If optional $format or $locale is set the date format is checked
      * according to Zend_Date, see Zend_Date::isDate()
      *
-     * @param  string $value
+     * @param  string|array|Zend_Date $value
      * @return boolean
      */
     public function isValid($value)
     {
-        if (!is_string($value) && !is_int($value) && !is_float($value)) {
+        if (!is_string($value) && !is_int($value) && !is_float($value) &&
+            !is_array($value) && !($value instanceof Zend_Date)) {
             $this->_error(self::INVALID);
             return false;
         }
 
         $this->_setValue($value);
 
-        if (($this->_format !== null) or ($this->_locale !== null)) {
+        if (($this->_format !== null) || ($this->_locale !== null) || is_array($value) ||
+             $value instanceof Zend_Date) {
             require_once 'Zend/Date.php';
             if (!Zend_Date::isDate($value, $this->_format, $this->_locale)) {
                 if ($this->_checkFormat($value) === false) {

+ 20 - 1
tests/Zend/Validate/DateTest.php

@@ -140,7 +140,7 @@ class Zend_Validate_DateTest extends PHPUnit_Framework_TestCase
         $this->assertTrue($this->_validator->setFormat('dd.MM.YYYY')->isValid('10.01.2008'));
         $this->assertEquals('dd.MM.YYYY', $this->_validator->getFormat());
 
-        $this->assertTrue($this->_validator->setFormat('MMM yyyy')->isValid('Jan 2010'));
+        $this->assertTrue($this->_validator->setFormat('MM yyyy')->isValid('01 2010'));
         $this->assertFalse($this->_validator->setFormat('dd/MM/yyyy')->isValid('2008/10/22'));
         $this->assertTrue($this->_validator->setFormat('dd/MM/yy')->isValid('22/10/08'));
         $this->assertFalse($this->_validator->setFormat('dd/MM/yy')->isValid('22/10'));
@@ -224,6 +224,25 @@ class Zend_Validate_DateTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * ZF-7630
+     */
+    public function testDateObjectVerification()
+    {
+        $date = new Zend_Date();
+        $this->assertTrue($this->_validator->isValid($date), "'$date' expected to be valid");
+    }
+
+    /**
+     * ZF-6457
+     */
+    public function testArrayVerification()
+    {
+        $date  = new Zend_Date();
+        $array = $date->toArray();
+        $this->assertTrue($this->_validator->isValid($array), "array expected to be valid");
+    }
+
+    /**
      * Ignores a raised PHP error when in effect, but throws a flag to indicate an error occurred
      *
      * @param  integer $errno