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