This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
FormDateSelect.php
110 lines (94 loc) · 3.65 KB
/
FormDateSelect.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
<?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_Form
*/
namespace Zend\Form\View\Helper;
use DateTime;
use IntlDateFormatter;
use Zend\Form\ElementInterface;
use Zend\Form\Element\DateSelect as DateSelectElement;
use Zend\Form\Exception;
use Zend\Form\View\Helper\FormMonthSelect as FormMonthSelectHelper;
/**
* @category Zend
* @package Zend_Form
* @subpackage View
*/
class FormDateSelect extends FormMonthSelectHelper
{
/**
* Render a date element that is composed of three selects
*
* @param ElementInterface $element
* @throws \Zend\Form\Exception\InvalidArgumentException
* @throws \Zend\Form\Exception\DomainException
* @return string
*/
public function render(ElementInterface $element)
{
if (!$element instanceof DateSelectElement) {
throw new Exception\InvalidArgumentException(sprintf(
'%s requires that the element is of type Zend\Form\Element\Select',
__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__
));
}
$selectHelper = $this->getSelectElementHelper();
$pattern = $this->parsePattern();
$daysOptions = $this->getDaysOptions($pattern['day']);
$monthsOptions = $this->getMonthsOptions($pattern['month']);
$yearOptions = $this->getYearsOptions($element->getMinYear(), $element->getMaxYear());
if ($element->shouldCreateEmptyOption()) {
$daysOptions = array('' => '') + $daysOptions;
$monthsOptions = array('' => '') + $monthsOptions;
$yearOptions = array('' => '') + $yearOptions;
}
$dayElement = $element->getDayElement()->setValueOptions($daysOptions);
$monthElement = $element->getMonthElement()->setValueOptions($monthsOptions);
$yearElement = $element->getYearElement()->setValueOptions($yearOptions);
$markup = array();
$markup[$pattern['day']] = $selectHelper->render($dayElement);
$markup[$pattern['month']] = $selectHelper->render($monthElement);
$markup[$pattern['year']] = $selectHelper->render($yearElement);
$markup = sprintf(
'%s %s %s %s %s',
$markup[array_shift($pattern)],
array_shift($pattern), // Delimiter
$markup[array_shift($pattern)],
array_shift($pattern), // Delimiter
$markup[array_shift($pattern)]
);
return $markup;
}
/**
* Create a key => value options for days
*
* @param string $pattern Pattern to use for days
* @return array
*/
protected function getDaysOptions($pattern)
{
$keyFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, 'dd');
$valueFormatter = new IntlDateFormatter($this->getLocale(), null, null, null, null, $pattern);
$date = new DateTime('1970-01-01');
$result = array();
for ($day = 1; $day <= 31; $day++) {
$key = $keyFormatter->format($date);
$value = $valueFormatter->format($date);
$result[$key] = $value;
$date->modify('+1 day');
}
return $result;
}
}