This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
FormMonthSelect.php
283 lines (241 loc) · 7.19 KB
/
FormMonthSelect.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
<?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 Locale;
use Zend\Form\ElementInterface;
use Zend\Form\Element\MonthSelect as MonthSelectElement;
use Zend\Form\Exception;
/**
* @category Zend
* @package Zend_Form
* @subpackage View
*/
class FormMonthSelect extends AbstractHelper
{
/**
* FormSelect helper
*
* @var FormSelect
*/
protected $selectHelper;
/**
* Date formatter to use
*
* @var int
*/
protected $dateType = IntlDateFormatter::LONG;
/**
* Pattern to use for Date rendering
*
* @var string
*/
protected $pattern;
/**
* Locale to use
*
* @var string
*/
protected $locale;
/**
* Render a month element that is composed of two selects
*
* @param \Zend\Form\ElementInterface $element
* @throws \Zend\Form\Exception\InvalidArgumentException
* @throws \Zend\Form\Exception\DomainException
* @return string
*/
public function render(ElementInterface $element)
{
if (!$element instanceof MonthSelectElement) {
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();
// The pattern always contains "day" part and the first separator, so we have to remove it
unset($pattern['day']);
unset($pattern[0]);
$monthsOptions = $this->getMonthsOptions($pattern['month']);
$yearOptions = $this->getYearsOptions($element->getMinYear(), $element->getMaxYear());
$monthElement = $element->getMonthElement()->setValueOptions($monthsOptions);
$yearElement = $element->getYearElement()->setValueOptions($yearOptions);
if ($element->shouldCreateEmptyOption()) {
$monthElement->setEmptyOption('');
$yearElement->setEmptyOption('');
}
$markup = array();
$markup[$pattern['month']] = $selectHelper->render($monthElement);
$markup[$pattern['year']] = $selectHelper->render($yearElement);
$markup = sprintf(
'%s %s %s',
$markup[array_shift($pattern)],
array_shift($pattern), // Delimiter
$markup[array_shift($pattern)]
);
return $markup;
}
/**
* Invoke helper as function
*
* Proxies to {@link render()}.
*
* @param \Zend\Form\ElementInterface $element
* @param int $dateType
* @param null|string $locale
* @return FormDateSelect
*/
public function __invoke(ElementInterface $element = null, $dateType = IntlDateFormatter::LONG, $locale = null)
{
if (!$element) {
return $this;
}
$this->setDateType($dateType);
if ($locale !== null) {
$this->setLocale($locale);
}
return $this->render($element);
}
/**
* @return string
*/
public function getPattern()
{
if ($this->pattern === null) {
$intl = new IntlDateFormatter($this->getLocale(), $this->dateType, IntlDateFormatter::NONE);
$this->pattern = $intl->getPattern();
}
return $this->pattern;
}
/**
* Parse the pattern
*
* @return array
*/
protected function parsePattern()
{
$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, 'd') !== false) {
$result['day'] = $value;
} elseif (stripos($value, 'm') !== false) {
$result['month'] = $value;
} elseif (stripos($value, 'y') !== false) {
$result['year'] = $value;
} else {
$result[] = $value;
}
}
return $result;
}
/**
* @param int $dateType
* @return FormDateSelect
*/
public function setDateType($dateType)
{
// The FULL format uses values that are not used
if ($dateType === IntlDateFormatter::FULL) {
$dateType = IntlDateFormatter::LONG;
}
$this->dateType = $dateType;
return $this;
}
/**
* @return int
*/
public function getDateType()
{
return $this->dateType;
}
/**
* @param string $locale
* @return FormDateSelect
*/
public function setLocale($locale)
{
$this->locale = $locale;
return $this;
}
/**
* @return string
*/
public function getLocale()
{
if ($this->locale === null) {
$this->locale = Locale::getDefault();
}
return $this->locale;
}
/**
* Create a key => value options for months
*
* @param string $pattern Pattern to use for months
* @return array
*/
protected function getMonthsOptions($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');
$result = array();
for ($month = 1; $month <= 12; $month++) {
$key = $keyFormatter->format($date);
$value = $valueFormatter->format($date);
$result[$key] = $value;
$date->modify('+1 month');
}
return $result;
}
/**
* Create a key => value options for years
* NOTE: we don't use a pattern for years, as years written as two digits can lead to hard to
* read date for users, so we only use four digits years
*
* @param int $minYear
* @param int $maxYear
* @return array
*/
protected function getYearsOptions($minYear, $maxYear)
{
$result = array();
for ($i = $maxYear; $i >= $minYear; --$i) {
$result[$i] = $i;
}
return $result;
}
/**
* Retrieve the FormSelect helper
*
* @return FormRow
*/
protected function getSelectElementHelper()
{
if ($this->selectHelper) {
return $this->selectHelper;
}
if (method_exists($this->view, 'plugin')) {
$this->selectHelper = $this->view->plugin('formselect');
}
return $this->selectHelper;
}
}