Development

#1665: sfMyDateValidator.class.php

You must first sign up to be able to contribute.

Ticket #1665: sfMyDateValidator.class.php

File sfMyDateValidator.class.php, 3.5 kB (added by Arthur.Koziel, 1 year ago)
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.
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     Arthur Koziel <arthur@arthurkoziel.de>
18  * @author        Fabio Dellutri <fdellutri@gmail.com>
19  * @author     Nick Lane <nick.lane@internode.on.net>
20  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
21  * @author     Sean Kerr <skerr@mojavi.org>
22  * @version    SVN: $Id: sfDateValidator.class.php 3233 2007-01-11 21:01:08Z fabien $
23  */
24 class sfMyDateValidator extends sfValidator
25 {
26     /**
27    * Execute this validator.
28    *
29    * @param mixed A file or parameter value/array
30    * @param error An error message reference
31    *
32    * @return bool true, if this validator executes successfully, otherwise false
33    */
34     public function execute(&$value, &$error)
35     {
36         // Gets user culture
37         $user_culture = sfContext::getInstance()->getUser()->getCulture();       
38         
39         // Validate the given date
40         $value1 = $this->getValidDate($value, $user_culture);
41         if (!$value1)
42         {
43             $error = $this->getParameterHolder()->get('date_error');
44             return false;
45         }
46
47         // Is there a compare to do?
48         $compareDate = $this->getParameterHolder()->get('compare');
49
50         // If the compare date is given
51         if ($compareDate)
52         {
53             $compareValue = $this->getContext()->getRequest()->getParameter($compareDate);
54             $operator = $this->getParameterHolder()->get('operator');
55             $value2 = $this->getValidDate($compareValue, $user_culture);
56
57             // If the check date is valid, compare it. Otherwise ignore the comparison
58             if ($value2)
59             {
60                 $valid = false;
61                 switch ($operator)
62                 {
63                     case '>':
64                         $valid = $value1 $value2;
65                         break;
66                     case '>=':
67                         $valid = $value1 >= $value2;
68                         break;
69                     case '==':
70                         $valid = $value1 == $value2;
71                         break;
72                     case '<=':
73                         $valid = $value1 <= $value2;
74                         break;
75                     case '<':
76                         $valid = $value1 $value2;
77                         break;
78
79                     default:
80                         throw new sfValidatorException(sprintf('Invalid date comparison operator "%s"', $operator));
81                 }
82
83                 if (!$valid)
84                 {
85                     $error = $this->getParameter('compare_error');
86
87                     return false;
88                 }
89             }
90         }
91
92         return true;
93     }
94
95     /**
96    * Converts the given date into a Unix timestamp.
97    *
98    * Returns null if the date is invalid
99    *
100    * @param $value    Date to convert
101    * @param $culture  Language culture to use
102    */
103     protected function getValidDate($value, $culture)
104     {
105         if (!empty($culture))
106         {
107             list($d, $m, $y) = sfI18N::getDateForCulture(
108                   $value,
109                   $culture
110             );
111             $value = "$y-$m-$d";
112         }
113
114         $result = strtotime($value);
115         if ($result == -1 || $result === null)
116         {
117             return null;
118         }
119         return $result;
120     }
121
122     /**
123    * Initializes the validator.
124    *
125    * @param sfContext The current application context
126    * @param array   An associative array of initialization parameters
127    *
128    * @return bool true, if initialization completes successfully, otherwise false
129    */
130     public function initialize($context, $parameters = null)
131     {
132         // Initialize parent
133         parent::initialize($context, $parameters);
134
135         // Set defaults
136         $this->getParameterHolder()->set('date_error', 'Invalid date');
137         $this->getParameterHolder()->set('compare_error', 'Compare failed');
138         $this->getParameterHolder()->set('operator', '==');
139
140         $this->getParameterHolder()->add($parameters);
141
142         return true;
143     }
144 }
145