Development

/branches/1.2/lib/validator/sfValidatorDate.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/validator/sfValidatorDate.class.php

Revision 13278, 6.4 kB (checked in by FabianLange, 5 years ago)

[1.2] fixed PHPDoc on sfValidatorDate. fixes #5039

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfValidatorDate validates a date. It also converts the input value to a valid date.
13  *
14  * @package    symfony
15  * @subpackage validator
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfValidatorDate extends sfValidatorBase
20 {
21   /**
22    * Configures the current validator.
23    *
24    * Available options:
25    *
26    *  * date_format:             A regular expression that dates must match
27    *  * with_time:               true if the validator must return a time, false otherwise
28    *  * date_output:             The format to use when returning a date (default to Y-m-d)
29    *  * datetime_output:         The format to use when returning a date with time (default to Y-m-d H:i:s)
30    *  * date_format_error:       The date format to use when displaying an error for a bad_format error (use date_format if not provided)
31    *  * max:                     The maximum date allowed (as a timestamp)
32    *  * min:                     The minimum date allowed (as a timestamp)
33    *  * date_format_range_error: The date format to use when displaying an error for min/max (default to d/m/Y H:i:s)
34    *
35    * Available error codes:
36    *
37    *  * bad_format
38    *  * min
39    *  * max
40    *
41    * @param array $options    An array of options
42    * @param array $messages   An array of error messages
43    *
44    * @see sfValidatorBase
45    */
46   protected function configure($options = array(), $messages = array())
47   {
48     $this->addMessage('bad_format', '"%value%" does not match the date format (%date_format%).');
49     $this->addMessage('max', 'The date must be before %max%.');
50     $this->addMessage('min', 'The date must be after %min%.');
51
52     $this->addOption('date_format', null);
53     $this->addOption('with_time', false);
54     $this->addOption('date_output', 'Y-m-d');
55     $this->addOption('datetime_output', 'Y-m-d H:i:s');
56     $this->addOption('date_format_error');
57     $this->addOption('min', null);
58     $this->addOption('max', null);
59     $this->addOption('date_format_range_error', 'd/m/Y H:i:s');
60   }
61
62   /**
63    * @see sfValidatorBase
64    */
65   protected function doClean($value)
66   {
67     if (is_array($value))
68     {
69       $clean = $this->convertDateArrayToTimestamp($value);
70     }
71     else if ($regex = $this->getOption('date_format'))
72     {
73       if (!preg_match($regex, $value, $match))
74       {
75         throw new sfValidatorError($this, 'bad_format', array('value' => $value, 'date_format' => $this->getOption('date_format_error') ? $this->getOption('date_format_error') : $this->getOption('date_format')));
76       }
77
78       $clean = $this->convertDateArrayToTimestamp($match);
79     }
80     else if (!ctype_digit($value))
81     {
82       $clean = strtotime($value);
83       if (false === $clean)
84       {
85         throw new sfValidatorError($this, 'invalid', array('value' => $value));
86       }
87     }
88     else
89     {
90       $clean = (integer) $value;
91     }
92
93     if ($this->hasOption('max') && $clean > $this->getOption('max'))
94     {
95       throw new sfValidatorError($this, 'max', array('value' => $value, 'max' => date($this->getOption('date_format_range_error'), $this->getOption('max'))));
96     }
97
98     if ($this->hasOption('min') && $clean < $this->getOption('min'))
99     {
100       throw new sfValidatorError($this, 'min', array('value' => $value, 'min' => date($this->getOption('date_format_range_error'), $this->getOption('min'))));
101     }
102
103     return $clean === $this->getEmptyValue() ? $clean : date($this->getOption('with_time') ? $this->getOption('datetime_output') : $this->getOption('date_output'), $clean);
104   }
105
106   /**
107    * Converts an array representing a date to a timestamp.
108    *
109    * The array can contains the following keys: year, month, day, hour, minute, second
110    *
111    * @param  array $value  An array of date elements
112    *
113    * @return int A timestamp
114    */
115   protected function convertDateArrayToTimestamp($value)
116   {
117     // all elements must be empty or a number
118     foreach (array('year', 'month', 'day', 'hour', 'minute', 'second') as $key)
119     {
120       if (isset($value[$key]) && !preg_match('#^\d+$#', $value[$key]) && !empty($value[$key]))
121       {
122         throw new sfValidatorError($this, 'invalid', array('value' => $value));
123       }
124     }
125
126     // if one date value is empty, all others must be empty too
127     $empties =
128       (!isset($value['year']) || !$value['year'] ? 1 : 0) +
129       (!isset($value['month']) || !$value['month'] ? 1 : 0) +
130       (!isset($value['day']) || !$value['day'] ? 1 : 0)
131     ;
132     if ($empties > 0 && $empties < 3)
133     {
134       throw new sfValidatorError($this, 'invalid', array('value' => $value));
135     }
136     else if (3 == $empties)
137     {
138       return $this->getEmptyValue();
139     }
140
141     if (!checkdate(intval($value['month']), intval($value['day']), intval($value['year'])))
142     {
143       throw new sfValidatorError($this, 'invalid', array('value' => $value));
144     }
145
146     if ($this->getOption('with_time'))
147     {
148       // if second is set, minute and hour must be set
149       // if minute is set, hour must be set
150       if (
151         $this->isValueSet($value, 'second') && (!$this->isValueSet($value, 'minute') || !$this->isValueSet($value, 'hour'))
152         ||
153         $this->isValueSet($value, 'minute') && !$this->isValueSet($value, 'hour')
154       )
155       {
156         throw new sfValidatorError($this, 'invalid', array('value' => $value));
157       }
158
159       $clean = mktime(
160         isset($value['hour']) ? intval($value['hour']) : 0,
161         isset($value['minute']) ? intval($value['minute']) : 0,
162         isset($value['second']) ? intval($value['second']) : 0,
163         intval($value['month']),
164         intval($value['day']),
165         intval($value['year'])
166       );
167     }
168     else
169     {
170       $clean = mktime(0, 0, 0, intval($value['month']), intval($value['day']), intval($value['year']));
171     }
172
173     if (false === $clean)
174     {
175       throw new sfValidatorError($this, 'invalid', array('value' => var_export($value, true)));
176     }
177
178     return $clean;
179   }
180
181   protected function isValueSet($values, $key)
182   {
183     return isset($values[$key]) && !in_array($values[$key], array(null, ''), true);
184   }
185
186   /**
187    * @see sfValidatorBase
188    */
189   protected function isEmpty($value)
190   {
191     if (is_array($value))
192     {
193       $filtered = array_filter($value);
194
195       return empty($filtered);
196     }
197
198     return parent::isEmpty($value);
199   }
200 }
201
Note: See TracBrowser for help on using the browser.