You are here

public function Math::sequential in Realistic Dummy Content 3.x

Same name and namespace in other branches
  1. 8.2 api/src/includes/Math.php \Drupal\realistic_dummy_content_api\includes\Math::sequential()
  2. 7.2 api/src/includes/Math.php \Drupal\realistic_dummy_content_api\includes\Math::sequential()

Generate sequential number based on a hash.

Returns the starting number on every call until the hash is changed, in which case it returns the second number, and so on.

The idea behind this is that for a single node, we might want to retrieve the 3rd file for each field (they go together).

In the above example, if the 3rd file does not exist, we will return the first file, in order to never return a number which is outside the range of start to end.

Parameters

int $start: The first possible number in the range.

int $end: The last possible number in the range.

string $hash: The number returned by this function will be in sequence: each call to realistic_dummy_content_api_sequential()'s return is incremented by one, unless $hash is the same as in the last call, in which case the return will be the same as in the last call.

Return value

int A sequential number based on the $hash. Please see the description of the $hash parameter, above.

File

api/src/includes/Math.php, line 37

Class

Math
Everything having to do with numbers and calculations.

Namespace

Drupal\realistic_dummy_content_api\includes

Code

public function sequential($start, $end, $hash) {
  static $static_hash = NULL;
  if (!$static_hash) {
    $static_hash = $hash;
  }
  static $current = NULL;
  if (!$current) {
    $current = $start;
  }
  if ($static_hash != $hash) {
    $static_hash = $hash;
    $current -= $start;
    $current++;
    $current %= $end - $start + 1;
    $current += $start;
  }
  if ($current > $end) {
    $return = $end;
  }
  elseif ($current < $start) {
    $return = $start;
  }
  else {
    $return = $current;
  }
  return $return;
}