MomentFormatConverter.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\CoreBundle\Date;
  11. /**
  12. * Handles Moment.js <-> PHP date format conversion.
  13. *
  14. * Inspired by https://github.com/fightbulc/moment.php/blob/master/src/Moment/CustomFormats/MomentJs.php
  15. *
  16. *
  17. * @author Hugo Briand <briand@ekino.com>
  18. * @author Andrej Hudec <pulzarraider@gmail.com>
  19. */
  20. class MomentFormatConverter
  21. {
  22. /**
  23. * @var array This defines the mapping between PHP ICU date format (key) and moment.js date format (value)
  24. * For ICU formats see http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax
  25. * For Moment formats see http://momentjs.com/docs/#/displaying/format/
  26. */
  27. private static $formatConvertRules = array(
  28. // year
  29. 'yyyy' => 'YYYY', 'yy' => 'YY', 'y' => 'YYYY',
  30. // month
  31. // 'MMMM'=>'MMMM', 'MMM'=>'MMM', 'MM'=>'MM',
  32. // day
  33. 'dd' => 'DD', 'd' => 'D',
  34. // hour
  35. // 'HH'=>'HH', 'H'=>'H', 'h'=>'h', 'hh'=>'hh',
  36. // am/pm
  37. // 'a' => 'a',
  38. // minute
  39. // 'mm'=>'mm', 'm'=>'m',
  40. // second
  41. // 'ss'=>'ss', 's'=>'s',
  42. // day of week
  43. 'EEEEEE' => 'dd', 'EEEE' => 'dddd', 'EE' => 'ddd',
  44. // timezone
  45. 'ZZZZZ' => 'Z', 'ZZZ' => 'ZZ',
  46. );
  47. /**
  48. * Returns associated moment.js format.
  49. *
  50. * @param $format PHP Date format
  51. *
  52. * @return string Moment.js date format
  53. */
  54. public function convert($format)
  55. {
  56. $size = strlen($format);
  57. $foundOne = false;
  58. $output = '';
  59. //process the format string letter by letter
  60. for ($i = 0; $i < $size; ++$i) {
  61. //if finds a '
  62. if ($format[$i] === "'") {
  63. //if the next character are T' forming 'T', send a T to the
  64. //output
  65. if ($format[$i + 1] === 'T' && $format[$i + 2] === '\'') {
  66. $output .= 'T';
  67. $i += 2;
  68. } else {
  69. //if it's no a 'T' then send whatever is inside the '' to
  70. //the output, but send it inside [] (useful for cases like
  71. //the brazilian translation that uses a 'de' in the date)
  72. $output .= '[';
  73. $temp = current(explode("'", substr($format, $i + 1)));
  74. $output .= $temp;
  75. $output .= ']';
  76. $i += strlen($temp) + 1;
  77. }
  78. } else {
  79. //if no ' is found, then search all the rules, see if any of
  80. //them matchs
  81. $foundOne = false;
  82. foreach (self::$formatConvertRules as $key => $value) {
  83. if (substr($format, $i, strlen($key)) === $key) {
  84. $output .= $value;
  85. $foundOne = true;
  86. $i += strlen($key) - 1;
  87. break;
  88. }
  89. }
  90. //if no rule is matched, then just add the character to the
  91. //output
  92. if (!$foundOne) {
  93. $output .= $format[$i];
  94. }
  95. }
  96. }
  97. return $output;
  98. }
  99. }