You are here

class DateRecurNonRecurringHelper in Recurring Dates Field 3.0.x

Same name and namespace in other branches
  1. 8.2 src/DateRecurNonRecurringHelper.php \Drupal\date_recur\DateRecurNonRecurringHelper
  2. 3.x src/DateRecurNonRecurringHelper.php \Drupal\date_recur\DateRecurNonRecurringHelper
  3. 3.1.x src/DateRecurNonRecurringHelper.php \Drupal\date_recur\DateRecurNonRecurringHelper

Dummy helper for handling non-recurring values.

Hierarchy

Expanded class hierarchy of DateRecurNonRecurringHelper

2 files declare their use of DateRecurNonRecurringHelper
DateRecurItem.php in src/Plugin/Field/FieldType/DateRecurItem.php
DateRecurNonRecurringHelperUnitTest.php in tests/src/Unit/DateRecurNonRecurringHelperUnitTest.php

File

src/DateRecurNonRecurringHelper.php, line 10

Namespace

Drupal\date_recur
View source
class DateRecurNonRecurringHelper implements DateRecurHelperInterface {

  /**
   * The occurrences.
   *
   * @var \Drupal\date_recur\DateRange[]
   */
  protected $occurrences = [];

  /**
   * Constructor for DateRecurNonRecurringHelper.
   *
   * @param \DateTimeInterface $dtStart
   *   The initial occurrence start date.
   * @param \DateTimeInterface|null $dtStartEnd
   *   The initial occurrence end date, or NULL to use start date.
   */
  public function __construct(\DateTimeInterface $dtStart, \DateTimeInterface $dtStartEnd = NULL) {
    $dtStartEnd = $dtStartEnd ?? clone $dtStart;
    $this->occurrences = [
      new DateRange($dtStart, $dtStartEnd),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(string $string, \DateTimeInterface $dtStart, ?\DateTimeInterface $dtStartEnd = NULL) : DateRecurHelperInterface {
    return new static($dtStart, $dtStartEnd);
  }

  /**
   * {@inheritdoc}
   */
  public function getRules() : array {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function isInfinite() : bool {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function generateOccurrences(?\DateTimeInterface $rangeStart = NULL, ?\DateTimeInterface $rangeEnd = NULL) : \Generator {
    foreach ($this->occurrences as $occurrence) {
      $occurrenceStart = $occurrence
        ->getStart();
      $occurrenceEnd = $occurrence
        ->getEnd();
      if ($rangeStart) {
        if ($occurrenceStart < $rangeStart && $occurrenceEnd < $rangeStart) {
          continue;
        }
      }
      if ($rangeEnd) {
        if ($occurrenceStart > $rangeEnd && $occurrenceEnd > $rangeEnd) {
          break;
        }
      }
      (yield $occurrence);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getOccurrences(\DateTimeInterface $rangeStart = NULL, ?\DateTimeInterface $rangeEnd = NULL, ?int $limit = NULL) : array {
    if (isset($limit) && (!is_int($limit) || $limit < 0)) {

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

    // There can either by zero or one occurrence generated for non-recurring
    // generator.
    if (isset($limit) && $limit === 0) {
      return [];
    }
    return iterator_to_array($this
      ->generateOccurrences($rangeStart, $rangeEnd));
  }

  /**
   * {@inheritdoc}
   */
  public function getExcluded() : array {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function current() : DateRange {
    return current($this->occurrences);
  }

  /**
   * {@inheritdoc}
   */
  public function next() : void {
    next($this->occurrences);
  }

  /**
   * {@inheritdoc}
   */
  public function key() : ?int {
    $key = key($this->occurrences);
    assert(is_int($key));
    return $key;
  }

  /**
   * {@inheritdoc}
   */
  public function valid() : bool {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function rewind() : void {
    reset($this->occurrences);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DateRecurNonRecurringHelper::$occurrences protected property The occurrences.
DateRecurNonRecurringHelper::createInstance public static function Create a new instance. Overrides DateRecurHelperInterface::createInstance
DateRecurNonRecurringHelper::current public function
DateRecurNonRecurringHelper::generateOccurrences public function Calculates occurrences as a generator. Overrides DateRecurHelperInterface::generateOccurrences
DateRecurNonRecurringHelper::getExcluded public function Get excluded dates. Overrides DateRecurHelperInterface::getExcluded
DateRecurNonRecurringHelper::getOccurrences public function Get all occurrences. Overrides DateRecurHelperInterface::getOccurrences
DateRecurNonRecurringHelper::getRules public function Get the rules that comprise this helper. Overrides DateRecurHelperInterface::getRules
DateRecurNonRecurringHelper::isInfinite public function Determine whether this is infinite. Overrides DateRecurHelperInterface::isInfinite
DateRecurNonRecurringHelper::key public function
DateRecurNonRecurringHelper::next public function
DateRecurNonRecurringHelper::rewind public function
DateRecurNonRecurringHelper::valid public function
DateRecurNonRecurringHelper::__construct public function Constructor for DateRecurNonRecurringHelper.