moved date_pattern to IntlDateFormatter

added code to use custom date_pattern
This commit is contained in:
Amal Raghav 2011-05-03 08:57:55 +05:30 committed by Matthieu Vachon
parent dd104bc8c0
commit 52a1e1d281
2 changed files with 26 additions and 24 deletions

View File

@ -23,8 +23,9 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
{
private $dateFormat;
private $timeFormat;
private $pattern;
private $calendar;
/**
* Constructor.
@ -39,7 +40,7 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
* @throws UnexpectedTypeException If a format is not supported
* @throws UnexpectedTypeException if a timezone is not a string
*/
public function __construct($inputTimezone = null, $outputTimezone = null, $dateFormat = null, $timeFormat = null)
public function __construct($inputTimezone = null, $outputTimezone = null, $dateFormat = null, $timeFormat = null, $calendar = null, $pattern = null)
{
parent::__construct($inputTimezone, $outputTimezone);
@ -61,6 +62,8 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
$this->dateFormat = $dateFormat;
$this->timeFormat = $timeFormat;
$this->calendar = $calendar;
$this->pattern = $pattern;
}
/**
@ -145,11 +148,12 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
*/
protected function getIntlDateFormatter()
{
return new \IntlDateFormatter(
\Locale::getDefault(),
$this->dateFormat,
$this->timeFormat,
$this->outputTimezone
);
$dateFormat = $this->dateFormat;
$timeFormat = $this->timeFormat;
$timezone = $this->outputTimezone;
$calendar = $this->calendar;
$pattern = $this->pattern;
return new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, $timezone, $calendar, $pattern);
}
}
}

View File

@ -31,11 +31,13 @@ class DateType extends AbstractType
\Locale::getDefault(),
$options['format'],
\IntlDateFormatter::NONE,
\DateTimeZone::UTC
\DateTimeZone::UTC,
\IntlDateFormatter::GREGORIAN,
$options['pattern']
);
if ($options['widget'] === 'single_text') {
$builder->appendClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $options['format'], \IntlDateFormatter::NONE));
if ($options['widget'] === 'single-text') {
$builder->appendClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $options['format'], \IntlDateFormatter::NONE, \IntlDateFormatter::GREGORIAN, $formatter->getPattern()));
} else {
$yearOptions = $monthOptions = $dayOptions = array();
$widget = $options['widget'];
@ -91,19 +93,15 @@ class DateType extends AbstractType
$view->set('widget', $form->getAttribute('widget'));
if ($view->hasChildren()) {
$pattern = $form->getAttribute('formatter')->getPattern();
//if custom date_pattern is set then use it or else use the the formatter or the default pattern
if (!$pattern = $form->getAttribute('date_pattern')) {
$pattern = $form->getAttribute('formatter')->getPattern();
// set right order with respect to locale (e.g.: de_DE=dd.MM.yy; en_US=M/d/yy)
// lookup various formats at http://userguide.icu-project.org/formatparse/datetime
if (preg_match('/^([yMd]+).+([yMd]+).+([yMd]+)$/', $pattern)) {
$pattern = preg_replace(array('/y+/', '/M+/', '/d+/'), array('{{ year }}', '{{ month }}', '{{ day }}'), $pattern);
} else {
// default fallback
$pattern = '{{ year }}-{{ month }}-{{ day }}';
}
// set right order with respect to locale (e.g.: de_DE=dd.MM.yy; en_US=M/d/yy)
// lookup various formats at http://userguide.icu-project.org/formatparse/datetime
if (preg_match('/^([yMd]+).+([yMd]+).+([yMd]+)$/', $pattern)) {
$pattern = preg_replace(array('/y+/', '/M+/', '/d+/'), array('{{ year }}', '{{ month }}', '{{ day }}'), $pattern);
} else {
// default fallback
$pattern = '{{ year }}-{{ month }}-{{ day }}';
}
$view->set('date_pattern', $pattern);