This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
FormDateTimeSelect.php
290 lines (251 loc) · 9.46 KB
/
FormDateTimeSelect.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Form\View\Helper;
use DateTime;
use IntlDateFormatter;
use Zend\Form\ElementInterface;
use Zend\Form\Element\DateTimeSelect as DateTimeSelectElement;
use Zend\Form\Exception;
use Zend\Form\View\Helper\FormDateSelect as FormDateSelectHelper;
class FormDateTimeSelect extends FormDateSelectHelper
{
/**
* Time formatter to use
*
* @var int
*/
protected $timeType = IntlDateFormatter::LONG;
/**
* Render a date element that is composed of six selects
*
* @param ElementInterface $element
* @throws \Zend\Form\Exception\InvalidArgumentException
* @throws \Zend\Form\Exception\DomainException
* @return string
*/
public function render(ElementInterface $element)
{
if (!$element instanceof DateTimeSelectElement) {
throw new Exception\InvalidArgumentException(sprintf(
'%s requires that the element is of type Zend\Form\Element\DateTimeSelect',
__METHOD__
));
}
$name = $element->getName();
if ($name === null || $name === '') {
throw new Exception\DomainException(sprintf(
'%s requires that the element has an assigned name; none discovered',
__METHOD__
));
}
$shouldRenderDelimiters = $element->shouldRenderDelimiters();
$selectHelper = $this->getSelectElementHelper();
$pattern = $this->parsePattern($shouldRenderDelimiters);
$daysOptions = $this->getDaysOptions($pattern['day']);
$monthsOptions = $this->getMonthsOptions($pattern['month']);
$yearOptions = $this->getYearsOptions($element->getMinYear(), $element->getMaxYear());
$hourOptions = $this->getHoursOptions($pattern['hour']);
$minuteOptions = $this->getMinutesOptions($pattern['minute']);
$secondOptions = $this->getSecondsOptions($pattern['second']);
$dayElement = $element->getDayElement()->setValueOptions($daysOptions);
$monthElement = $element->getMonthElement()->setValueOptions($monthsOptions);
$yearElement = $element->getYearElement()->setValueOptions($yearOptions);
$hourElement = $element->getHourElement()->setValueOptions($hourOptions);
$minuteElement = $element->getMinuteElement()->setValueOptions($minuteOptions);
$secondElement = $element->getSecondElement()->setValueOptions($secondOptions);
if ($element->shouldCreateEmptyOption()) {
$dayElement->setEmptyOption('');
$yearElement->setEmptyOption('');
$monthElement->setEmptyOption('');
$hourElement->setEmptyOption('');
$minuteElement->setEmptyOption('');
$secondElement->setEmptyOption('');
}
$data = array();
$data[$pattern['day']] = $selectHelper->render($dayElement);
$data[$pattern['month']] = $selectHelper->render($monthElement);
$data[$pattern['year']] = $selectHelper->render($yearElement);
$data[$pattern['hour']] = $selectHelper->render($hourElement);
$data[$pattern['minute']] = $selectHelper->render($minuteElement);
if ($element->shouldShowSeconds()) {
$data[$pattern['second']] = $selectHelper->render($secondElement);
} else {
unset($pattern['second']);
if ($shouldRenderDelimiters) {
unset($pattern[4]);
}
}
$markup = '';
foreach ($pattern as $key => $value) {
// Delimiter
if (is_numeric($key)) {
$markup .= $value;
} else {
$markup .= $data[$value];
}
}
$markup = trim($markup);
return $markup;
}
/**
* Invoke helper as function
*
* Proxies to {@link render()}.
*
* @param \Zend\Form\ElementInterface $element
* @param int $dateType
* @param int|null|string $timeType
* @param null|string $locale
* @return string
*/
public function __invoke(
ElementInterface $element = null,
$dateType = IntlDateFormatter::LONG,
$timeType = IntlDateFormatter::LONG,
$locale = null
) {
if (!$element) {
return $this;
}
$this->setDateType($dateType);
$this->setTimeType($timeType);
if ($locale !== null) {
$this->setLocale($locale);
}
return $this->render($element);
}
/**
* @param int $timeType
* @return FormDateTimeSelect
*/
public function setTimeType($timeType)
{
// The FULL format uses values that are not used
if ($timeType === IntlDateFormatter::FULL) {
$timeType = IntlDateFormatter::LONG;
}
$this->timeType = $timeType;
return $this;
}
/**
* @return int
*/
public function getTimeType()
{
return $this->timeType;
}
/**
* Override to also get time part
*
* @return string
*/
public function getPattern()
{
if ($this->pattern === null) {
$intl = new IntlDateFormatter($this->getLocale(), $this->dateType, $this->timeType);
// remove time zone format character
$pattern = rtrim($intl->getPattern(), ' z');
$this->pattern = $pattern;
}
return $this->pattern;
}
/**
* Parse the pattern
*
* @param bool $renderDelimiters
* @return array
*/
protected function parsePattern($renderDelimiters = true)
{
$pattern = $this->getPattern();
$pregResult = preg_split("/([ -,.:\/]*'.*?'[ -,.:\/]*)|([ -,.:\/]+)/", $pattern, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$result = array();
foreach ($pregResult as $value) {
if (stripos($value, "'") === false && stripos($value, 'd') !== false) {
$result['day'] = $value;
} elseif (stripos($value, "'") === false && strpos($value, 'M') !== false) {
$result['month'] = $value;
} elseif (stripos($value, "'") === false && stripos($value, 'y') !== false) {
$result['year'] = $value;
} elseif (stripos($value, "'") === false && stripos($value, 'h') !== false) {
$result['hour'] = $value;
} elseif (stripos($value, "'") === false && stripos($value, 'm') !== false) {
$result['minute'] = $value;
} elseif (stripos($value, "'") === false && strpos($value, 's') !== false) {
$result['second'] = $value;
} elseif (stripos($value, "'") === false && stripos($value, 'a') !== false) {
// ignore ante/post meridiem marker
continue;
} elseif ($renderDelimiters) {
$result[] = str_replace("'", '', $value);
}
}
return $result;
}
/**
* Create a key => value options for hours
*
* @param string $pattern Pattern to use for hours
* @return array
*/
protected function getHoursOptions($pattern)
{
$keyFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, 'HH');
$valueFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, $pattern);
$date = new DateTime('1970-01-01 00:00:00');
$result = array();
for ($hour = 1; $hour <= 24; $hour++) {
$key = $keyFormatter->format($date);
$value = $valueFormatter->format($date);
$result[$key] = $value;
$date->modify('+1 hour');
}
return $result;
}
/**
* Create a key => value options for minutes
*
* @param string $pattern Pattern to use for minutes
* @return array
*/
protected function getMinutesOptions($pattern)
{
$keyFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, 'mm');
$valueFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, $pattern);
$date = new DateTime('1970-01-01 00:00:00');
$result = array();
for ($min = 1; $min <= 60; $min++) {
$key = $keyFormatter->format($date);
$value = $valueFormatter->format($date);
$result[$key] = $value;
$date->modify('+1 minute');
}
return $result;
}
/**
* Create a key => value options for seconds
*
* @param string $pattern Pattern to use for seconds
* @return array
*/
protected function getSecondsOptions($pattern)
{
$keyFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, 'ss');
$valueFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, $pattern);
$date = new DateTime('1970-01-01 00:00:00');
$result = array();
for ($sec = 1; $sec <= 60; $sec++) {
$key = $keyFormatter->format($date);
$value = $valueFormatter->format($date);
$result[$key] = $value;
$date->modify('+1 second');
}
return $result;
}
}