DatePatternPlugin.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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\Renderer\Plugin;
  11. use Symfony\Component\Form\Renderer\RendererInterface;
  12. class DatePatternPlugin implements PluginInterface
  13. {
  14. private $formatter;
  15. public function __construct(\IntlDateFormatter $formatter)
  16. {
  17. $this->formatter = $formatter;
  18. }
  19. public function setUp(RendererInterface $renderer)
  20. {
  21. $renderer->setParameter('date_pattern', $this->getPattern());
  22. }
  23. public function getPattern()
  24. {
  25. // // set order as specified in the pattern
  26. // if ($this->getOption('pattern')) {
  27. // return $this->getOption('pattern');
  28. // }
  29. $pattern = $this->formatter->getPattern();
  30. // set right order with respect to locale (e.g.: de_DE=dd.MM.yy; en_US=M/d/yy)
  31. // lookup various formats at http://userguide.icu-project.org/formatparse/datetime
  32. if (preg_match('/^([yMd]+).+([yMd]+).+([yMd]+)$/', $pattern)) {
  33. return preg_replace(array('/y+/', '/M+/', '/d+/'), array('{{ year }}', '{{ month }}', '{{ day }}'), $pattern);
  34. }
  35. // default fallback
  36. return '{{ year }}-{{ month }}-{{ day }}';
  37. }
  38. }