-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathDateTimeSelect.php
367 lines (321 loc) · 9.94 KB
/
DateTimeSelect.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
declare(strict_types=1);
namespace Laminas\Form\Element;
use DateTime as PhpDateTime;
use DateTimeInterface;
use Exception;
use Laminas\Form\Exception\InvalidArgumentException;
use Laminas\Form\FormInterface;
use Laminas\Validator\Date as DateValidator;
use Laminas\Validator\ValidatorInterface;
use function array_merge;
use function is_array;
use function is_string;
use function sprintf;
class DateTimeSelect extends DateSelect
{
/**
* Select form element that contains values for hour
*
* @var Select
*/
protected $hourElement;
/**
* Select form element that contains values for minute
*
* @var Select
*/
protected $minuteElement;
/**
* Select form element that contains values for second
*
* @var Select
*/
protected $secondElement;
/**
* Is the seconds select shown when the element is rendered?
*
* @var bool
*/
protected $shouldShowSeconds = false;
/**
* Constructor. Add the hour, minute and second select elements
*
* @param null|int|string $name Optional name for the element
* @param array $options Optional options for the element
*/
public function __construct($name = null, array $options = [])
{
parent::__construct($name, $options);
$this->hourElement = new Select('hour');
$this->minuteElement = new Select('minute');
$this->secondElement = new Select('second');
}
/**
* Set options for DateTimeSelect element.
*
* Accepted options for DateTimeSelect (plus the ones from DateSelect):
*
* - hour_attributes: HTML attributes to be rendered with the hour element
* - minute_attributes: HTML attributes to be rendered with the minute element
* - second_attributes: HTML attributes to be rendered with the second element
* - should_show_seconds: if set to true, the seconds select is shown
*
* @return $this
*/
public function setOptions(iterable $options)
{
parent::setOptions($options);
if (isset($this->options['hour_attributes'])) {
$this->setHourAttributes($this->options['hour_attributes']);
}
if (isset($this->options['minute_attributes'])) {
$this->setMinuteAttributes($this->options['minute_attributes']);
}
if (isset($this->options['second_attributes'])) {
$this->setSecondAttributes($this->options['second_attributes']);
}
if (isset($this->options['should_show_seconds'])) {
$this->setShouldShowSeconds($this->options['should_show_seconds']);
}
return $this;
}
public function getHourElement(): Select
{
return $this->hourElement;
}
public function getMinuteElement(): Select
{
return $this->minuteElement;
}
public function getSecondElement(): Select
{
return $this->secondElement;
}
/** @return list<Select> */
public function getElements(): array
{
return array_merge(parent::getElements(), [
$this->hourElement,
$this->minuteElement,
$this->secondElement,
]);
}
/**
* Set the hour attributes
*
* @param array<string, scalar|null> $hourAttributes
* @return $this
*/
public function setHourAttributes(array $hourAttributes)
{
$this->hourElement->setAttributes($hourAttributes);
return $this;
}
/**
* Get the hour attributes
*
* @return array<string, scalar|null>
*/
public function getHourAttributes(): array
{
return $this->hourElement->getAttributes();
}
/**
* Set the minute attributes
*
* @param array<string, scalar|null> $minuteAttributes
* @return $this
*/
public function setMinuteAttributes(array $minuteAttributes)
{
$this->minuteElement->setAttributes($minuteAttributes);
return $this;
}
/**
* Get the minute attributes
*
* @return array<string, scalar|null>
*/
public function getMinuteAttributes(): array
{
return $this->minuteElement->getAttributes();
}
/**
* Set the second attributes
*
* @param array<string, scalar|null> $secondAttributes
* @return $this
*/
public function setSecondAttributes(array $secondAttributes)
{
$this->secondElement->setAttributes($secondAttributes);
return $this;
}
/**
* Get the second attributes
*
* @return array<string, scalar|null>
*/
public function getSecondAttributes(): array
{
return $this->secondElement->getAttributes();
}
/**
* If set to true, this indicate that the second select is shown. If set to true, the seconds will be
* assumed to always be 00
*
* @return $this
*/
public function setShouldShowSeconds(bool $shouldShowSeconds)
{
$this->shouldShowSeconds = $shouldShowSeconds;
return $this;
}
public function shouldShowSeconds(): bool
{
return $this->shouldShowSeconds;
}
/**
* @param PhpDateTime|iterable|string|null|mixed $value
* @return $this
* @throws InvalidArgumentException
*/
public function setValue($value)
{
if (is_string($value)) {
try {
$value = new PhpDateTime($value);
} catch (Exception) {
throw new InvalidArgumentException('Value should be a parsable string or an instance of \DateTime');
}
}
if (null === $value && ! $this->shouldCreateEmptyOption()) {
$value = new PhpDateTime();
}
if ($value instanceof DateTimeInterface) {
$value = [
'year' => $value->format('Y'),
'month' => $value->format('m'),
'day' => $value->format('d'),
'hour' => $value->format('H'),
'minute' => $value->format('i'),
'second' => $value->format('s'),
];
}
if (is_array($value)) {
$this->yearElement->setValue($value['year']);
$this->monthElement->setValue($value['month']);
$this->dayElement->setValue($value['day']);
$this->hourElement->setValue($value['hour']);
$this->minuteElement->setValue($value['minute']);
$this->secondElement->setValue($value['second'] ?? '00');
} else {
$this->yearElement->setValue(null);
$this->monthElement->setValue(null);
$this->dayElement->setValue(null);
$this->hourElement->setValue(null);
$this->minuteElement->setValue(null);
$this->secondElement->setValue(null);
}
return $this;
}
public function getValue(): ?string
{
$year = $this->getYearElement()->getValue();
$month = $this->getMonthElement()->getValue();
$day = $this->getDayElement()->getValue();
$hour = $this->getHourElement()->getValue();
$minute = $this->getMinuteElement()->getValue();
$second = $this->getSecondElement()->getValue();
// if everything is null, return null
if (
$this->shouldCreateEmptyOption()
&& null === $year && null === $month && null === $day
&& null === $hour && null === $minute && (null === $second || '00' === $second)
) {
return null;
}
// if time is given, but date is null, use current date
if (
$this->shouldCreateEmptyOption()
&& null === $year && null === $month && null === $day
) {
$now = new PhpDateTime();
$year = $now->format('Y');
$month = $now->format('m');
$day = $now->format('d');
}
// if date is given, but time is null, use 00:00:00 instead
if (
$this->shouldCreateEmptyOption()
&& null === $hour && null === $minute && (null === $second || '00' === $second)
) {
$hour = '00';
$minute = '00';
$second = '00';
}
return sprintf(
'%04d-%02d-%02d %02d:%02d:%02d',
$year,
$month,
$day,
$hour,
$minute,
$second
);
}
/**
* Prepare the form element (mostly used for rendering purposes)
*/
public function prepareElement(FormInterface $form): void
{
parent::prepareElement($form);
$name = $this->getName();
$this->hourElement->setName($name . '[hour]');
$this->minuteElement->setName($name . '[minute]');
$this->secondElement->setName($name . '[second]');
}
/**
* Get validator
*/
protected function getValidator(): ValidatorInterface
{
if (null === $this->validator) {
$this->validator = new DateValidator(['format' => 'Y-m-d H:i:s']);
}
return $this->validator;
}
/**
* @inheritDoc
*/
public function getInputSpecification(): array
{
$spec = [
'required' => false,
'filters' => [
['name' => 'DateTimeSelect'],
],
'validators' => [
$this->getValidator(),
],
];
$name = $this->getName();
if ($name !== null) {
$spec['name'] = $name;
}
return $spec;
}
/**
* Clone the element (this is needed by Collection element, as it needs different copies of the elements)
*/
public function __clone()
{
$this->dayElement = clone $this->dayElement;
$this->monthElement = clone $this->monthElement;
$this->yearElement = clone $this->yearElement;
$this->hourElement = clone $this->hourElement;
$this->minuteElement = clone $this->minuteElement;
$this->secondElement = clone $this->secondElement;
}
}