You are here

public function ActiveHours::getRanges in SMS Framework 8

Same name and namespace in other branches
  1. 2.x modules/sms_user/src/ActiveHours.php \Drupal\sms_user\ActiveHours::getRanges()
  2. 2.1.x modules/sms_user/src/ActiveHours.php \Drupal\sms_user\ActiveHours::getRanges()

Get ranges converted to local timezone and sorted chronologically.

Parameters

string|\DateTimeZone $timezone: A timezone string or object.

Return value

\Drupal\sms_user\ActiveHoursDates[] A array of date pairs sorted chronologically by start dates.

Overrides ActiveHoursInterface::getRanges

2 calls to ActiveHours::getRanges()
ActiveHours::findNextTime in modules/sms_user/src/ActiveHours.php
Determine the next valid active hours date range for a user.
ActiveHours::inHours in modules/sms_user/src/ActiveHours.php
Determine if the current time of a user is within permitted hour ranges.

File

modules/sms_user/src/ActiveHours.php, line 103

Class

ActiveHours
Defines the user active hours service.

Namespace

Drupal\sms_user

Code

public function getRanges($timezone) {
  $this
    ->build();
  $dates = [];
  foreach ($this->ranges as $range) {
    $dates[] = new ActiveHoursDates(new DrupalDateTime($range['start'], $timezone), new DrupalDateTime($range['end'], $timezone));
  }

  // Sort so nearest date is closest.
  // Can't do this in build() since computed relative dates can be different
  // per timezone.
  usort($dates, function ($a, $b) {
    if ($a
      ->getStartDate() == $b
      ->getStartDate()) {
      return 0;
    }
    return $a
      ->getStartDate() < $b
      ->getStartDate() ? -1 : 1;
  });
  return $dates;
}