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.

пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. $timeEnd = $opening->getTimeEnd();
  42. $timeStart = $opening->getTimeStart();
  43. if((($timeEnd && $now < $timeEnd) || !$timeEnd)
  44. && (($timeStart && $now > $timeStart) || !$timeStart) ) {
  45. return true;
  46. }
  47. }
  48. else {
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. protected function isClosingDay(int $weekDay, array $openings): bool
  55. {
  56. $openingDay = $this->getOpeningByWeekday($weekDay, $openings);
  57. if ($openingDay && $openingDay->getTimeEnd()) {
  58. return true;
  59. }
  60. return false;
  61. }
  62. public function getOpeningByWeekday(int $weekDay, array $openings): ?OpeningInterface
  63. {
  64. foreach ($openings as $opening) {
  65. if ($opening->getDay() == $weekDay) {
  66. return $opening;
  67. }
  68. }
  69. return null;
  70. }
  71. public function getFormatedDateByFormatAndDelimiterDayTime(
  72. \DateTime $date,
  73. \DateTimeInterface $time,
  74. string $formatDate = '',
  75. string $delimiterDayTime = ''
  76. ): string {
  77. setlocale(LC_TIME, 'fr_FR.UTF8', 'fr.UTF8', 'fr_FR.UTF-8', 'fr.UTF-8');
  78. if (!strlen($formatDate)) {
  79. $formatDate = '%A %e %B';
  80. }
  81. return strftime(
  82. $formatDate . ' ' . $delimiterDayTime . ' ' . $time->format('H\hi'),
  83. $date->getTimestamp()
  84. );
  85. }
  86. }