You are here

static function RealisticDummyContent::sequential in Realistic Dummy Content 8

Generate sequential number based on a hash

Returns the starting number on every call until the hash is changed, at 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

$start: The first possible number in the range.

$end: The last possible number in the range.

$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 the same as in the last call.

Return value

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

3 calls to RealisticDummyContent::sequential()
RealisticDummyContent::rand in api/src/facade/RealisticDummyContent.php
Generate a random, or sequential, number
RealisticDummyContentTest::assertSequential in api/tests/src/Unit/facade/RealisticDummyContentTest.php
Helper function to assert that the sequential number generator works.
realistic_dummy_content_api_sequential in api/realistic_dummy_content_api.module
Generate sequential number based on a hash

File

api/src/facade/RealisticDummyContent.php, line 242
Define autoload class.

Class

RealisticDummyContent

Namespace

Drupal\realistic_dummy_content_api\facade

Code

static 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;
}