|
- <?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;
- $isClosingDay = false;
-
- do {
- if ($count) {
- $date->modify('+1 day');
- }
- $weekDay = $date->format('N');
- $isClosingDay = $this->$methodTestDay($weekDay, $openings);
- $count++;
- } while (!$isClosingDay && $count <= 7);
-
- if ($isClosingDay) {
- return $this->getOpeningByWeekday($weekDay, $openings);
- }
-
- return null;
- }
-
- public function isOpeningDay(int $weekDay, array $openings): bool
- {
- return $this->isOpeningOrClosingDay($weekDay, $openings, 'opening');
- }
-
- public function isClosingDay(int $weekDay, array $openings): bool
- {
- return $this->isOpeningOrClosingDay($weekDay, $openings, 'closing');
- }
-
- protected function isOpeningOrClosingDay(int $weekDay, array $openings, string $testOpeningOrClosing = 'opening')
- {
- if ($testOpeningOrClosing == 'opening') {
- $methodGetTime = 'getTimeStart';
- } else {
- $methodGetTime = 'getTimeEnd';
- }
-
- $openingDay = $this->getOpeningByWeekday($weekDay, $openings);
- if ($openingDay && $openingDay->$methodGetTime()) {
- 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()
- );
- }
-
- }
|