Recurr\Rule::loadFromArray PHP Méthode

loadFromArray() public méthode

Populate the object based on a RRULE array.
public loadFromArray ( $parts ) : Rule
Résultat Rule
    public function loadFromArray($parts)
    {
        // FREQ is required
        if (!isset($parts['FREQ'])) {
            throw new InvalidRRule('FREQ is required');
        } else {
            if (!in_array($parts['FREQ'], array_keys(self::$freqs))) {
                throw new InvalidRRule('FREQ is invalid');
            }
            $this->setFreq(self::$freqs[$parts['FREQ']]);
        }
        // DTSTART
        if (isset($parts['DTSTART'])) {
            $this->isStartDateFromDtstart = true;
            $date = new \DateTime($parts['DTSTART']);
            $date->setTimezone(new \DateTimeZone($this->getTimezone()));
            $this->setStartDate($date);
        }
        // DTEND
        if (isset($parts['DTEND'])) {
            $date = new \DateTime($parts['DTEND']);
            $date->setTimezone(new \DateTimeZone($this->getTimezone()));
            $this->setEndDate($date);
        }
        // UNTIL or COUNT
        if (isset($parts['UNTIL']) && isset($parts['COUNT'])) {
            throw new InvalidRRule('UNTIL and COUNT must not exist together in the same RRULE');
        } elseif (isset($parts['UNTIL'])) {
            $date = new \DateTime($parts['UNTIL']);
            $date->setTimezone(new \DateTimeZone($this->getTimezone()));
            $this->setUntil($date);
        } elseif (isset($parts['COUNT'])) {
            $this->setCount($parts['COUNT']);
        }
        // INTERVAL
        if (isset($parts['INTERVAL'])) {
            $this->setInterval($parts['INTERVAL']);
        }
        // BYSECOND
        if (isset($parts['BYSECOND'])) {
            $this->setBySecond(explode(',', $parts['BYSECOND']));
        }
        // BYMINUTE
        if (isset($parts['BYMINUTE'])) {
            $this->setByMinute(explode(',', $parts['BYMINUTE']));
        }
        // BYHOUR
        if (isset($parts['BYHOUR'])) {
            $this->setByHour(explode(',', $parts['BYHOUR']));
        }
        // BYDAY
        if (isset($parts['BYDAY'])) {
            $this->setByDay(explode(',', $parts['BYDAY']));
        }
        // BYMONTHDAY
        if (isset($parts['BYMONTHDAY'])) {
            $this->setByMonthDay(explode(',', $parts['BYMONTHDAY']));
        }
        // BYYEARDAY
        if (isset($parts['BYYEARDAY'])) {
            $this->setByYearDay(explode(',', $parts['BYYEARDAY']));
        }
        // BYWEEKNO
        if (isset($parts['BYWEEKNO'])) {
            $this->setByWeekNumber(explode(',', $parts['BYWEEKNO']));
        }
        // BYMONTH
        if (isset($parts['BYMONTH'])) {
            $this->setByMonth(explode(',', $parts['BYMONTH']));
        }
        // BYSETPOS
        if (isset($parts['BYSETPOS'])) {
            $this->setBySetPosition(explode(',', $parts['BYSETPOS']));
        }
        // WKST
        if (isset($parts['WKST'])) {
            $this->setWeekStart($parts['WKST']);
        }
        // RDATE
        if (isset($parts['RDATE'])) {
            $this->setRDates(explode(',', $parts['RDATE']));
        }
        // EXDATE
        if (isset($parts['EXDATE'])) {
            $this->setExDates(explode(',', $parts['EXDATE']));
        }
    }

Usage Example

 public function testLoadFromArray()
 {
     $this->rule->loadFromArray(array('FREQ' => 'YEARLY', 'COUNT' => '2', 'INTERVAL' => '2', 'BYSECOND' => '30', 'BYMINUTE' => '10', 'BYHOUR' => '5,15', 'BYDAY' => 'SU,WE', 'BYMONTHDAY' => '16,22', 'BYYEARDAY' => '201,203', 'BYWEEKNO' => '29,32', 'BYMONTH' => '7,8', 'BYSETPOS' => '1,3', 'WKST' => 'TU', 'EXDATE' => '20140607,20140620T010000,20140620T160000Z'));
     $this->assertEquals(Frequency::YEARLY, $this->rule->getFreq());
     $this->assertEquals(2, $this->rule->getCount());
     $this->assertEquals(2, $this->rule->getInterval());
     $this->assertEquals(array(30), $this->rule->getBySecond());
     $this->assertEquals(array(10), $this->rule->getByMinute());
     $this->assertEquals(array(5, 15), $this->rule->getByHour());
     $this->assertEquals(array('SU', 'WE'), $this->rule->getByDay());
     $this->assertEquals(array(16, 22), $this->rule->getByMonthDay());
     $this->assertEquals(array(201, 203), $this->rule->getByYearDay());
     $this->assertEquals(array(29, 32), $this->rule->getByWeekNumber());
     $this->assertEquals(array(7, 8), $this->rule->getByMonth());
     $this->assertEquals(array(1, 3), $this->rule->getBySetPosition());
     $this->assertEquals('TU', $this->rule->getWeekStart());
     $this->assertEquals(array(new DateExclusion(new \DateTime(20140607), false), new DateExclusion(new \DateTime('20140620T010000'), true), new DateExclusion(new \DateTime('20140620 16:00:00 UTC'), true, true)), $this->rule->getExDates());
 }