<commit_before><?php
/**
* @version		$Id$
* @category		Koowa
* @package      Koowa_Filter
* @copyright    Copyright (C) 2007 - 2009 <NAME> and <NAME>. All rights reserved.
* @license      GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
* @link 		http://www.koowa.org
*/

/**
 * Timestamp filter
 * 
 * Validates or sanitizes a value is an ISO 8601 timestamp string.
 *
 * @author		<NAME> <<EMAIL>>
 * @category	Koowa
 * @package     Koowa_Filter
 */
class KFilterTimestamp extends KFilterAbstract
{
	/**
	 * 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). 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).
	 * 
	 * @param mixed $value The value to validate.
	 * @return	bool	True when the variable is valid
	 */
	protected function _validate($value)
	{
		 // look for YmdHis keys?
        if (is_array($value)) {
            $value = $this->_arrayToTimestamp($value);
        }
        
        // correct length?
        if (strlen($value) != 19) {
            return false;
        }
        
        // valid date?
        $date = substr($value, 0, 10);
        if (! $this->_filter->validateIsoDate($date)) {
            return false;
        }
        
        // valid separator?
        $sep = substr($value, 10, 1);
        if ($sep != 'T' && $sep != ' ') {
            return false;
        }
        
        // valid time?
        $time = substr($value, 11, 8);
        if (! $this->_filter->validateIsoTime($time)) {
            return false;
        }
        
        return true;
	}
	
	/**
	 * Forces the value to an ISO-8601 formatted timestamp using a space separator 
	 * ("yyyy-mm-dd hh:ii:ss") instead of a "T" separator.
     * 
     * @param mixed The value to be sanitized.  If an integer, it is used as a Unix timestamp; 
     * 				otherwise, converted to a Unix timestamp using [[php::strtotime() | ]].  
     * 				If an array, and it has *all* the keys for `Y, m, d, h, i, s`, then the 
     * 				array is converted into an ISO 8601 string before sanitizing.
	 * @return	string The sanitized value.
	 */
	protected function _sanitize($value)
	{
		 // look for YmdHis keys?
        if (is_array($value)) {
            $value = $this->_arrayToTimestamp($value);
        }
		
        $format = 'Y-m-d H:i:s';
        if (is_int($value)) {
            return date($format, $value);
        } 
        
        return date($format, strtotime($value));
	}
	
	/**
     * Converts an array of timestamp parts to a string timestamp.
     * 
     * @param 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.
     * 
     * @param array The array of date parts.
     * @return string
     */
    protected function _arrayToDate($array)
    {<commit_msg>See #111 : null datetime results in KDatabaseException <commit_after><?php
/**
* @version		$Id$
* @category		Koowa
* @package      Koowa_Filter
* @copyright    Copyright (C) 2007 - 2009 <NAME> and <NAME>. All rights reserved.
* @license      GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
* @link 		http://www.koowa.org
*/

/**
 * Timestamp filter
 * 
 * Validates or sanitizes a value is an ISO 8601 timestamp string.
 *
 * @author		<NAME> <<EMAIL>>
 * @category	Koowa
 * @package     Koowa_Filter
 */
class KFilterTimestamp extends KFilterAbstract
{
	/**
	 * 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). 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).
	 * 
	 * @param mixed The value to validate.
	 * @return	bool	True when the variable is valid
	 */
	protected function _validate($value)
	{
		 // look for YmdHis keys?
        if (is_array($value)) {
            $value = $this->_arrayToTimestamp($value);
        }
        
        // correct length?
        if (strlen($value) != 19) {
            return false;
        }
        
        // valid date?
        $date = substr($value, 0, 10);
        if (! $this->_filter->validateIsoDate($date)) {
            return false;
        }
        
        // valid separator?
        $sep = substr($value, 10, 1);
        if ($sep != 'T' && $sep != ' ') {
            return false;
        }
        
        // valid time?
        $time = substr($value, 11, 8);
        if (! $this->_filter->validateIsoTime($time)) {
            return false;
        }
        
        return true;
	}
	
	/**
	 * Forces the value to an ISO-8601 formatted timestamp using a space separator 
	 * ("yyyy-mm-dd hh:ii:ss") instead of a "T" separator.
     * 
     * @param mixed The value to be sanitized.  If an integer, it is used as a Unix timestamp; 
     * 				otherwise, converted to a Unix timestamp using [[php::strtotime() | ]].  
     * 				If an array, and it has *all* the keys for `Y, m, d, h, i, s`, then the 
     * 				array is converted into an ISO 8601 string before sanitizing.
	 * @return	string The sanitized value.
	 */
	protected function _sanitize($value)
	{
		// look for YmdHis keys?
        if (is_array($value)) {
            $value = $this->_arrayToTimestamp($value);
        }
		
		$result = '0000-00-00 00:00:00';
        if (!(empty($value) || $value == $result))
        {
             $format = 'Y-m-d H:i:s';
        	if (is_int($value)) {
            	$result = date($format, $value);
        	} else {
        		$result = date($format, strtotime($value));
        	}	 
        } 
		
        return $result;
	}
	
	/**
     * Converts an array of timestamp parts to a string timestamp.
     * 
     * @param 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.
     * 
     * @param array The array of date parts.
     * @return string
     */
    protected function _arrayToDate($array)
    {
{'fraction_non_alphanumeric': 0.1220771768756276, 'fraction_numerical': 0.01506240137713384, 'mean_word_length': 1.9220452640402348, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7050565', 'n_tokens_mistral': 2287, 'n_tokens_neox': 2090, 'n_words': 707}