DateTimeFormatter.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Stdlib\DateTime;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Framework\Phrase;
  9. /**
  10. * {@inheritdoc}
  11. */
  12. class DateTimeFormatter implements DateTimeFormatterInterface
  13. {
  14. /**
  15. * @var bool
  16. */
  17. protected $useIntlFormatObject;
  18. /**
  19. * @var \Magento\Framework\Locale\ResolverInterface
  20. */
  21. private $localeResolver;
  22. /**
  23. * @param bool|null $useIntlFormatObject
  24. */
  25. public function __construct(
  26. $useIntlFormatObject = null
  27. ) {
  28. $this->useIntlFormatObject = (null === $useIntlFormatObject)
  29. ? !defined('HHVM_VERSION')
  30. : $useIntlFormatObject;
  31. }
  32. /**
  33. * Get locale resolver
  34. *
  35. * @return \Magento\Framework\Locale\ResolverInterface|mixed
  36. */
  37. private function getLocaleResolver()
  38. {
  39. if ($this->localeResolver === null) {
  40. $this->localeResolver = \Magento\Framework\App\ObjectManager::getInstance()->get(
  41. \Magento\Framework\Locale\ResolverInterface::class
  42. );
  43. }
  44. return $this->localeResolver;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function formatObject($object, $format = null, $locale = null)
  50. {
  51. $locale = (null === $locale) ? $this->getLocaleResolver()->getLocale() : $locale;
  52. if ($this->useIntlFormatObject) {
  53. return \IntlDateFormatter::formatObject($object, $format, $locale);
  54. }
  55. return $this->doFormatObject($object, $format, $locale);
  56. }
  57. /**
  58. * Implements what IntlDateFormatter::formatObject() is in PHP 5.5+
  59. *
  60. * @param \IntlCalendar|\DateTimeInterface $object
  61. * @param string|int|array|null $format
  62. * @param string|null $locale
  63. * @return string
  64. * @throws LocalizedException
  65. */
  66. protected function doFormatObject($object, $format = null, $locale = null)
  67. {
  68. $pattern = $dateFormat = $timeFormat = $calendar = null;
  69. if (is_array($format)) {
  70. list($dateFormat, $timeFormat) = $format;
  71. } elseif (is_numeric($format)) {
  72. $dateFormat = $format;
  73. } elseif (is_string($format) || null == $format) {
  74. $dateFormat = $timeFormat = \IntlDateFormatter::MEDIUM;
  75. $pattern = $format;
  76. } else {
  77. throw new LocalizedException(
  78. new Phrase('The format type is invalid. Verify the format type and try again.')
  79. );
  80. }
  81. $timezone = $object->getTimezone();
  82. if ($object instanceof \IntlCalendar) {
  83. $timezone = $timezone->toDateTimeZone();
  84. }
  85. $timezone = $timezone->getName();
  86. if ($timezone === '+00:00') {
  87. $timezone = 'UTC';
  88. } elseif ($timezone[0] === '+' || $timezone[0] === '-') { // $timezone[0] is first symbol of string
  89. $timezone = 'GMT' . $timezone;
  90. }
  91. return (new \IntlDateFormatter($locale, $dateFormat, $timeFormat, $timezone, $calendar, $pattern))
  92. ->format($object);
  93. }
  94. }