You are here

public function DateRecurRRule::parseRrule in Recurring Dates Field 8

Parse an RFC rrule string and add a start date (DTSTART).

Parameters

string $rrule:

\DateTime|DrupalDateTime $startDate:

Return value

array An array of rrule parts.

Throws

\InvalidArgumentException

File

src/DateRecurRRule.php, line 138

Class

DateRecurRRule

Namespace

Drupal\date_recur

Code

public function parseRrule($rrule, $startDate, $check_only = FALSE) {

  // Correct formatting.
  if (strpos($rrule, "\n") === FALSE && strpos($rrule, 'RRULE:') !== 0) {
    $rrule = "RRULE:{$rrule}";
  }
  $dtstart = 'DTSTART:' . $startDate
    ->format(self::RFC_DATE_FORMAT);

  // Check for unsupported parts.
  $set_keys = [
    'RDATE',
    'EXRULE',
    'EXDATE',
  ];
  $rules = $set_parts = [];
  foreach (explode("\n", $rrule) as $key => $part) {
    $els = explode(':', $part);
    if (in_array($els[0], $set_keys)) {
      $set_parts[$els[0]][] = $part;
    }
    else {
      if ($els[0] == 'RRULE') {
        $rules[] = $part;
      }
      else {
        if ($els[0] == 'DTSTART') {
          $dtstart = $part;
        }
        else {
          throw new \InvalidArgumentException("Unsupported line: " . $part);
        }
      }
    }
  }
  if (!count($rules)) {
    throw new \InvalidArgumentException("Missing RRULE line: " . $rrule);
  }
  if (count($rules) > 1) {
    throw new \InvalidArgumentException("More than one RRULE line is not supported.");
  }
  $rrule = $dtstart . "\n" . $rules[0];
  if (empty($parts['WKST'])) {
    $parts['WKST'] = 'MO';
  }
  $this->parts = RfcParser::parseRRule($rrule);
  $this->setParts = $set_parts;
}