DateCaster.php 4.6 KB

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