[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/validator/ -> sfDateValidator.class.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the symfony package.
   5   * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
   6   * (c) 2004-2006 Sean Kerr.
   7   * 
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  /**
  13   * sfDateValidator verifies a parameter is of a date format.
  14   *
  15   * @package    symfony
  16   * @subpackage validator
  17   * @author     Nick Lane <nick.lane@internode.on.net>
  18   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  19   * @author     Sean Kerr <skerr@mojavi.org>
  20   * @version    SVN: $Id: sfDateValidator.class.php 3233 2007-01-11 21:01:08Z fabien $
  21   */
  22  class sfDateValidator extends sfValidator
  23  {
  24    /**
  25     * Execute this validator.
  26     *
  27     * @param mixed A file or parameter value/array
  28     * @param error An error message reference
  29     *
  30     * @return bool true, if this validator executes successfully, otherwise false
  31     */
  32    public function execute(&$value, &$error)
  33    {
  34      $culture = $this->getContext()->getUser()->getCulture();
  35  
  36      // Validate the given date
  37      $value1 = $this->getValidDate($value, $culture);
  38      if (!$value1)
  39      {
  40        $error = $this->getParameter('date_error');
  41  
  42        return false;
  43      }
  44  
  45      // Is there a compare to do?
  46      $compareDateParam = $this->getParameter('compare');
  47      $compareDate = $this->getContext()->getRequest()->getParameter($compareDateParam);
  48  
  49      // If the compare date is given
  50      if ($compareDate)
  51      {
  52        $operator = trim($this->getParameter('operator', '=='), '\'" ');
  53        $value2 = $this->getValidDate($compareDate, $culture);
  54  
  55        // If the check date is valid, compare it. Otherwise ignore the comparison
  56        if ($value2)
  57        {
  58          $valid = false;
  59          switch ($operator)
  60          {
  61            case '>':
  62              $valid = $value1 >  $value2;
  63              break;
  64            case '>=':
  65              $valid = $value1 >= $value2;
  66              break;
  67            case '==':
  68              $valid = $value1 == $value2;
  69              break;          
  70            case '<=':
  71              $valid = $value1 <= $value2;
  72              break;
  73            case '<':
  74              $valid = $value1 <  $value2;
  75              break;
  76  
  77            default:
  78              throw new sfValidatorException(sprintf('Invalid date comparison operator "%s"', $operator));
  79          }
  80  
  81          if (!$valid)
  82          {
  83            $error = $this->getParameter('compare_error');
  84  
  85            return false;
  86          }
  87        }
  88      }
  89  
  90      return true;
  91    }
  92  
  93    /**
  94     * Converts the given date into a Unix timestamp.
  95     * 
  96     * Returns null if the date is invalid
  97     * 
  98     * @param $value    Date to convert
  99     * @param $culture  Language culture to use
 100     */
 101    protected function getValidDate($value, $culture)
 102    {
 103      // Use the language culture date format
 104      $result = sfI18N::getDateForCulture($value, $culture);
 105      list($d, $m, $y) = $result;
 106  
 107      // Make sure the date is a valid gregorian calendar date also
 108      if ($result === null || !checkdate($m, $d, $y))
 109      {
 110        return null;
 111      }
 112  
 113      return strtotime("$y-$m-$d 00:00");
 114    }
 115  
 116    /**
 117     * Initializes the validator.
 118     *
 119     * @param sfContext The current application context
 120     * @param array   An associative array of initialization parameters
 121     *
 122     * @return bool true, if initialization completes successfully, otherwise false
 123     */
 124    public function initialize($context, $parameters = null)
 125    {
 126      // Initialize parent
 127      parent::initialize($context, $parameters);
 128  
 129      // Set defaults
 130      $this->getParameterHolder()->set('date_error', 'Invalid date');
 131      $this->getParameterHolder()->set('compare_error', 'Compare failed');
 132  
 133      $this->getParameterHolder()->add($parameters);
 134  
 135      return true;
 136    }
 137  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7