DateFieldConfig.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Form\Config;
  11. use Symfony\Component\Form\FieldInterface;
  12. use Symfony\Component\Form\ChoiceList\PaddedChoiceList;
  13. use Symfony\Component\Form\ChoiceList\MonthChoiceList;
  14. use Symfony\Component\Form\Renderer\Plugin\DatePatternPlugin;
  15. use Symfony\Component\Form\ValueTransformer\DateTimeToLocalizedStringTransformer;
  16. use Symfony\Component\Form\ValueTransformer\DateTimeToArrayTransformer;
  17. use Symfony\Component\Form\ValueTransformer\DateTimeToStringTransformer;
  18. use Symfony\Component\Form\ValueTransformer\DateTimeToTimestampTransformer;
  19. use Symfony\Component\Form\ValueTransformer\ReversedTransformer;
  20. class DateFieldConfig extends AbstractFieldConfig
  21. {
  22. public function configure(FieldInterface $field, array $options)
  23. {
  24. $formatter = new \IntlDateFormatter(
  25. \Locale::getDefault(),
  26. $options['format'],
  27. \IntlDateFormatter::NONE
  28. );
  29. if ($options['widget'] === 'text') {
  30. $field->setValueTransformer(new DateTimeToLocalizedStringTransformer(array(
  31. 'date_format' => $options['format'],
  32. 'time_format' => \IntlDateFormatter::NONE,
  33. 'input_timezone' => $options['data_timezone'],
  34. 'output_timezone' => $options['user_timezone'],
  35. )));
  36. } else {
  37. // Only pass a subset of the options to children
  38. $yearOptions = array(
  39. 'choice_list' => new PaddedChoiceList(
  40. $options['years'], 4, '0', STR_PAD_LEFT
  41. ),
  42. );
  43. $monthOptions = array(
  44. 'choice_list' => new MonthChoiceList(
  45. $formatter, $options['months']
  46. ),
  47. );
  48. $dayOptions = array(
  49. 'choice_list' => new PaddedChoiceList(
  50. $options['days'], 2, '0', STR_PAD_LEFT
  51. ),
  52. );
  53. $field->add($this->getInstance('choice', 'year', $yearOptions))
  54. ->add($this->getInstance('choice', 'month', $monthOptions))
  55. ->add($this->getInstance('choice', 'day', $dayOptions))
  56. ->setValueTransformer(new DateTimeToArrayTransformer(array(
  57. 'input_timezone' => $options['data_timezone'],
  58. 'output_timezone' => $options['user_timezone'],
  59. 'fields' => array('year', 'month', 'day'),
  60. )))
  61. ->addRendererPlugin(new DatePatternPlugin($formatter))
  62. // Don't modify \DateTime classes by reference, we treat
  63. // them like immutable value objects
  64. ->setModifyByReference(false);
  65. }
  66. if ($options['type'] === 'string') {
  67. $field->setNormalizationTransformer(new ReversedTransformer(
  68. new DateTimeToStringTransformer(array(
  69. 'input_timezone' => $options['data_timezone'],
  70. 'output_timezone' => $options['data_timezone'],
  71. 'format' => 'Y-m-d',
  72. ))
  73. ));
  74. } else if ($options['type'] === 'timestamp') {
  75. $field->setNormalizationTransformer(new ReversedTransformer(
  76. new DateTimeToTimestampTransformer(array(
  77. 'output_timezone' => $options['data_timezone'],
  78. 'input_timezone' => $options['data_timezone'],
  79. ))
  80. ));
  81. } else if ($options['type'] === 'array') {
  82. $field->setNormalizationTransformer(new ReversedTransformer(
  83. new DateTimeToArrayTransformer(array(
  84. 'input_timezone' => $options['data_timezone'],
  85. 'output_timezone' => $options['data_timezone'],
  86. 'fields' => array('year', 'month', 'day'),
  87. ))
  88. ));
  89. }
  90. $field->setRendererVar('widget', $options['widget']);
  91. }
  92. public function getDefaultOptions(array $options)
  93. {
  94. return array(
  95. 'template' => 'date',
  96. 'years' => range(date('Y') - 5, date('Y') + 5),
  97. 'months' => range(1, 12),
  98. 'days' => range(1, 31),
  99. 'widget' => 'choice',
  100. 'type' => 'datetime',
  101. 'pattern' => null,
  102. 'format' => \IntlDateFormatter::MEDIUM,
  103. 'data_timezone' => date_default_timezone_get(),
  104. 'user_timezone' => date_default_timezone_get(),
  105. 'csrf_protection' => false,
  106. );
  107. }
  108. public function getParent(array $options)
  109. {
  110. return $options['widget'] === 'text' ? 'field' : 'form';
  111. }
  112. public function getIdentifier()
  113. {
  114. return 'date';
  115. }
  116. }