[Solar-svn] Revision 3129

pmjones at solarphp.com pmjones at solarphp.com
Fri Apr 25 08:27:57 CDT 2008


Solar_Filter_ValidateIso(Date|Time|Timestamp): [ADD] These filters now accept the date/time/timestamp value as an array of parts keyed on their date() format characters (Y, m, d, H, i, s) and will validate them after internally converting to a string.



Modified: trunk/Solar/Filter/ValidateIsoDate.php
===================================================================
--- trunk/Solar/Filter/ValidateIsoDate.php	2008-04-25 13:16:47 UTC (rev 3128)
+++ trunk/Solar/Filter/ValidateIsoDate.php	2008-04-25 13:27:55 UTC (rev 3129)
@@ -24,7 +24,7 @@
  * @package Solar_Filter
  * 
  */
-class Solar_Filter_ValidateIsoDate extends Solar_Filter_Abstract {
+class Solar_Filter_ValidateIsoDate extends Solar_Filter_ValidateIsoTimestamp {
     
     /**
      * 
@@ -40,6 +40,11 @@
      */
     public function validateIsoDate($value)
     {
+        // look for Ymd keys?
+        if (is_array($value)) {
+            $value = $this->_arrayToDate($value);
+        }
+        
         if ($this->_filter->validateBlank($value)) {
             return ! $this->_filter->getRequire();
         }

Modified: trunk/Solar/Filter/ValidateIsoTime.php
===================================================================
--- trunk/Solar/Filter/ValidateIsoTime.php	2008-04-25 13:16:47 UTC (rev 3128)
+++ trunk/Solar/Filter/ValidateIsoTime.php	2008-04-25 13:27:55 UTC (rev 3129)
@@ -24,12 +24,16 @@
  * @package Solar_Filter
  * 
  */
-class Solar_Filter_ValidateIsoTime extends Solar_Filter_Abstract {
+class Solar_Filter_ValidateIsoTime extends Solar_Filter_ValidateIsoTimestamp {
     
     /**
      * 
      * Validates that the value is an ISO 8601 time string (hh:ii::ss format).
      * 
+     * As an alternative, the value may be an array with all of the keys for
+     * `H`, `i`, and optionally `s`, in which case the value is
+     * converted to an ISO 8601 string before validating it.
+     * 
      * Per note from Chris Drozdowski about ISO 8601, allows two
      * midnight times ... 00:00:00 for the beginning of the day, and
      * 24:00:00 for the end of the day.
@@ -41,6 +45,11 @@
      */
     public function validateIsoTime($value)
     {
+        // look for His keys?
+        if (is_array($value)) {
+            $value = $this->_arrayToTime($value);
+        }
+        
         $expr = '/^(([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]$/D';
         
         return $this->_filter->validatePregMatch($value, $expr) ||

Modified: trunk/Solar/Filter/ValidateIsoTimestamp.php
===================================================================
--- trunk/Solar/Filter/ValidateIsoTimestamp.php	2008-04-25 13:16:47 UTC (rev 3128)
+++ trunk/Solar/Filter/ValidateIsoTimestamp.php	2008-04-25 13:27:55 UTC (rev 3129)
@@ -31,7 +31,10 @@
      * Validates that the value is an ISO 8601 timestamp string.
      * 
      * The format is "yyyy-mm-ddThh:ii:ss" (note the literal "T" in the
-     * middle, which acts as a separator -- may also be a space).
+     * middle, which acts as a separator -- may also be a space). As an
+     * alternative, the value may be an array with all of the keys for
+     * `Y, m, d, H, i`, and optionally `s`, in which case the value is
+     * converted to an ISO 8601 string before validating it.
      * 
      * Also checks that the date itself is valid (for example, no Feb 30).
      * 
@@ -42,6 +45,11 @@
      */
     public function validateIsoTimestamp($value)
     {
+        // look for YmdHis keys?
+        if (is_array($value)) {
+            $value = $this->_arrayToTimestamp($value);
+        }
+        
         if ($this->_filter->validateBlank($value)) {
             return ! $this->_filter->getRequire();
         }
@@ -72,4 +80,78 @@
         // must be ok
         return true;
     }
+    
+    /**
+     * 
+     * Converts an array of timestamp parts to a string timestamp.
+     * 
+     * @var array $array The array of timestamp parts.
+     * 
+     * @return string
+     * 
+     */
+    protected function _arrayToTimestamp($array)
+    {
+        $value = $this->_arrayToDate($array)
+               . ' '
+               . $this->_arrayToTime($array);
+               
+        return trim($value);
+    }
+    
+    /**
+     * 
+     * Converts an array of date parts to a string date.
+     * 
+     * @var array $array The array of date parts.
+     * 
+     * @return string
+     * 
+     */
+    protected function _arrayToDate($array)
+    {
+        $date = array_key_exists('Y', $array) &&
+                trim($array['Y']) != '' &&
+                array_key_exists('m', $array) &&
+                trim($array['m']) != '' &&
+                array_key_exists('d', $array) &&
+                trim($array['d']) != '';
+              
+        if (! $date) {
+            return;
+        }
+        
+        return $array['Y'] . '-'
+             . $array['m'] . '-'
+             . $array['d'];
+    }
+    
+    /**
+     * 
+     * Converts an array of time parts to a string time.
+     * 
+     * @var array $array The array of time parts.
+     * 
+     * @return string
+     * 
+     */
+    protected function _arrayToTime($array)
+    {
+        $time = array_key_exists('H', $array) &&
+                trim($array['H']) != '' &&
+                array_key_exists('i', $array) &&
+                trim($array['i']) != '';
+              
+        if (! $time) {
+            return;
+        }
+        
+        $s = array_key_exists('s', $array) && trim($array['s']) != ''
+           ? $array['s']
+           : '00';
+        
+        return $array['H'] . ':'
+             . $array['i'] . ':'
+             . $s;
+    }
 }
\ No newline at end of file




More information about the Solar-svn mailing list