|
@@ -77,23 +77,17 @@ class DateType extends AbstractType
|
|
|
|
|
|
// Only pass a subset of the options to children
|
|
// Only pass a subset of the options to children
|
|
$yearOptions = array(
|
|
$yearOptions = array(
|
|
- 'choice_list' => new PaddedChoiceList(
|
|
|
|
- array_combine($options['years'], $options['years']), 4, '0', STR_PAD_LEFT
|
|
|
|
- ),
|
|
|
|
|
|
+ 'choices' => $this->formatTimestamps($formatter, '/y+/', $this->listYears($options['years'])),
|
|
'empty_value' => $options['empty_value']['year'],
|
|
'empty_value' => $options['empty_value']['year'],
|
|
'required' => $options['required'],
|
|
'required' => $options['required'],
|
|
);
|
|
);
|
|
$monthOptions = array(
|
|
$monthOptions = array(
|
|
- 'choice_list' => new MonthChoiceList(
|
|
|
|
- $formatter, $options['months']
|
|
|
|
- ),
|
|
|
|
|
|
+ 'choices' => $this->formatTimestamps($formatter, '/[M|L]+/', $this->listMonths($options['months'])),
|
|
'empty_value' => $options['empty_value']['month'],
|
|
'empty_value' => $options['empty_value']['month'],
|
|
'required' => $options['required'],
|
|
'required' => $options['required'],
|
|
);
|
|
);
|
|
$dayOptions = array(
|
|
$dayOptions = array(
|
|
- 'choice_list' => new PaddedChoiceList(
|
|
|
|
- array_combine($options['days'], $options['days']), 2, '0', STR_PAD_LEFT
|
|
|
|
- ),
|
|
|
|
|
|
+ 'choices' => $this->formatTimestamps($formatter, '/d+/', $this->listDays($options['days'])),
|
|
'empty_value' => $options['empty_value']['day'],
|
|
'empty_value' => $options['empty_value']['day'],
|
|
'required' => $options['required'],
|
|
'required' => $options['required'],
|
|
);
|
|
);
|
|
@@ -208,4 +202,64 @@ class DateType extends AbstractType
|
|
{
|
|
{
|
|
return 'date';
|
|
return 'date';
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ private function formatTimestamps(\IntlDateFormatter $formatter, $regex, array $timestamps)
|
|
|
|
+ {
|
|
|
|
+ $pattern = $formatter->getPattern();
|
|
|
|
+ $timezone = $formatter->getTimezoneId();
|
|
|
|
+ // Use it, if exists.
|
|
|
|
+ $setTimeZone = method_exists($formatter, 'setTimeZone');
|
|
|
|
+ if ($setTimeZone) {
|
|
|
|
+ $formatter->setTimeZone('UTC');
|
|
|
|
+ } else {
|
|
|
|
+ $formatter->setTimeZoneId('UTC');
|
|
|
|
+ }
|
|
|
|
+ if (preg_match($regex, $pattern, $matches)) {
|
|
|
|
+ $formatter->setPattern($matches[0]);
|
|
|
|
+ foreach ($timestamps as $key => $timestamp) {
|
|
|
|
+ $timestamps[$key] = $formatter->format($timestamp);
|
|
|
|
+ }
|
|
|
|
+ // I'd like to clone the formatter above, but then we get a
|
|
|
|
+ // segmentation fault, so let's restore the old state instead
|
|
|
|
+ $formatter->setPattern($pattern);
|
|
|
|
+ }
|
|
|
|
+ if ($setTimeZone) {
|
|
|
|
+ $formatter->setTimeZone($timezone);
|
|
|
|
+ } else {
|
|
|
|
+ $formatter->setTimeZoneId($timezone);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return $timestamps;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function listYears(array $years)
|
|
|
|
+ {
|
|
|
|
+ $result = array();
|
|
|
|
+ foreach ($years as $year) {
|
|
|
|
+ $result[$year] = gmmktime(0, 0, 0, 6, 15, $year);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return $result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function listMonths(array $months)
|
|
|
|
+ {
|
|
|
|
+ $result = array();
|
|
|
|
+ foreach ($months as $month) {
|
|
|
|
+ $result[$month] = gmmktime(0, 0, 0, $month, 15);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return $result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function listDays(array $days)
|
|
|
|
+ {
|
|
|
|
+ $result = array();
|
|
|
|
+ foreach ($days as $day) {
|
|
|
|
+ $result[$day] = gmmktime(0, 0, 0, 5, $day);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return $result;
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|