[Form] The IntlDateFormatter pattern can now be passed via the format option

* Also changed the default value of the calendar paramter to \IntlDateFormatter:GREGORIAN
   in DateTimeToLocalizedStringTransformer which is the same as the default value in
   StubIntlDateFormatter
This commit is contained in:
Matthieu Vachon 2011-05-20 21:09:50 -04:00
parent 58f869a920
commit a8152326fb
2 changed files with 26 additions and 10 deletions

View File

@ -40,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, $calendar = null, $pattern = null)
public function __construct($inputTimezone = null, $outputTimezone = null, $dateFormat = null, $timeFormat = null, $calendar = \IntlDateFormatter::GREGORIAN, $pattern = null)
{
parent::__construct($inputTimezone, $outputTimezone);

View File

@ -27,17 +27,39 @@ class DateType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$format = $options['format'];
$pattern = null;
$allowedFormatOptionValues = array(
\IntlDateFormatter::FULL,
\IntlDateFormatter::LONG,
\IntlDateFormatter::MEDIUM,
\IntlDateFormatter::SHORT,
);
// If $format is not in the allowed options, it's considered as the pattern of the formatter if it is a string
if (!in_array($format, $allowedFormatOptionValues, true)) {
if (is_string($format)) {
$defaultOptions = $this->getDefaultOptions($options);
$format = $defaultOptions['format'];
$pattern = $options['format'];
} else {
throw new FormException('The "format" option must be one of the IntlDateFormatter constants (FULL, LONG, MEDIUM, SHORT) or a string representing a custom pattern');
}
}
$formatter = new \IntlDateFormatter(
\Locale::getDefault(),
$options['format'],
$format,
\IntlDateFormatter::NONE,
\DateTimeZone::UTC,
\IntlDateFormatter::GREGORIAN,
$options['pattern']
$pattern
);
if ($options['widget'] === 'single-text') {
$builder->appendClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $options['format'], \IntlDateFormatter::NONE, \IntlDateFormatter::GREGORIAN, $formatter->getPattern()));
$builder->appendClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $format, \IntlDateFormatter::NONE, \IntlDateFormatter::GREGORIAN, $pattern));
} else {
$yearOptions = $monthOptions = $dayOptions = array();
$widget = $options['widget'];
@ -139,12 +161,6 @@ class DateType extends AbstractType
'text',
'choice',
),
'format' => array(
\IntlDateFormatter::FULL,
\IntlDateFormatter::LONG,
\IntlDateFormatter::MEDIUM,
\IntlDateFormatter::SHORT,
),
);
}