You are here

protected static function JobSchedulerCronTab::parseElement in Job Scheduler 8.3

Same name and namespace in other branches
  1. 8.2 src/JobSchedulerCronTab.php \Drupal\job_scheduler\JobSchedulerCronTab::parseElement()

Parses each text element. Recursive up to some point.

Parameters

string $type: The element type. One of 'minutes', 'hours', 'mday', 'mon', 'wday'.

string $string: The element string to parse.

bool $translate: (optional) Whether or not to translate. Defaults to false.

Return value

array The element string parsed into an array.

1 call to JobSchedulerCronTab::parseElement()
JobSchedulerCronTab::values in src/JobSchedulerCronTab.php
Parses an array of values, check whether this is valid.

File

src/JobSchedulerCronTab.php, line 265

Class

JobSchedulerCronTab
Class for job scheduler crontab.

Namespace

Drupal\job_scheduler

Code

protected static function parseElement($type, $string, $translate = FALSE) {
  $string = trim($string);
  if ($translate) {
    $string = self::translateNames($type, $string);
  }
  if ($string === '*') {

    // This means all possible values, return right away, no need to double
    // check.
    return self::possibleValues($type);
  }
  elseif (strpos($string, '/')) {

    // Multiple. Example */2, for weekday will expand into 2, 4, 6.
    list($values, $multiple) = explode('/', $string);
    $values = self::parseElement($type, $values);
    foreach ($values as $value) {
      if (!($value % $multiple)) {
        $range[] = $value;
      }
    }
  }
  elseif (strpos($string, ',')) {

    // Now process list parts, expand into items, process each and merge back.
    $list = explode(',', $string);
    $range = [];
    foreach ($list as $item) {
      if ($values = self::parseElement($type, $item)) {
        $range = array_merge($range, $values);
      }
    }
  }
  elseif (strpos($string, '-')) {

    // This defines a range. Example 1-3, will expand into 1,2,3.
    list($start, $end) = explode('-', $string);

    // Double check the range is within possible values.
    $range = range($start, $end);
  }
  elseif (is_numeric($string)) {

    // This looks like a single number, double check it's int.
    $range = [
      (int) $string,
    ];
  }

  // Return unique sorted values and double check they're within possible
  // values.
  if (!empty($range)) {
    $range = array_intersect(array_unique($range), self::possibleValues($type));
    sort($range);

    // Sunday validation. We need cron values to match PHP values, thus week
    // day 7 is not allowed, must be 0.
    if ($type == 'wday' && in_array(7, $range)) {
      array_pop($range);
      array_unshift($range, 0);
    }
    return $range;
  }
  else {

    // No match found for this one, will produce an error with validation.
    return [];
  }
}