Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

89 linhas
2.4KB

  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. return (bool) $this->getOpeningByWeekday($weekDay, $openings);
  38. }
  39. protected function isClosingDay(int $weekDay, array $openings): bool
  40. {
  41. $openingDay = $this->getOpeningByWeekday($weekDay, $openings);
  42. if ($openingDay && $openingDay->getTimeEnd()) {
  43. return true;
  44. }
  45. return false;
  46. }
  47. public function getOpeningByWeekday(int $weekDay, array $openings): ?OpeningInterface
  48. {
  49. foreach ($openings as $opening) {
  50. if ($opening->getDay() == $weekDay) {
  51. return $opening;
  52. }
  53. }
  54. return null;
  55. }
  56. public function getFormatedDateByFormatAndDelimiterDayTime(
  57. \DateTime $date,
  58. \DateTimeInterface $time,
  59. string $formatDate = '',
  60. string $delimiterDayTime = ''
  61. ): string {
  62. setlocale(LC_TIME, 'fr_FR.UTF8', 'fr.UTF8', 'fr_FR.UTF-8', 'fr.UTF-8');
  63. if (!strlen($formatDate)) {
  64. $formatDate = '%A %e %B';
  65. }
  66. return strftime(
  67. $formatDate . ' ' . $delimiterDayTime . ' ' . $time->format('H\hi'),
  68. $date->getTimestamp()
  69. );
  70. }
  71. }