2011-02-19 12:52:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Form;
|
|
|
|
|
2011-02-19 16:05:14 +00:00
|
|
|
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
|
2011-02-19 14:26:07 +00:00
|
|
|
use Symfony\Component\Form\ChoiceList\DefaultChoiceList;
|
2011-02-19 16:05:14 +00:00
|
|
|
use Symfony\Component\Form\ChoiceList\PaddedChoiceList;
|
|
|
|
use Symfony\Component\Form\ChoiceList\MonthChoiceList;
|
2011-02-19 14:26:07 +00:00
|
|
|
use Symfony\Component\Form\DataProcessor\RadioToArrayConverter;
|
2011-02-19 12:52:37 +00:00
|
|
|
use Symfony\Component\Form\Renderer\DefaultRenderer;
|
2011-02-19 14:26:07 +00:00
|
|
|
use Symfony\Component\Form\Renderer\Theme\ThemeInterface;
|
2011-02-19 12:52:37 +00:00
|
|
|
use Symfony\Component\Form\Renderer\Plugin\IdPlugin;
|
|
|
|
use Symfony\Component\Form\Renderer\Plugin\NamePlugin;
|
2011-02-19 16:05:14 +00:00
|
|
|
use Symfony\Component\Form\Renderer\Plugin\ParameterPlugin;
|
2011-02-19 14:26:07 +00:00
|
|
|
use Symfony\Component\Form\Renderer\Plugin\ChoicePlugin;
|
|
|
|
use Symfony\Component\Form\Renderer\Plugin\ParentNamePlugin;
|
2011-02-19 16:05:14 +00:00
|
|
|
use Symfony\Component\Form\Renderer\Plugin\DatePatternPlugin;
|
2011-02-21 16:41:09 +00:00
|
|
|
use Symfony\Component\Form\Renderer\Plugin\MoneyPatternPlugin;
|
|
|
|
use Symfony\Component\Form\ValueTransformer\BooleanToStringTransformer;
|
|
|
|
use Symfony\Component\Form\ValueTransformer\NumberToLocalizedStringTransformer;
|
|
|
|
use Symfony\Component\Form\ValueTransformer\IntegerToLocalizedStringTransformer;
|
|
|
|
use Symfony\Component\Form\ValueTransformer\MoneyToLocalizedStringTransformer;
|
2011-02-19 14:26:07 +00:00
|
|
|
use Symfony\Component\Form\ValueTransformer\ScalarToChoicesTransformer;
|
2011-02-19 16:05:14 +00:00
|
|
|
use Symfony\Component\Form\ValueTransformer\DateTimeToArrayTransformer;
|
2011-02-19 12:52:37 +00:00
|
|
|
|
|
|
|
class FormFactory
|
|
|
|
{
|
|
|
|
private $theme;
|
|
|
|
|
2011-02-19 14:26:07 +00:00
|
|
|
public function __construct(ThemeInterface $theme)
|
|
|
|
{
|
|
|
|
$this->setTheme($theme);
|
|
|
|
}
|
|
|
|
|
2011-02-19 12:52:37 +00:00
|
|
|
public function setTheme(ThemeInterface $theme)
|
|
|
|
{
|
|
|
|
$this->theme = $theme;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTheme()
|
|
|
|
{
|
|
|
|
return $this->theme;
|
|
|
|
}
|
|
|
|
|
2011-02-19 14:26:07 +00:00
|
|
|
protected function getField($key, $template)
|
|
|
|
{
|
|
|
|
$field = new Field($key);
|
|
|
|
|
|
|
|
return $field
|
|
|
|
->setRenderer(new DefaultRenderer($this->theme, $template))
|
|
|
|
->addRendererPlugin(new IdPlugin($field))
|
2011-02-21 17:16:40 +00:00
|
|
|
->addRendererPlugin(new NamePlugin($field))
|
|
|
|
->setRendererVar('field', $field)
|
|
|
|
->setRendererVar('class', null)
|
|
|
|
->setRendererVar('max_length', null)
|
|
|
|
->setRendererVar('size', null)
|
|
|
|
->setRendererVar('label', ucfirst(strtolower(str_replace('_', ' ', $key))));
|
2011-02-19 14:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getForm($key, $template)
|
2011-02-19 12:52:37 +00:00
|
|
|
{
|
2011-02-21 17:16:40 +00:00
|
|
|
$form = new Form($key);
|
2011-02-19 12:52:37 +00:00
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
return $form
|
2011-02-19 12:52:37 +00:00
|
|
|
->setRenderer(new DefaultRenderer($this->theme, $template))
|
2011-02-21 17:16:40 +00:00
|
|
|
->addRendererPlugin(new IdPlugin($form))
|
|
|
|
->addRendererPlugin(new NamePlugin($form))
|
|
|
|
->setRendererVar('field', $form)
|
|
|
|
->setRendererVar('class', null)
|
|
|
|
->setRendererVar('label', ucfirst(strtolower(str_replace('_', ' ', $key))));
|
2011-02-19 12:52:37 +00:00
|
|
|
}
|
|
|
|
|
2011-02-19 16:30:19 +00:00
|
|
|
public function getTextField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'max_length' => null,
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
return $this->getField($key, 'text')
|
2011-02-21 17:16:40 +00:00
|
|
|
->setRendererVar('max_length', $options['max_length']);
|
2011-02-19 16:30:19 +00:00
|
|
|
}
|
|
|
|
|
2011-02-21 16:41:09 +00:00
|
|
|
public function getHiddenField($key, array $options = array())
|
|
|
|
{
|
|
|
|
return $this->getField($key, 'hidden')
|
|
|
|
->setHidden(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNumberField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
// default precision is locale specific (usually around 3)
|
|
|
|
'precision' => null,
|
|
|
|
'grouping' => false,
|
|
|
|
'rounding_mode' => NumberToLocalizedStringTransformer::ROUND_HALFUP,
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
return $this->getField($key, 'number')
|
|
|
|
->setValueTransformer(new NumberToLocalizedStringTransformer(array(
|
|
|
|
'precision' => $options['precision'],
|
|
|
|
'grouping' => $options['grouping'],
|
|
|
|
'rounding-mode' => $options['rounding_mode'],
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIntegerField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
// default precision is locale specific (usually around 3)
|
|
|
|
'precision' => null,
|
|
|
|
'grouping' => false,
|
|
|
|
// Integer cast rounds towards 0, so do the same when displaying fractions
|
|
|
|
'rounding_mode' => IntegerToLocalizedStringTransformer::ROUND_DOWN,
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
return $this->getField($key, 'integer')
|
|
|
|
->setValueTransformer(new IntegerToLocalizedStringTransformer(array(
|
|
|
|
'precision' => $options['precision'],
|
|
|
|
'grouping' => $options['grouping'],
|
|
|
|
'rounding-mode' => $options['rounding_mode'],
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMoneyField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'precision' => 2,
|
|
|
|
'grouping' => false,
|
|
|
|
'divisor' => 1,
|
|
|
|
'currency' => 'EUR',
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
return $this->getField($key, 'money')
|
|
|
|
->setValueTransformer(new MoneyToLocalizedStringTransformer(array(
|
|
|
|
'precision' => $options['precision'],
|
|
|
|
'grouping' => $options['grouping'],
|
|
|
|
'divisor' => $options['divisor'],
|
|
|
|
)))
|
|
|
|
->addRendererPlugin(new MoneyPatternPlugin($options['currency']));
|
|
|
|
}
|
|
|
|
|
2011-02-19 14:26:07 +00:00
|
|
|
public function getCheckboxField($key, array $options = array())
|
2011-02-19 12:52:37 +00:00
|
|
|
{
|
2011-02-19 16:05:14 +00:00
|
|
|
$options = array_merge(array(
|
2011-02-19 14:26:07 +00:00
|
|
|
'value' => '1',
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
return $this->getField($key, 'checkbox')
|
2011-02-19 12:52:37 +00:00
|
|
|
->setValueTransformer(new BooleanToStringTransformer())
|
2011-02-21 17:16:40 +00:00
|
|
|
->setRendererVar('value', $options['value']);
|
2011-02-19 12:52:37 +00:00
|
|
|
}
|
|
|
|
|
2011-02-19 14:26:07 +00:00
|
|
|
public function getRadioField($key, array $options = array())
|
2011-02-19 12:52:37 +00:00
|
|
|
{
|
2011-02-19 16:05:14 +00:00
|
|
|
$options = array_merge(array(
|
2011-02-19 14:26:07 +00:00
|
|
|
'value' => null,
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
$field = $this->getField($key, 'radio');
|
2011-02-19 12:52:37 +00:00
|
|
|
|
|
|
|
return $field
|
|
|
|
->setValueTransformer(new BooleanToStringTransformer())
|
2011-02-19 14:26:07 +00:00
|
|
|
->addRendererPlugin(new ParentNamePlugin($field))
|
2011-02-21 17:16:40 +00:00
|
|
|
->setRendererVar('value', $options['value']);
|
2011-02-19 14:26:07 +00:00
|
|
|
}
|
|
|
|
|
2011-02-19 16:05:14 +00:00
|
|
|
protected function getChoiceFieldForList($key, ChoiceListInterface $choiceList, array $options = array())
|
2011-02-19 14:26:07 +00:00
|
|
|
{
|
2011-02-19 16:05:14 +00:00
|
|
|
$options = array_merge(array(
|
2011-02-19 14:26:07 +00:00
|
|
|
'multiple' => false,
|
|
|
|
'expanded' => false,
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
if (!$options['expanded']) {
|
|
|
|
$field = $this->getField($key, 'choice');
|
|
|
|
} else {
|
2011-02-19 16:05:14 +00:00
|
|
|
$field = $this->getForm($key, 'choice');
|
2011-02-19 14:26:07 +00:00
|
|
|
$choices = array_merge($choiceList->getPreferredChoices(), $choiceList->getOtherChoices());
|
|
|
|
|
|
|
|
foreach ($choices as $choice => $value) {
|
|
|
|
if ($options['multiple']) {
|
|
|
|
$field->add($this->getCheckboxField($choice, array(
|
|
|
|
'value' => $choice,
|
|
|
|
)));
|
|
|
|
} else {
|
|
|
|
$field->add($this->getRadioField($choice, array(
|
|
|
|
'value' => $choice,
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-19 16:05:14 +00:00
|
|
|
$field->addRendererPlugin(new ChoicePlugin($choiceList))
|
2011-02-21 17:16:40 +00:00
|
|
|
->setRendererVar('multiple', $options['multiple'])
|
|
|
|
->setRendererVar('expanded', $options['expanded']);
|
2011-02-19 14:26:07 +00:00
|
|
|
|
|
|
|
if ($options['multiple'] && $options['expanded']) {
|
|
|
|
$field->setValueTransformer(new ArrayToChoicesTransformer($choiceList));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$options['multiple'] && $options['expanded']) {
|
|
|
|
$field->setValueTransformer(new ScalarToChoicesTransformer($choiceList));
|
|
|
|
$field->setDataPreprocessor(new RadioToArrayConverter());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($options['multiple'] && !$options['expanded']) {
|
|
|
|
$field->addRendererPlugin(new SelectMultipleNamePlugin($field));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $field;
|
2011-02-19 12:52:37 +00:00
|
|
|
}
|
2011-02-19 16:05:14 +00:00
|
|
|
|
|
|
|
public function getChoiceField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'choices' => array(),
|
|
|
|
'preferred_choices' => array(),
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
$choiceList = new DefaultChoiceList(
|
|
|
|
$options['choices'],
|
|
|
|
$options['preferred_choices']
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->getChoiceFieldForList($key, $choiceList, $options);
|
|
|
|
}
|
|
|
|
|
2011-02-19 16:35:59 +00:00
|
|
|
public function getCountryField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'choices' => Locale::getDisplayCountries(\Locale::getDefault()),
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
return $this->getChoiceField($key, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLanguageField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'choices' => Locale::getDisplayLanguages(\Locale::getDefault()),
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
return $this->getChoiceField($key, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLocaleField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'choices' => Locale::getDisplayLocales(\Locale::getDefault()),
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
return $this->getChoiceField($key, $options);
|
|
|
|
}
|
|
|
|
|
2011-02-19 16:05:14 +00:00
|
|
|
protected function getDayField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'days' => range(1, 31),
|
|
|
|
'preferred_choices' => array(),
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
$choiceList = new PaddedChoiceList(
|
|
|
|
$options['days'], 2, '0', STR_PAD_LEFT, $options['preferred_choices']
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->getChoiceFieldForList($key, $choiceList, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getMonthField($key, \IntlDateFormatter $formatter, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'months' => range(1, 12),
|
|
|
|
'preferred_choices' => array(),
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
$choiceList = new MonthChoiceList(
|
|
|
|
$formatter, $options['months'], $options['preferred_choices']
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->getChoiceFieldForList($key, $choiceList, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getYearField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'years' => range(date('Y') - 5, date('Y') + 5),
|
|
|
|
'preferred_choices' => array(),
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
$choiceList = new PaddedChoiceList(
|
|
|
|
$options['years'], 4, '0', STR_PAD_LEFT, $options['preferred_choices']
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->getChoiceFieldForList($key, $choiceList, $options);
|
|
|
|
}
|
|
|
|
|
2011-02-19 16:30:19 +00:00
|
|
|
protected function getHourField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'widget' => 'choice',
|
|
|
|
'hours' => range(0, 23),
|
|
|
|
'preferred_choices' => array(),
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
if ($options['widget'] == 'text') {
|
|
|
|
return $this->getTextField($key, array('max_length' => 2));
|
|
|
|
} else {
|
|
|
|
$choiceList = new PaddedChoiceList(
|
|
|
|
$options['hours'], 2, '0', STR_PAD_LEFT, $options['preferred_choices']
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->getChoiceFieldForList($key, $choiceList, $options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getMinuteField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'widget' => 'choice',
|
|
|
|
'minutes' => range(0, 59),
|
|
|
|
'preferred_choices' => array(),
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
if ($options['widget'] == 'text') {
|
|
|
|
return $this->getTextField($key, array('max_length' => 2));
|
|
|
|
} else {
|
|
|
|
$choiceList = new PaddedChoiceList(
|
|
|
|
$options['minutes'], 2, '0', STR_PAD_LEFT, $options['preferred_choices']
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->getChoiceFieldForList($key, $choiceList, $options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getSecondField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'widget' => 'choice',
|
|
|
|
'seconds' => range(0, 59),
|
|
|
|
'preferred_choices' => array(),
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
if ($options['widget'] == 'text') {
|
|
|
|
return $this->getTextField($key, array('max_length' => 2));
|
|
|
|
} else {
|
|
|
|
$choiceList = new PaddedChoiceList(
|
|
|
|
$options['seconds'], 2, '0', STR_PAD_LEFT, $options['preferred_choices']
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->getChoiceFieldForList($key, $choiceList, $options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-19 16:05:14 +00:00
|
|
|
public function getDateField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'widget' => 'choice',
|
|
|
|
'type' => 'datetime',
|
|
|
|
'pattern' => null,
|
|
|
|
'format' => \IntlDateFormatter::MEDIUM,
|
|
|
|
'data_timezone' => date_default_timezone_get(),
|
|
|
|
'user_timezone' => date_default_timezone_get(),
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
$formatter = new \IntlDateFormatter(
|
|
|
|
\Locale::getDefault(),
|
|
|
|
$options['format'],
|
|
|
|
\IntlDateFormatter::NONE
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($options['widget'] === 'text') {
|
|
|
|
$field = $this->getField($key, 'date')
|
|
|
|
->setValueTransformer(new DateTimeToLocalizedStringTransformer(array(
|
|
|
|
'date_format' => $options['format'],
|
|
|
|
'time_format' => DateTimeToLocalizedStringTransformer::NONE,
|
|
|
|
'input_timezone' => $options['data_timezone'],
|
|
|
|
'output_timezone' => $options['user_timezone'],
|
2011-02-19 16:30:19 +00:00
|
|
|
)));
|
2011-02-19 16:05:14 +00:00
|
|
|
} else {
|
|
|
|
$field = $this->getForm($key, 'date')
|
|
|
|
->add($this->getYearField('year', $options))
|
|
|
|
->add($this->getMonthField('month', $formatter, $options))
|
|
|
|
->add($this->getDayField('day', $options))
|
|
|
|
->setValueTransformer(new DateTimeToArrayTransformer(array(
|
|
|
|
'input_timezone' => $options['data_timezone'],
|
|
|
|
'output_timezone' => $options['user_timezone'],
|
|
|
|
)))
|
|
|
|
->addRendererPlugin(new DatePatternPlugin($formatter))
|
|
|
|
// Don't modify \DateTime classes by reference, we treat
|
|
|
|
// them like immutable value objects
|
|
|
|
->setModifyByReference(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($options['type'] === 'string') {
|
|
|
|
$field->setNormalizationTransformer(new ReversedTransformer(
|
|
|
|
new DateTimeToStringTransformer(array(
|
|
|
|
'input_timezone' => $options['data_timezone'],
|
|
|
|
'output_timezone' => $options['data_timezone'],
|
|
|
|
'format' => 'Y-m-d',
|
|
|
|
))
|
|
|
|
));
|
|
|
|
} else if ($options['type'] === 'timestamp') {
|
|
|
|
$field->setNormalizationTransformer(new ReversedTransformer(
|
|
|
|
new DateTimeToTimestampTransformer(array(
|
|
|
|
'output_timezone' => $options['data_timezone'],
|
|
|
|
'input_timezone' => $options['data_timezone'],
|
|
|
|
))
|
|
|
|
));
|
|
|
|
} else if ($options['type'] === 'raw') {
|
|
|
|
$field->setNormalizationTransformer(new ReversedTransformer(
|
|
|
|
new DateTimeToArrayTransformer(array(
|
|
|
|
'input_timezone' => $options['data_timezone'],
|
|
|
|
'output_timezone' => $options['data_timezone'],
|
|
|
|
'fields' => array('year', 'month', 'day'),
|
|
|
|
))
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
$field->setRendererVar('widget', $options['widget']);
|
2011-02-19 16:30:19 +00:00
|
|
|
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
2011-02-19 16:35:59 +00:00
|
|
|
public function getBirthdayField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'years' => range($currentYear-120, $currentYear),
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
return $this->getDateField($key, $options);
|
|
|
|
}
|
|
|
|
|
2011-02-19 16:30:19 +00:00
|
|
|
public function getTimeField($key, array $options = array())
|
|
|
|
{
|
|
|
|
$options = array_merge(array(
|
|
|
|
'widget' => 'choice',
|
|
|
|
'type' => 'datetime',
|
|
|
|
'with_seconds' => false,
|
|
|
|
'pattern' => null,
|
|
|
|
'data_timezone' => date_default_timezone_get(),
|
|
|
|
'user_timezone' => date_default_timezone_get(),
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
$children = array('hour', 'minute');
|
|
|
|
$field = $this->getForm($key, 'time')
|
|
|
|
->add($this->getHourField('hour', $options))
|
|
|
|
->add($this->getMinuteField('minute', $options))
|
|
|
|
// Don't modify \DateTime classes by reference, we treat
|
|
|
|
// them like immutable value objects
|
|
|
|
->setModifyByReference(false);
|
|
|
|
|
|
|
|
if ($options['with_seconds']) {
|
|
|
|
$children[] = 'second';
|
|
|
|
$field->add($this->getSecondField('second', $options));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($options['type'] == 'string') {
|
|
|
|
$field->setNormalizationTransformer(new ReversedTransformer(
|
|
|
|
new DateTimeToStringTransformer(array(
|
|
|
|
'format' => 'H:i:s',
|
|
|
|
'input_timezone' => $options['data_timezone'],
|
|
|
|
'output_timezone' => $options['data_timezone'],
|
|
|
|
))
|
|
|
|
));
|
|
|
|
} else if ($options['type'] == 'timestamp') {
|
|
|
|
$field->setNormalizationTransformer(new ReversedTransformer(
|
|
|
|
new DateTimeToTimestampTransformer(array(
|
|
|
|
'input_timezone' => $options['data_timezone'],
|
|
|
|
'output_timezone' => $options['data_timezone'],
|
|
|
|
))
|
|
|
|
));
|
|
|
|
} else if ($options['type'] === 'raw') {
|
|
|
|
$field->setNormalizationTransformer(new ReversedTransformer(
|
|
|
|
new DateTimeToArrayTransformer(array(
|
|
|
|
'input_timezone' => $options['data_timezone'],
|
|
|
|
'output_timezone' => $options['data_timezone'],
|
|
|
|
'fields' => $children,
|
|
|
|
))
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
$field
|
|
|
|
->setValueTransformer(new DateTimeToArrayTransformer(array(
|
|
|
|
'input_timezone' => $options['data_timezone'],
|
|
|
|
'output_timezone' => $options['user_timezone'],
|
|
|
|
// if the field is rendered as choice field, the values should be trimmed
|
|
|
|
// of trailing zeros to render the selected choices correctly
|
|
|
|
'pad' => $options['widget'] === 'text',
|
|
|
|
'fields' => $children,
|
|
|
|
)))
|
2011-02-21 17:16:40 +00:00
|
|
|
->setRendererVar('widget', $options['widget'])
|
|
|
|
->setRendererVar('with_seconds', $options['with_seconds']);
|
2011-02-19 16:30:19 +00:00
|
|
|
|
2011-02-19 16:05:14 +00:00
|
|
|
return $field;
|
|
|
|
}
|
2011-02-19 12:52:37 +00:00
|
|
|
}
|