You are here

public function RlHelper::getOccurrences in Recurring Dates Field 3.x

Same name and namespace in other branches
  1. 8.2 src/Rl/RlHelper.php \Drupal\date_recur\Rl\RlHelper::getOccurrences()
  2. 3.0.x src/Rl/RlHelper.php \Drupal\date_recur\Rl\RlHelper::getOccurrences()
  3. 3.1.x src/Rl/RlHelper.php \Drupal\date_recur\Rl\RlHelper::getOccurrences()

Get all occurrences.

A limit and/or range-end must be passed.

Parameters

\DateTimeInterface|null $rangeStart: The start of the range, or start with the first available occurrence.

\DateTimeInterface|null $rangeEnd: The end of the range.

int|null $limit: A limit.

Return value

\Drupal\date_recur\DateRange[] The occurrences.

Throws

\InvalidArgumentException Exceptions thrown if ranges are invalid or undefined.

Overrides DateRecurHelperInterface::getOccurrences

File

src/Rl/RlHelper.php, line 177

Class

RlHelper
Helper for recurring rules implemented with rlanvin/rrule.

Namespace

Drupal\date_recur\Rl

Code

public function getOccurrences(\DateTimeInterface $rangeStart = NULL, ?\DateTimeInterface $rangeEnd = NULL, ?int $limit = NULL) : array {
  if ($this
    ->isInfinite() && !isset($rangeEnd) && !isset($limit)) {
    throw new \InvalidArgumentException('An infinite rule must have a date or count limit.');
  }
  $generator = $this
    ->generateOccurrences($rangeStart, $rangeEnd);
  if (isset($limit)) {
    if (!is_int($limit) || $limit < 0) {

      // Limit must be a number and more than zero.
      throw new \InvalidArgumentException('Invalid count limit.');
    }

    // Generate occurrences until the limit is reached.
    $occurrences = [];
    foreach ($generator as $value) {
      if (count($occurrences) >= $limit) {
        break;
      }
      $occurrences[] = $value;
    }
    return $occurrences;
  }
  return iterator_to_array($generator);
}