You are here

class DateRange in Recurring Dates Field 3.1.x

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

Defines a date range.

Hierarchy

Expanded class hierarchy of DateRange

5 files declare their use of DateRange
DateRecurBasicFormatter.php in src/Plugin/Field/FieldFormatter/DateRecurBasicFormatter.php
DateRecurDateRangeUnitTest.php in tests/src/Unit/DateRecurDateRangeUnitTest.php
DateRecurFieldItemListTest.php in tests/src/Kernel/DateRecurFieldItemListTest.php
DateRecurRruleUnitTest.php in tests/src/Unit/DateRecurRruleUnitTest.php
RlHelper.php in src/Rl/RlHelper.php

File

src/DateRange.php, line 10

Namespace

Drupal\date_recur
View source
class DateRange {

  /**
   * The start date.
   *
   * @var \DateTimeInterface
   */
  protected $start;

  /**
   * The end date.
   *
   * @var \DateTimeInterface
   */
  protected $end;

  /**
   * Creates a new DateRange.
   *
   * @param \DateTimeInterface $start
   *   The start date.
   * @param \DateTimeInterface $end
   *   The end date.
   */
  public function __construct(\DateTimeInterface $start, \DateTimeInterface $end) {
    $this->start = clone $start;
    $this->end = clone $end;
    $this
      ->validateDates();
  }

  /**
   * Get the start date.
   *
   * @return \DateTimeInterface
   *   The start date.
   */
  public function getStart() : \DateTimeInterface {
    return clone $this->start;
  }

  /**
   * Set the start date.
   *
   * @param \DateTimeInterface $start
   *   The start date.
   *
   * @return $this
   *   Return object for chaining.
   *
   * @throws \InvalidArgumentException
   *   When there is a problem with the start and/or end date.
   */
  public function setStart(\DateTimeInterface $start) {

    // Clone to ensure references are lost.
    $this->start = clone $start;
    $this
      ->validateDates();
    return $this;
  }

  /**
   * Get the end date.
   *
   * @return \DateTimeInterface
   *   The end date.
   */
  public function getEnd() : \DateTimeInterface {
    return clone $this->end;
  }

  /**
   * Set the end date.
   *
   * @param \DateTimeInterface $end
   *   The end date.
   *
   * @return $this
   *   Return object for chaining.
   *
   * @throws \InvalidArgumentException
   *   When there is a problem with the start and/or end date.
   */
  public function setEnd(\DateTimeInterface $end) {

    // Clone to ensure references are lost.
    $this->end = clone $end;
    $this
      ->validateDates();
    return $this;
  }

  /**
   * Validates the start and end dates.
   *
   * @throws \InvalidArgumentException
   *   When there is a problem with the start and/or end date.
   */
  protected function validateDates() : void {

    // Normalize end date timezone.
    if ($this->start
      ->getTimezone()
      ->getName() !== $this->end
      ->getTimezone()
      ->getName()) {
      throw new \InvalidArgumentException('Provided dates must be the same timezone.');
    }
    if ($this->end < $this->start) {
      throw new \InvalidArgumentException('End date must not occur before start date.');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DateRange::$end protected property The end date.
DateRange::$start protected property The start date.
DateRange::getEnd public function Get the end date.
DateRange::getStart public function Get the start date.
DateRange::setEnd public function Set the end date.
DateRange::setStart public function Set the start date.
DateRange::validateDates protected function Validates the start and end dates.
DateRange::__construct public function Creates a new DateRange.