forked from zendframework/zendframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Date.php
161 lines (140 loc) · 4.14 KB
/
Date.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Validator
*/
namespace Zend\Validator;
use DateTime;
use Traversable;
/**
* @category Zend
* @package Zend_Validator
*/
class Date extends AbstractValidator
{
const INVALID = 'dateInvalid';
const INVALID_DATE = 'dateInvalidDate';
const FALSEFORMAT = 'dateFalseFormat';
/**
* Validation failure message template definitions
*
* @var array
*/
protected $messageTemplates = array(
self::INVALID => "Invalid type given. String, integer, array or DateTime expected",
self::INVALID_DATE => "The input does not appear to be a valid date",
self::FALSEFORMAT => "The input does not fit the date format '%format%'",
);
/**
* @var array
*/
protected $messageVariables = array(
'format' => 'format'
);
/**
* Optional format
*
* @var string|null
*/
protected $format;
/**
* Sets validator options
*
* @param string|array|Traversable $options OPTIONAL
*/
public function __construct($options = array())
{
if ($options instanceof Traversable) {
$options = iterator_to_array($options);
} elseif (!is_array($options)) {
$options = func_get_args();
$temp['format'] = array_shift($options);
$options = $temp;
}
if (array_key_exists('format', $options)) {
$this->setFormat($options['format']);
}
parent::__construct($options);
}
/**
* Returns the format option
*
* @return string|null
*/
public function getFormat()
{
return $this->format;
}
/**
* Sets the format option
*
* @param string $format
* @return Date provides a fluent interface
*/
public function setFormat($format = null)
{
$this->format = $format;
return $this;
}
/**
* Returns true if $value is a valid date of the format YYYY-MM-DD
* If optional $format is set the date format is checked
* according to DateTime
*
* @param string|array|int|DateTime $value
* @return boolean
*/
public function isValid($value)
{
if (!is_string($value)
&& !is_array($value)
&& !is_int($value)
&& !($value instanceof DateTime)
) {
$this->error(self::INVALID);
return false;
}
$this->setValue($value);
$format = $this->getFormat();
if ($value instanceof DateTime) {
return true;
} elseif (is_int($value)
|| (is_string($value) && null !== $format)
) {
$date = (is_int($value))
? date_create("@$value") // from timestamp
: DateTime::createFromFormat($format, $value);
// Invalid dates can show up as warnings (ie. "2007-02-99")
// and still return a DateTime object
$errors = DateTime::getLastErrors();
if ($errors['warning_count'] > 0) {
$this->error(self::INVALID_DATE);
return false;
}
if ($date === false) {
$this->error(self::INVALID_DATE);
return false;
}
} else {
if (is_array($value)) {
$value = implode('-', $value);
}
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
$this->format = 'Y-m-d';
$this->error(self::FALSEFORMAT);
$this->format = null;
return false;
}
list($year, $month, $day) = sscanf($value, '%d-%d-%d');
if (!checkdate($month, $day, $year)) {
$this->error(self::INVALID_DATE);
return false;
}
}
return true;
}
}