Development

/branches/1.1/lib/plugins/sfCompat10Plugin/lib/validator/sfDateValidator.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/plugins/sfCompat10Plugin/lib/validator/sfDateValidator.class.php

Revision 10270, 3.7 kB (checked in by Carl.Vondrick, 6 years ago)

1.1: added deprecated warnings to all sfCompatPlugin classes (closes #3882)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
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 <sean@code-box.org>
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  * WARNING: This class is deprecated and will be removed in symfony 1.2.
16  *
17  * @package    symfony
18  * @subpackage validator
19  * @author     Nick Lane <nick.lane@internode.on.net>
20  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
21  * @author     Sean Kerr <sean@code-box.org>
22  * @version    SVN: $Id$
23  * @deprecated Deprecated since symfony 1.1
24  */
25 class sfDateValidator extends sfValidator
26 {
27   /**
28    * Execute this validator.
29    *
30    * @param mixed A file or parameter value/array
31    * @param error An error message reference
32    *
33    * @return bool true, if this validator executes successfully, otherwise false
34    */
35   public function execute(&$value, &$error)
36   {
37     $culture = $this->context->getUser()->getCulture();
38
39     // Validate the given date
40     $value1 = $this->getValidDate($value, $culture);
41     if (!$value1)
42     {
43       $error = $this->getParameter('date_error');
44
45       return false;
46     }
47
48     // Is there a compare to do?
49     $compareDateParam = $this->getParameter('compare');
50     $compareDate = $this->context->getRequest()->getParameter($compareDateParam);
51
52     // If the compare date is given
53     if ($compareDate)
54     {
55       $operator = trim($this->getParameter('operator', '=='), '\'" ');
56       $value2 = $this->getValidDate($compareDate, $culture);
57
58       // If the check date is valid, compare it. Otherwise ignore the comparison
59       if ($value2)
60       {
61         $valid = false;
62         switch ($operator)
63         {
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           case '<':
77             $valid = $value1 $value2;
78             break;
79
80           default:
81             throw new sfValidatorException(sprintf('Invalid date comparison operator "%s".', $operator));
82         }
83
84         if (!$valid)
85         {
86           $error = $this->getParameter('compare_error');
87
88           return false;
89         }
90       }
91     }
92
93     return true;
94   }
95
96   /**
97    * Converts the given date into a Unix timestamp.
98    *
99    * Returns null if the date is invalid
100    *
101    * @param $value    Date to convert
102    * @param $culture  Language culture to use
103    */
104   protected function getValidDate($value, $culture)
105   {
106     // Use the language culture date format
107     $result = $this->context->getI18N()->getDateForCulture($value, $culture);
108     list($d, $m, $y) = $result;
109
110     // Make sure the date is a valid gregorian calendar date also
111     if ($result === null || !checkdate($m, $d, $y))
112     {
113       return null;
114     }
115
116     return strtotime("$y-$m-$d 00:00");
117   }
118
119   /**
120    * Initializes the validator.
121    *
122    * @param sfContext The current application context
123    * @param array   An associative array of initialization parameters
124    *
125    * @return bool true, if initialization completes successfully, otherwise false
126    */
127   public function initialize($context, $parameters = null)
128   {
129     // Initialize parent
130     parent::initialize($context, $parameters);
131
132     // Set defaults
133     $this->getParameterHolder()->set('date_error', 'Invalid date');
134     $this->getParameterHolder()->set('compare_error', 'Compare failed');
135
136     $this->getParameterHolder()->add($parameters);
137
138     return true;
139   }
140 }
141
Note: See TracBrowser for help on using the browser.