DateCaster.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\VarDumper\Caster;
  11. use Symfony\Component\VarDumper\Cloner\Stub;
  12. /**
  13. * Casts DateTimeInterface related classes to array representation.
  14. *
  15. * @author Dany Maillard <danymaillard93b@gmail.com>
  16. */
  17. class DateCaster
  18. {
  19. public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub, $isNested, $filter)
  20. {
  21. $prefix = Caster::PREFIX_VIRTUAL;
  22. $location = $d->getTimezone()->getLocation();
  23. $fromNow = (new \DateTime())->diff($d);
  24. $title = $d->format('l, F j, Y')
  25. ."\n".self::formatInterval($fromNow).' from now'
  26. .($location ? ($d->format('I') ? "\nDST On" : "\nDST Off") : '')
  27. ;
  28. $a = array();
  29. $a[$prefix.'date'] = new ConstStub(self::formatDateTime($d, $location ? ' e (P)' : ' P'), $title);
  30. $stub->class .= $d->format(' @U');
  31. return $a;
  32. }
  33. public static function castInterval(\DateInterval $interval, array $a, Stub $stub, $isNested, $filter)
  34. {
  35. $now = new \DateTimeImmutable();
  36. $numberOfSeconds = $now->add($interval)->getTimestamp() - $now->getTimestamp();
  37. $title = number_format($numberOfSeconds, 0, '.', ' ').'s';
  38. $i = array(Caster::PREFIX_VIRTUAL.'interval' => new ConstStub(self::formatInterval($interval), $title));
  39. return $filter & Caster::EXCLUDE_VERBOSE ? $i : $i + $a;
  40. }
  41. private static function formatInterval(\DateInterval $i)
  42. {
  43. $format = '%R ';
  44. if (0 === $i->y && 0 === $i->m && ($i->h >= 24 || $i->i >= 60 || $i->s >= 60)) {
  45. $i = date_diff($d = new \DateTime(), date_add(clone $d, $i)); // recalculate carry over points
  46. $format .= 0 < $i->days ? '%ad ' : '';
  47. } else {
  48. $format .= ($i->y ? '%yy ' : '').($i->m ? '%mm ' : '').($i->d ? '%dd ' : '');
  49. }
  50. if (\PHP_VERSION_ID >= 70100 && isset($i->f)) {
  51. $format .= $i->h || $i->i || $i->s || $i->f ? '%H:%I:'.self::formatSeconds($i->s, substr($i->f, 2)) : '';
  52. } else {
  53. $format .= $i->h || $i->i || $i->s ? '%H:%I:%S' : '';
  54. }
  55. $format = '%R ' === $format ? '0s' : $format;
  56. return $i->format(rtrim($format));
  57. }
  58. public static function castTimeZone(\DateTimeZone $timeZone, array $a, Stub $stub, $isNested, $filter)
  59. {
  60. $location = $timeZone->getLocation();
  61. $formatted = (new \DateTime('now', $timeZone))->format($location ? 'e (P)' : 'P');
  62. $title = $location && \extension_loaded('intl') ? \Locale::getDisplayRegion('-'.$location['country_code'], \Locale::getDefault()) : '';
  63. $z = array(Caster::PREFIX_VIRTUAL.'timezone' => new ConstStub($formatted, $title));
  64. return $filter & Caster::EXCLUDE_VERBOSE ? $z : $z + $a;
  65. }
  66. public static function castPeriod(\DatePeriod $p, array $a, Stub $stub, $isNested, $filter)
  67. {
  68. if (\defined('HHVM_VERSION_ID') || \PHP_VERSION_ID < 50620 || (\PHP_VERSION_ID >= 70000 && \PHP_VERSION_ID < 70005)) { // see https://bugs.php.net/bug.php?id=71635
  69. return $a;
  70. }
  71. $dates = array();
  72. if (\PHP_VERSION_ID >= 70107) { // see https://bugs.php.net/bug.php?id=74639
  73. foreach (clone $p as $i => $d) {
  74. if (3 === $i) {
  75. $now = new \DateTimeImmutable();
  76. $dates[] = sprintf('%s more', ($end = $p->getEndDate())
  77. ? ceil(($end->format('U.u') - $d->format('U.u')) / ($now->add($p->getDateInterval())->format('U.u') - $now->format('U.u')))
  78. : $p->recurrences - $i
  79. );
  80. break;
  81. }
  82. $dates[] = sprintf('%s) %s', $i + 1, self::formatDateTime($d));
  83. }
  84. }
  85. $period = sprintf(
  86. 'every %s, from %s (%s) %s',
  87. self::formatInterval($p->getDateInterval()),
  88. self::formatDateTime($p->getStartDate()),
  89. $p->include_start_date ? 'included' : 'excluded',
  90. ($end = $p->getEndDate()) ? 'to '.self::formatDateTime($end) : 'recurring '.$p->recurrences.' time/s'
  91. );
  92. $p = array(Caster::PREFIX_VIRTUAL.'period' => new ConstStub($period, implode("\n", $dates)));
  93. return $filter & Caster::EXCLUDE_VERBOSE ? $p : $p + $a;
  94. }
  95. private static function formatDateTime(\DateTimeInterface $d, $extra = '')
  96. {
  97. return $d->format('Y-m-d H:i:'.self::formatSeconds($d->format('s'), $d->format('u')).$extra);
  98. }
  99. private static function formatSeconds($s, $us)
  100. {
  101. return sprintf('%02d.%s', $s, 0 === ($len = \strlen($t = rtrim($us, '0'))) ? '0' : ($len <= 3 ? str_pad($t, 3, '0') : $us));
  102. }
  103. }