You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OpeningSolver.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Lc\CaracoleBundle\Solver\Section;
  3. use Lc\CaracoleBundle\Model\Section\OpeningInterface;
  4. class OpeningSolver
  5. {
  6. public function getNextOpeningOfOpening(\DateTime $date, array $openings): ?OpeningInterface
  7. {
  8. return $this->getNextOpening($date, $openings, 'opening');
  9. }
  10. public function getNextOpeningOfClosing(\DateTime $date, array $openings): ?OpeningInterface
  11. {
  12. return $this->getNextOpening($date, $openings, 'closing');
  13. }
  14. protected function getNextOpening(\DateTime $date, array $openings, $typeOpening = 'opening')
  15. {
  16. if ($typeOpening == 'opening') {
  17. $methodTestDay = 'isOpeningDay';
  18. } else {
  19. $methodTestDay = 'isClosingDay';
  20. }
  21. $count = 0;
  22. $isClosingDay = false;
  23. do {
  24. if ($count) {
  25. $date->modify('+1 day');
  26. }
  27. $weekDay = $date->format('N');
  28. $isClosingDay = $this->$methodTestDay($weekDay, $openings);
  29. $count++;
  30. } while (!$isClosingDay && $count <= 7);
  31. if ($isClosingDay) {
  32. return $this->getOpeningByWeekday($weekDay, $openings);
  33. }
  34. return null;
  35. }
  36. public function isOpeningDay(int $weekDay, array $openings): bool
  37. {
  38. return $this->isOpeningOrClosingDay($weekDay, $openings, 'opening');
  39. }
  40. public function isClosingDay(int $weekDay, array $openings): bool
  41. {
  42. return $this->isOpeningOrClosingDay($weekDay, $openings, 'closing');
  43. }
  44. protected function isOpeningOrClosingDay(int $weekDay, array $openings, string $testOpeningOrClosing = 'opening')
  45. {
  46. if ($testOpeningOrClosing == 'opening') {
  47. $methodGetTime = 'getTimeStart';
  48. } else {
  49. $methodGetTime = 'getTimeEnd';
  50. }
  51. $openingDay = $this->getOpeningByWeekday($weekDay, $openings);
  52. if ($openingDay && $openingDay->$methodGetTime()) {
  53. return true;
  54. }
  55. return false;
  56. }
  57. public function getOpeningByWeekday(int $weekDay, array $openings): ?OpeningInterface
  58. {
  59. foreach ($openings as $opening) {
  60. if ($opening->getDay() == $weekDay) {
  61. return $opening;
  62. }
  63. }
  64. return null;
  65. }
  66. public function getFormatedDateByFormatAndDelimiterDayTime(
  67. \DateTime $date,
  68. \DateTimeInterface $time,
  69. string $formatDate = '',
  70. string $delimiterDayTime = ''
  71. ): string {
  72. setlocale(LC_TIME, 'fr_FR.UTF8', 'fr.UTF8', 'fr_FR.UTF-8', 'fr.UTF-8');
  73. if (!strlen($formatDate)) {
  74. $formatDate = '%A %e %B';
  75. }
  76. return strftime(
  77. $formatDate . ' ' . $delimiterDayTime . ' ' . $time->format('H\hi'),
  78. $date->getTimestamp()
  79. );
  80. }
  81. }