/** * Calendrier constructor. * * Génère chaque jours du calendrier pour une période scolaire donnée * * @param PeriodInterface $period * @param function - $closureIsDayOff */ public function __construct(PeriodInterface $yearPeriod, $closureIsDayOff) { $this->yearPeriod = $yearPeriod; $this->days = array(); $period = new Period($this->yearPeriod->getFirstDate(), new \DateTimeImmutable($this->yearPeriod->getLastDate()->format('Y-m') . '-31')); foreach ($period->getDayIterator() as $currentDay) { $d = new Day($currentDay); $d->setOff($closureIsDayOff($d)); if (!$yearPeriod->isIncluded($d->getDate())) { $d->setOff(true); } $this->days[$d->getMonth()][$d->getDay()] = $d; } }
/** * Return the week dates (first day, last day, list of days) given options passed * * @param array $options with the following keys : * - date_day : \DateTimeInterface of the day of reference * - enable_next_week : boolean - True if the week to return is the next following * the week containing the day of reference * - days_ofweek_off [optional] : Day::constants[] list of days generally off in the week. * - dates_off [optional] : \DateTime[] list of dates off in the week * * @return array associative array with the following keys : * - first_day : \DateTimeImmutable of the first day of the week (the monday) * - last_day : \DateTimeImmutable of the last day of the week (the friday) * - days : \DateTimeImmutable[] index array of days in the week that take in account * the closed days passed in options */ public function getWeekDates($options) { $options = $this->weekMealResolver->resolve($options); if (true === $options['enable_next_week']) { $firstDayFormat = ' next monday'; $lastDayFormat = ' next friday'; } else { $firstDayFormat = ' monday this week'; $lastDayFormat = ' friday this week'; } $firstDay = new \DateTimeImmutable($options['date_day']->format('Y-m-d') . $firstDayFormat); $lastDay = new \DateTimeImmutable($firstDay->format('Y-m-d') . $lastDayFormat); $periode = new Period($firstDay, $lastDay); $days = []; foreach ($periode->getDayIterator() as $date) { if (false === \in_array(Day::getWeekDayFrom($date), $options['days_ofweek_off']) && false === \in_array($date, $options['dates_off'])) { $days[] = $date; } } return array('first_day' => $firstDay, 'last_day' => $lastDay, 'days' => $days); }
/** * @covers ::getDayIterator * @uses \Scheduler\Component\DateContainer\PeriodIterator */ public function testGetDayIterator() { $this->assertInstanceOf(PeriodIterator::class, self::$period->getDayIterator(), "getDayIterator returns a Period iterator"); $this->assertCount(61, self::$period->getDayIterator(), "Day iterator (Period Iterator) must returns 61 days"); }