moved date_pattern to IntlDateFormatter
added code to use custom date_pattern
This commit is contained in:
parent
dd104bc8c0
commit
52a1e1d281
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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,19 +93,15 @@ class DateType extends AbstractType
|
|||||||
$view->set('widget', $form->getAttribute('widget'));
|
$view->set('widget', $form->getAttribute('widget'));
|
||||||
|
|
||||||
if ($view->hasChildren()) {
|
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
|
// set right order with respect to locale (e.g.: de_DE=dd.MM.yy; en_US=M/d/yy)
|
||||||
if (!$pattern = $form->getAttribute('date_pattern')) {
|
// lookup various formats at http://userguide.icu-project.org/formatparse/datetime
|
||||||
$pattern = $form->getAttribute('formatter')->getPattern();
|
if (preg_match('/^([yMd]+).+([yMd]+).+([yMd]+)$/', $pattern)) {
|
||||||
|
$pattern = preg_replace(array('/y+/', '/M+/', '/d+/'), array('{{ year }}', '{{ month }}', '{{ day }}'), $pattern);
|
||||||
// set right order with respect to locale (e.g.: de_DE=dd.MM.yy; en_US=M/d/yy)
|
} else {
|
||||||
// lookup various formats at http://userguide.icu-project.org/formatparse/datetime
|
// default fallback
|
||||||
if (preg_match('/^([yMd]+).+([yMd]+).+([yMd]+)$/', $pattern)) {
|
$pattern = '{{ year }}-{{ month }}-{{ day }}';
|
||||||
$pattern = preg_replace(array('/y+/', '/M+/', '/d+/'), array('{{ year }}', '{{ month }}', '{{ day }}'), $pattern);
|
|
||||||
} else {
|
|
||||||
// default fallback
|
|
||||||
$pattern = '{{ year }}-{{ month }}-{{ day }}';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$view->set('date_pattern', $pattern);
|
$view->set('date_pattern', $pattern);
|
||||||
|
Reference in New Issue
Block a user