You are here

final class Interval in Commerce Core 8.2

Provides a value object for intervals (1 month, 14 days, etc).

Hierarchy

Expanded class hierarchy of Interval

5 files declare their use of Interval
CartExpiration.php in modules/cart/src/Plugin/QueueWorker/CartExpiration.php
Cron.php in modules/cart/src/Cron.php
IntervalTest.php in tests/src/Kernel/IntervalTest.php
MonthlyTest.php in modules/number_pattern/tests/src/Kernel/Plugin/Commerce/NumberPattern/MonthlyTest.php
YearlyTest.php in modules/number_pattern/tests/src/Kernel/Plugin/Commerce/NumberPattern/YearlyTest.php
2 string references to 'Interval'
commerce_cart.schema.yml in modules/cart/config/schema/commerce_cart.schema.yml
modules/cart/config/schema/commerce_cart.schema.yml
commerce_cart_form_commerce_order_type_form_alter in modules/cart/commerce_cart.module
Implements hook_form_BASE_FORM_ID_alter() for 'commerce_order_type_form'.

File

src/Interval.php, line 10

Namespace

Drupal\commerce
View source
final class Interval {

  /**
   * The number.
   *
   * @var string
   */
  protected $number;

  /**
   * The unit.
   *
   * @var string
   */
  protected $unit;

  /**
   * Constructs a new Interval object.
   *
   * @param string $number
   *   The number.
   * @param string $unit
   *   The unit.
   */
  public function __construct(string $number, string $unit) {
    if (!is_numeric($number)) {
      throw new \InvalidArgumentException(sprintf('The provided interval number "%s" is not a numeric value.', $number));
    }
    if (!in_array($unit, [
      'minute',
      'hour',
      'day',
      'week',
      'month',
      'year',
    ])) {
      throw new \InvalidArgumentException(sprintf('Invalid interval unit "%s" provided.', $unit));
    }
    $this->number = $number;
    $this->unit = $unit;
  }

  /**
   * Gets the number.
   *
   * @return string
   *   The number.
   */
  public function getNumber() : string {
    return $this->number;
  }

  /**
   * Gets the unit.
   *
   * @return string
   *   The unit.
   */
  public function getUnit() : string {
    return $this->unit;
  }

  /**
   * Gets the string representation of the interval.
   *
   * @return string
   *   The string representation of the interval.
   */
  public function __toString() : string {
    return $this->number . ' ' . $this->unit;
  }

  /**
   * Gets the array representation of the interval.
   *
   * @return array
   *   The array representation of the interval.
   */
  public function toArray() : array {
    return [
      'number' => $this->number,
      'unit' => $this->unit,
    ];
  }

  /**
   * Adds the interval to the given date.
   *
   * @param \Drupal\Core\Datetime\DrupalDateTime $date
   *   The date.
   *
   * @return \Drupal\Core\Datetime\DrupalDateTime
   *   The new date.
   */
  public function add(DrupalDateTime $date) : DrupalDateTime {

    /** @var \Drupal\Core\Datetime\DrupalDateTime $new_date */
    $new_date = clone $date;
    $new_date
      ->modify('+' . $this
      ->__toString());

    // Jan 31st + 1 month should give Feb 28th, not Mar 3rd.
    if ($this->unit == 'month' && $new_date
      ->format('d') !== $date
      ->format('d')) {
      $new_date
        ->modify('last day of previous month');
    }
    return $new_date;
  }

  /**
   * Subtracts the interval from the given date.
   *
   * @param \Drupal\Core\Datetime\DrupalDateTime $date
   *   The date.
   *
   * @return \Drupal\Core\Datetime\DrupalDateTime
   *   The new date.
   */
  public function subtract(DrupalDateTime $date) : DrupalDateTime {

    /** @var \Drupal\Core\Datetime\DrupalDateTime $new_date */
    $new_date = clone $date;
    $new_date
      ->modify('-' . $this
      ->__toString());

    // Mar 31st - 1 month should Feb 28th, not Mar 3rd.
    if ($this->unit == 'month' && $new_date
      ->format('d') !== $date
      ->format('d')) {
      $new_date
        ->modify('last day of previous month');
    }
    return $new_date;
  }

  /**
   * Reduces the date to the lower boundary.
   *
   * For example, an Apr 14th date would be reduced to Apr 1st for monthly
   * intervals, and Jan 1st for yearly intervals.
   *
   * @param \Drupal\Core\Datetime\DrupalDateTime $date
   *   The date.
   *
   * @return \Drupal\Core\Datetime\DrupalDateTime
   *   The new date.
   */
  public function floor(DrupalDateTime $date) : DrupalDateTime {

    /** @var \Drupal\Core\Datetime\DrupalDateTime $new_date */
    $new_date = clone $date;
    switch ($this->unit) {
      case 'hour':
        $new_date
          ->setTime($new_date
          ->format('G'), 0);
        break;
      case 'day':
        $new_date
          ->setTime(0, 0, 0);
        break;
      case 'week':

        // @todo Account for weeks that start on a sunday.
        $new_date
          ->modify('monday this week');
        $new_date
          ->setTime(0, 0, 0);
        break;
      case 'month':
        $new_date
          ->modify('first day of this month');
        $new_date
          ->setTime(0, 0, 0);
        break;
      case 'year':
        $new_date
          ->modify('first day of january');
        $new_date
          ->setTime(0, 0, 0);
        break;
    }
    return $new_date;
  }

  /**
   * Increases the date to the upper boundary.
   *
   * For example, an Apr 14th date would be increased to May 1st for a 1 month
   * interval, and to June 1st for a 2 month interval.
   *
   * @param \Drupal\Core\Datetime\DrupalDateTime $date
   *   The date.
   *
   * @return \Drupal\Core\Datetime\DrupalDateTime
   *   The new date.
   */
  public function ceil(DrupalDateTime $date) : DrupalDateTime {
    return $this
      ->add($this
      ->floor($date));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Interval::$number protected property The number.
Interval::$unit protected property The unit.
Interval::add public function Adds the interval to the given date.
Interval::ceil public function Increases the date to the upper boundary.
Interval::floor public function Reduces the date to the lower boundary.
Interval::getNumber public function Gets the number.
Interval::getUnit public function Gets the unit.
Interval::subtract public function Subtracts the interval from the given date.
Interval::toArray public function Gets the array representation of the interval.
Interval::__construct public function Constructs a new Interval object.
Interval::__toString public function Gets the string representation of the interval.