OrderDate.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\ViewModel\DataProvider;
  6. use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
  7. use Magento\Framework\View\Element\Text;
  8. use Magento\Store\Model\ScopeInterface;
  9. /**
  10. * Order Info Date Formatter
  11. *
  12. * @package Temando\Shipping\ViewModel
  13. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  14. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  15. * @link https://www.temando.com/
  16. */
  17. class OrderDate implements OrderDateInterface
  18. {
  19. /**
  20. * @var TimezoneInterface
  21. */
  22. private $date;
  23. /**
  24. * @var Text
  25. */
  26. private $block;
  27. /**
  28. * OrderDate constructor.
  29. * @param TimezoneInterface $date
  30. * @param Text $block
  31. */
  32. public function __construct(TimezoneInterface $date, Text $block)
  33. {
  34. $this->date = $date;
  35. $this->block = $block;
  36. }
  37. /**
  38. * Create DateTime object with given date in the current locale.
  39. *
  40. * @param string $date
  41. * @return \DateTime
  42. */
  43. public function getDate(string $date): \DateTime
  44. {
  45. $localizedDate = $this->date->date(new \DateTime($date));
  46. return $localizedDate;
  47. }
  48. /**
  49. * Format given date in the locale configured for given store.
  50. *
  51. * @param string $date
  52. * @param int $storeId
  53. * @return string
  54. */
  55. public function getStoreDate(string $date, int $storeId): string
  56. {
  57. if (empty($date)) {
  58. return '';
  59. }
  60. $timezone = $this->date->getConfigTimezone(ScopeInterface::SCOPE_STORE, $storeId);
  61. $storeDate = $this->block->formatDate(
  62. $date,
  63. \IntlDateFormatter::MEDIUM,
  64. true,
  65. $timezone
  66. );
  67. return $storeDate;
  68. }
  69. /**
  70. * Format given date in the current locale.
  71. *
  72. * @param string $date
  73. * @return string
  74. */
  75. public function getAdminDate(string $date): string
  76. {
  77. if (empty($date)) {
  78. return '';
  79. }
  80. $localizedDate = $this->getDate($date);
  81. $adminDate = $this->block->formatDate(
  82. $localizedDate,
  83. \IntlDateFormatter::MEDIUM,
  84. true
  85. );
  86. return $adminDate;
  87. }
  88. /**
  89. * @param int $storeId
  90. * @return string
  91. */
  92. public function getStoreTimezone(int $storeId): string
  93. {
  94. return $this->date->getConfigTimezone(ScopeInterface::SCOPE_STORE, $storeId);
  95. }
  96. }