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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. do {
  23. if ($count) {
  24. $date->modify('+1 day');
  25. }
  26. $weekDay = $date->format('N');
  27. $testDay = $this->$methodTestDay($weekDay, $openings);
  28. $count++;
  29. } while (!$testDay && $count <= 8);
  30. if ($testDay) {
  31. return $this->getOpeningByWeekday($weekDay, $openings);
  32. }
  33. return null;
  34. }
  35. protected function isOpeningDay(int $weekDay, array $openings): bool
  36. {
  37. $opening = $this->getOpeningByWeekday($weekDay, $openings);
  38. if($opening) {
  39. $now = new \DateTime();
  40. if($weekDay == $now->format('N')) {
  41. if($now < $opening->getTimeEnd()) {
  42. return true;
  43. }
  44. }
  45. else {
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. protected function isClosingDay(int $weekDay, array $openings): bool
  52. {
  53. $openingDay = $this->getOpeningByWeekday($weekDay, $openings);
  54. if ($openingDay && $openingDay->getTimeEnd()) {
  55. return true;
  56. }
  57. return false;
  58. }
  59. public function getOpeningByWeekday(int $weekDay, array $openings): ?OpeningInterface
  60. {
  61. foreach ($openings as $opening) {
  62. if ($opening->getDay() == $weekDay) {
  63. return $opening;
  64. }
  65. }
  66. return null;
  67. }
  68. public function getFormatedDateByFormatAndDelimiterDayTime(
  69. \DateTime $date,
  70. \DateTimeInterface $time,
  71. string $formatDate = '',
  72. string $delimiterDayTime = ''
  73. ): string {
  74. setlocale(LC_TIME, 'fr_FR.UTF8', 'fr.UTF8', 'fr_FR.UTF-8', 'fr.UTF-8');
  75. if (!strlen($formatDate)) {
  76. $formatDate = '%A %e %B';
  77. }
  78. return strftime(
  79. $formatDate . ' ' . $delimiterDayTime . ' ' . $time->format('H\hi'),
  80. $date->getTimestamp()
  81. );
  82. }
  83. }