private function advanceToNextDayOfWeekIfNecessary(\DateTime $fireTime, $forceToAdvanceNextDay)
    {
        // a. Advance or adjust to next dayOfWeek if need to first, starting next day with startTimeOfDay.
        $sTimeOfDay = $this->getStartTimeOfDay();
        $fireTimeStartDate = $this->getTimeOfDayForDate($fireTime, $sTimeOfDay);
        $dayOfWeekOfFireTime = (int) $fireTimeStartDate->format('N');

        // b2. We need to advance to another day if isAfterTimePassEndTimeOfDay is true, or dayOfWeek is not set.
        $daysOfWeekToFire = $this->getDaysOfWeek();
        if ($forceToAdvanceNextDay || false == in_array($dayOfWeekOfFireTime, $daysOfWeekToFire, true)) {
            // Advance one day at a time until next available date.
            for ($i = 1; $i <= 7; $i++) {
                $fireTimeStartDate->add(new \DateInterval('P1D'));
                $dayOfWeekOfFireTime = (int) $fireTimeStartDate->format('N');
                if (in_array($dayOfWeekOfFireTime, $daysOfWeekToFire, true)) {
                    $fireTime = $fireTimeStartDate;

                    break;
                }
            }
        }

        // Check fireTime not pass the endTime
        $endTime = $this->getEndTime();
        if ($endTime && $fireTime > $endTime) {
            return null;
        }

        return $fireTime;
    }
private function advanceToNextDayOfWeekIfNecessary(\DateTime $fireTime, $forceToAdvanceNextDay)
    {
        // a. Advance or adjust to next dayOfWeek if need to first, starting next day with startTimeOfDay.
        $sTimeOfDay = $this->getStartTimeOfDay();
        $fireTimeStartDate = $this->getTimeOfDayForDate($fireTime, $sTimeOfDay);
        $dayOfWeekOfFireTime = (int) $fireTimeStartDate->format('N');

        // b2. We need to advance to another day if isAfterTimePassEndTimeOfDay is true, or dayOfWeek is not set.
        $daysOfWeekToFire = $this->getDaysOfWeek();
        if ($forceToAdvanceNextDay || false == in_array($dayOfWeekOfFireTime, $daysOfWeekToFire, true)) {
            // Advance one day at a time until next available date.
            for ($i = 1; $i <= 7; $i++) {
                $fireTimeStartDate->add(new \DateInterval('P1D'));
                $dayOfWeekOfFireTime = (int) $fireTimeStartDate->format('N');
                if (in_array($dayOfWeekOfFireTime, $daysOfWeekToFire, true)) {
                    $fireTime = $fireTimeStartDate;

                    break;
                }
            }
        }

        // Check fireTime not pass the endTime
        $endTime = $this->getEndTime();
        if ($endTime && $fireTime > $endTime) {
            return null;
        }

        return $fireTime;
    }
private function advanceToNextDayOfWeekIfNecessary ( \ DateTime $ fireTime , $ forceToAdvanceNextDay ) { // a. Advance or adjust to next dayOfWeek if need to first, starting next day with startTimeOfDay. $ sTimeOfDay = $ this -> getStartTimeOfDay ( ) ; $ fireTimeStartDate = $ this -> getTimeOfDayForDate ( $ fireTime , $ sTimeOfDay ) ; $ dayOfWeekOfFireTime = ( int ) $ fireTimeStartDate -> format ( 'N' ) ; // b2. We need to advance to another day if isAfterTimePassEndTimeOfDay is true, or dayOfWeek is not set. $ daysOfWeekToFire = $ this -> getDaysOfWeek ( ) ; if ( $ forceToAdvanceNextDay || false == in_array ( $ dayOfWeekOfFireTime , $ daysOfWeekToFire , true ) ) { // Advance one day at a time until next available date. for ( $ i = 1 ; $ i <= 7 ; $ i ++ ) { $ fireTimeStartDate -> add ( new \ DateInterval ( 'P1D' ) ) ; $ dayOfWeekOfFireTime = ( int ) $ fireTimeStartDate -> format ( 'N' ) ; if ( in_array ( $ dayOfWeekOfFireTime , $ daysOfWeekToFire , true ) ) { $ fireTime = $ fireTimeStartDate ; break ; } } } // Check fireTime not pass the endTime $ endTime = $ this -> getEndTime ( ) ; if ( $ endTime && $ fireTime > $ endTime ) { return null ; } return $ fireTime ; }
Given fireTime time determine if it is on a valid day of week. If so, simply return it unaltered,
if not, advance to the next valid week day, and set the time of day to the start time of day

@param \DateTime $fireTime - given next fireTime.
@param bool      $forceToAdvanceNextDay - flag to whether to advance day without check existing week day. This scenario
can happen when a caller determine fireTime has passed the endTimeOfDay that fireTime should move to next day anyway.
@return \DateTime a next day fireTime.
Given fireTime time determine if it is on a valid day of week . If so simply return it unaltered if not advance to the next valid week day and set the time of day to the start time of day