|
- <?php
-
- namespace Lc\CaracoleBundle\Solver\Section;
-
- use Lc\CaracoleBundle\Model\Section\OpeningInterface;
-
- class OpeningSolver
- {
- public function getNextOpeningOfOpening(\DateTime $date, array $openings): ?OpeningInterface
- {
- return $this->getNextOpening($date, $openings, 'opening');
- }
-
- public function getNextOpeningOfClosing(\DateTime $date, array $openings): ?OpeningInterface
- {
- return $this->getNextOpening($date, $openings, 'closing');
- }
-
- protected function getNextOpening(\DateTime $date, array $openings, $typeOpening = 'opening')
- {
- if ($typeOpening == 'opening') {
- $methodTestDay = 'isOpeningDay';
- } else {
- $methodTestDay = 'isClosingDay';
- }
-
- $count = 0;
-
- do {
- if ($count) {
- $date->modify('+1 day');
- }
- $weekDay = $date->format('N');
- $testDay = $this->$methodTestDay($weekDay, $openings);
- $count++;
- } while (!$testDay && $count <= 8);
-
- if ($testDay) {
- return $this->getOpeningByWeekday($weekDay, $openings);
- }
-
- return null;
- }
-
- protected function isOpeningDay(int $weekDay, array $openings): bool
- {
- $opening = $this->getOpeningByWeekday($weekDay, $openings);
-
- if($opening) {
- $now = new \DateTime();
- if($weekDay == $now->format('N')) {
- $timeEnd = $opening->getTimeEnd();
- $timeStart = $opening->getTimeStart();
-
- if((($timeEnd && $now < $timeEnd) || !$timeEnd)
- && (($timeStart && $now > $timeStart) || !$timeStart) ) {
- return true;
- }
- }
- else {
- return true;
- }
- }
-
- return false;
- }
-
- protected function isClosingDay(int $weekDay, array $openings): bool
- {
- $openingDay = $this->getOpeningByWeekday($weekDay, $openings);
-
- if ($openingDay && $openingDay->getTimeEnd()) {
- return true;
- }
-
- return false;
- }
-
- public function getOpeningByWeekday(int $weekDay, array $openings): ?OpeningInterface
- {
- foreach ($openings as $opening) {
- if ($opening->getDay() == $weekDay) {
- return $opening;
- }
- }
-
- return null;
- }
-
- public function getFormatedDateByFormatAndDelimiterDayTime(
- \DateTime $date,
- \DateTimeInterface $time,
- string $formatDate = '',
- string $delimiterDayTime = ''
- ): string {
- setlocale(LC_TIME, 'fr_FR.UTF8', 'fr.UTF8', 'fr_FR.UTF-8', 'fr.UTF-8');
- if (!strlen($formatDate)) {
- $formatDate = '%A %e %B';
- }
- return strftime(
- $formatDate . ' ' . $delimiterDayTime . ' ' . $time->format('H\hi'),
- $date->getTimestamp()
- );
- }
-
- }
|