<?php
/**
 * Implementation of the validator for a datetime input.
 *
 * @package MKLib
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */

/**
 * The validator for a datetime input.
 *
 * Input must be in the format d-M-YYYY HH:MM:SS, example: 01-12-2007 01:22:00
 * is 1-th December 2007 1:22 AM
 *
 * @package MKLib
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */
class MKLib_DateTimeValid extends Zend_Validate_Abstract
{
    const MSG_DATETIME = 'DateTime';

    const DATETIME_FORMAT = "d-M-YYYY HH:mm:SS";

    /**
     * format variable for the $_messageVariables
     * @var string
     */
    public $format;

    protected $_messageVariables = array('format' => "format");

    protected $_messageTemplates = array(self::MSG_DATETIME => "'%value%' is not valid DateTime format %format% ");

    /**
     * Main validation function
     * @return bool
     */
    public function isValid($value)
    {
        $this->_setValue($value);
        if (!Zend_Date::isDate($value, self::DATETIME_FORMAT)) {
            $this->format = self::DATETIME_FORMAT;
            $this->_setValue($value);
            //only one message, so self MSG_DATETIME is used
            $this->_error();
            return false;
        }
        return true;
    }
}
