Roomify\Bat\Event\EventItemizer::itemizeEvent PHP Method

itemizeEvent() public method

Transforms the event in a breakdown of days, hours and minutes with associated states.
public itemizeEvent ( ) : array
return array
    public function itemizeEvent()
    {
        // In order to itemize the event we cycle through each day of the event and determine
        // what should go in the DAY array to start with. While we could use P1M this created
        // problems with months like February (because the period is 30 days) so stepping through
        // each day is safer.
        $interval = new \DateInterval('P1D');
        // Set the end date to the last day of the month so that we are sure to get that last month unless
        // we are already dealing with the last day of the month
        if ($this->event->getEndDate()->format('d') != $this->event->getEndDate()->format('t')) {
            $adjusted_end_day = new \DateTime($this->event->getEndDate()->format('Y-n-t'));
        } elseif ($this->event->getStartDate()->format('Y-m-d H:i') == $this->event->getEndDate()->format('Y-m-d H:i') && $this->granularity == EventItemizer::BAT_DAILY) {
            $adjusted_end_day = new \DateTime($this->event->getEndDate()->add(new \DateInterval('PT1M'))->format('Y-m-d H:i'));
        } else {
            $adjusted_end_day = new \DateTime($this->event->getEndDate()->format('Y-m-d H:i'));
        }
        $daterange = new \DatePeriod($this->event->getStartDate(), $interval, $adjusted_end_day);
        $itemized = array();
        $old_month = $this->event->getStartDate()->format('Y-n');
        $start = TRUE;
        // Cycle through each month
        foreach ($daterange as $date) {
            // Check if we have
            if ($date->format('Y-n') != $old_month || $start) {
                $year = $date->format("Y");
                $dayinterval = new \DateInterval('P1D');
                // Handle the first month
                if ($this->event->isFirstMonth($date)) {
                    // If we are in the same month the end date is the end date of the event
                    if ($this->event->isSameMonth()) {
                        $dayrange = new \DatePeriod($this->event->getStartDate(), $dayinterval, new \DateTime($this->event->getEndDate()->format("Y-n-j 23:59:59")));
                    } else {
                        // alternatively it is the last day of the start month
                        $dayrange = new \DatePeriod($this->event->getStartDate(), $dayinterval, $this->event->endMonthDate($this->event->getStartDate()));
                    }
                    foreach ($dayrange as $day) {
                        $itemized[EventItemizer::BAT_DAY][$year][$day->format('n')]['d' . $day->format('j')] = $this->event->getValue();
                    }
                } elseif ($this->event->isLastMonth($date)) {
                    $dayrange = new \DatePeriod(new \DateTime($date->format("Y-n-1")), $dayinterval, $this->event->getEndDate());
                    foreach ($dayrange as $day) {
                        $itemized[EventItemizer::BAT_DAY][$year][$day->format('n')]['d' . $day->format('j')] = $this->event->getValue();
                    }
                } else {
                    $dayrange = new \DatePeriod(new \DateTime($date->format("Y-n-1")), $dayinterval, new \DateTime($date->format("Y-n-t 23:59:59")));
                    foreach ($dayrange as $day) {
                        $itemized[EventItemizer::BAT_DAY][$year][$day->format('n')]['d' . $day->format('j')] = $this->event->getValue();
                    }
                }
            }
            $start = FALSE;
            $old_month = $date->format('Y-n');
        }
        if ($this->granularity == EventItemizer::BAT_HOURLY) {
            // Add granural info in
            $itemized = $this->createDayGranural($itemized);
        }
        return $itemized;
    }

Usage Example

Example #1
0
 /**
  * Transforms the event in a breakdown of days, hours and minutes with associated states.
  *
  * @param EventItemizer $itemizer
  * @return array
  */
 public function itemize($itemizer)
 {
     $itemized = $itemizer->itemizeEvent();
     return $itemized;
 }