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 class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
{ {
private $dateFormat; private $dateFormat;
private $timeFormat; private $timeFormat;
private $pattern;
private $calendar;
/** /**
* Constructor. * Constructor.
@ -39,7 +40,7 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
* @throws UnexpectedTypeException If a format is not supported * @throws UnexpectedTypeException If a format is not supported
* @throws UnexpectedTypeException if a timezone is not a string * @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); parent::__construct($inputTimezone, $outputTimezone);
@ -61,6 +62,8 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
$this->dateFormat = $dateFormat; $this->dateFormat = $dateFormat;
$this->timeFormat = $timeFormat; $this->timeFormat = $timeFormat;
$this->calendar = $calendar;
$this->pattern = $pattern;
} }
/** /**
@ -145,11 +148,12 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
*/ */
protected function getIntlDateFormatter() protected function getIntlDateFormatter()
{ {
return new \IntlDateFormatter( $dateFormat = $this->dateFormat;
\Locale::getDefault(), $timeFormat = $this->timeFormat;
$this->dateFormat, $timezone = $this->outputTimezone;
$this->timeFormat, $calendar = $this->calendar;
$this->outputTimezone $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(), \Locale::getDefault(),
$options['format'], $options['format'],
\IntlDateFormatter::NONE, \IntlDateFormatter::NONE,
\DateTimeZone::UTC \DateTimeZone::UTC,
\IntlDateFormatter::GREGORIAN,
$options['pattern']
); );
if ($options['widget'] === 'single_text') { if ($options['widget'] === 'single-text') {
$builder->appendClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $options['format'], \IntlDateFormatter::NONE)); $builder->appendClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $options['format'], \IntlDateFormatter::NONE, \IntlDateFormatter::GREGORIAN, $formatter->getPattern()));
} else { } else {
$yearOptions = $monthOptions = $dayOptions = array(); $yearOptions = $monthOptions = $dayOptions = array();
$widget = $options['widget']; $widget = $options['widget'];
@ -91,9 +93,6 @@ class DateType extends AbstractType
$view->set('widget', $form->getAttribute('widget')); $view->set('widget', $form->getAttribute('widget'));
if ($view->hasChildren()) { if ($view->hasChildren()) {
//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(); $pattern = $form->getAttribute('formatter')->getPattern();
// set right order with respect to locale (e.g.: de_DE=dd.MM.yy; en_US=M/d/yy) // set right order with respect to locale (e.g.: de_DE=dd.MM.yy; en_US=M/d/yy)
@ -104,7 +103,6 @@ class DateType extends AbstractType
// default fallback // default fallback
$pattern = '{{ year }}-{{ month }}-{{ day }}'; $pattern = '{{ year }}-{{ month }}-{{ day }}';
} }
}
$view->set('date_pattern', $pattern); $view->set('date_pattern', $pattern);
} }