Changeset 3129

Show
Ignore:
Timestamp:
04/25/08 08:27:55 (2 months ago)
Author:
pmjones
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Solar/Filter/ValidateIsoDate.php

    r2926 r3129  
    2525 *  
    2626 */ 
    27 class Solar_Filter_ValidateIsoDate extends Solar_Filter_Abstract
     27class Solar_Filter_ValidateIsoDate extends Solar_Filter_ValidateIsoTimestamp
    2828     
    2929    /** 
     
    4141    public function validateIsoDate($value) 
    4242    { 
     43        // look for Ymd keys? 
     44        if (is_array($value)) { 
     45            $value = $this->_arrayToDate($value); 
     46        } 
     47         
    4348        if ($this->_filter->validateBlank($value)) { 
    4449            return ! $this->_filter->getRequire(); 
  • trunk/Solar/Filter/ValidateIsoTime.php

    r2926 r3129  
    2525 *  
    2626 */ 
    27 class Solar_Filter_ValidateIsoTime extends Solar_Filter_Abstract
     27class Solar_Filter_ValidateIsoTime extends Solar_Filter_ValidateIsoTimestamp
    2828     
    2929    /** 
    3030     *  
    3131     * Validates that the value is an ISO 8601 time string (hh:ii::ss format). 
     32     *  
     33     * As an alternative, the value may be an array with all of the keys for 
     34     * `H`, `i`, and optionally `s`, in which case the value is 
     35     * converted to an ISO 8601 string before validating it. 
    3236     *  
    3337     * Per note from Chris Drozdowski about ISO 8601, allows two 
     
    4246    public function validateIsoTime($value) 
    4347    { 
     48        // look for His keys? 
     49        if (is_array($value)) { 
     50            $value = $this->_arrayToTime($value); 
     51        } 
     52         
    4453        $expr = '/^(([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]$/D'; 
    4554         
  • trunk/Solar/Filter/ValidateIsoTimestamp.php

    r2926 r3129  
    3232     *  
    3333     * The format is "yyyy-mm-ddThh:ii:ss" (note the literal "T" in the 
    34      * middle, which acts as a separator -- may also be a space). 
     34     * middle, which acts as a separator -- may also be a space). As an 
     35     * alternative, the value may be an array with all of the keys for 
     36     * `Y, m, d, H, i`, and optionally `s`, in which case the value is 
     37     * converted to an ISO 8601 string before validating it. 
    3538     *  
    3639     * Also checks that the date itself is valid (for example, no Feb 30). 
     
    4346    public function validateIsoTimestamp($value) 
    4447    { 
     48        // look for YmdHis keys? 
     49        if (is_array($value)) { 
     50            $value = $this->_arrayToTimestamp($value); 
     51        } 
     52         
    4553        if ($this->_filter->validateBlank($value)) { 
    4654            return ! $this->_filter->getRequire(); 
     
    7381        return true; 
    7482    } 
     83     
     84    /** 
     85     *  
     86     * Converts an array of timestamp parts to a string timestamp. 
     87     *  
     88     * @var array $array The array of timestamp parts. 
     89     *  
     90     * @return string 
     91     *  
     92     */ 
     93    protected function _arrayToTimestamp($array) 
     94    { 
     95        $value = $this->_arrayToDate($array) 
     96               . ' ' 
     97               . $this->_arrayToTime($array); 
     98                
     99        return trim($value); 
     100    } 
     101     
     102    /** 
     103     *  
     104     * Converts an array of date parts to a string date. 
     105     *  
     106     * @var array $array The array of date parts. 
     107     *  
     108     * @return string 
     109     *  
     110     */ 
     111    protected function _arrayToDate($array) 
     112    { 
     113        $date = array_key_exists('Y', $array) && 
     114                trim($array['Y']) != '' && 
     115                array_key_exists('m', $array) && 
     116                trim($array['m']) != '' && 
     117                array_key_exists('d', $array) && 
     118                trim($array['d']) != ''; 
     119               
     120        if (! $date) { 
     121            return; 
     122        } 
     123         
     124        return $array['Y'] . '-' 
     125             . $array['m'] . '-' 
     126             . $array['d']; 
     127    } 
     128     
     129    /** 
     130     *  
     131     * Converts an array of time parts to a string time. 
     132     *  
     133     * @var array $array The array of time parts. 
     134     *  
     135     * @return string 
     136     *  
     137     */ 
     138    protected function _arrayToTime($array) 
     139    { 
     140        $time = array_key_exists('H', $array) && 
     141                trim($array['H']) != '' && 
     142                array_key_exists('i', $array) && 
     143                trim($array['i']) != ''; 
     144               
     145        if (! $time) { 
     146            return; 
     147        } 
     148         
     149        $s = array_key_exists('s', $array) && trim($array['s']) != '' 
     150           ? $array['s'] 
     151           : '00'; 
     152         
     153        return $array['H'] . ':' 
     154             . $array['i'] . ':' 
     155             . $s; 
     156    } 
    75157}