OpeningHoursMapper.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Rest\EntityMapper;
  6. use Temando\Shipping\Rest\Response\Fields\Location\OpeningHours;
  7. /**
  8. * Map API data to application data object
  9. *
  10. * @package Temando\Shipping\Rest
  11. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  12. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  13. * @link https://www.temando.com/
  14. */
  15. class OpeningHoursMapper
  16. {
  17. /**
  18. * Sort opening hours by day of week.
  19. *
  20. * Date formats:
  21. * [
  22. * "general => ["l" => ["from" => "H:i:sP", "to" => "H:i:sP"]],
  23. * "specific" => [
  24. * "openings" => [
  25. * ["description" => "some special opening", "from" => "c", "to" => "c"],
  26. * ["description" => "another special opening", "from" => "c", "to" => "c"],
  27. * ],
  28. * "closures" => [
  29. * ["description" => "some holiday", "from" => "c", "to" => "c"],
  30. * ["description" => "another special closure", "from" => "c", "to" => "c"],
  31. * ],
  32. * ]
  33. * }
  34. *
  35. * @param OpeningHours $apiHours
  36. * @return string[][]
  37. */
  38. public function map(OpeningHours $apiHours)
  39. {
  40. // general opening hours
  41. $openingHours = [];
  42. // specific opening hours
  43. $openingHoursSpecification = [
  44. 'openings' => [],
  45. 'closures' => [],
  46. ];
  47. foreach ($apiHours->getDefault() as $item) {
  48. $dow = $item->getDayOfWeek();
  49. if (!isset($openingHours[$dow])) {
  50. $openingHours[$dow] = [];
  51. }
  52. $openingHours[$dow][] = [
  53. 'from' => $item->getOpens(),
  54. 'to' => $item->getCloses(),
  55. ];
  56. }
  57. if ($apiHours->getExceptions()) {
  58. foreach ($apiHours->getExceptions()->getOpenings() as $opening) {
  59. $openingHoursSpecification['openings'][] = [
  60. 'description' => $opening->getDescription(),
  61. 'from' => $opening->getFrom(),
  62. 'to' => $opening->getTo(),
  63. ];
  64. }
  65. foreach ($apiHours->getExceptions()->getClosures() as $closure) {
  66. $openingHoursSpecification['closures'][] = [
  67. 'description' => $closure->getDescription(),
  68. 'from' => $closure->getFrom(),
  69. 'to' => $closure->getTo(),
  70. ];
  71. }
  72. }
  73. $openingHours = [
  74. 'general' => $openingHours,
  75. 'specific' => $openingHoursSpecification,
  76. ];
  77. return $openingHours;
  78. }
  79. }