You are here

function realistic_dummy_content_api_sequential in Realistic Dummy Content 7

Same name and namespace in other branches
  1. 8 api/realistic_dummy_content_api.module \realistic_dummy_content_api_sequential()

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.

2 calls to realistic_dummy_content_api_sequential()
realistic_dummy_content_api_rand in api/realistic_dummy_content_api.module
Generate a random, or sequential, number
realistic_dummy_content_UnitTestCase::assertSequential in api/tests/realistic_dummy_content_api.unit.test
Helper function to assert that the sequential number generator works.

File

api/realistic_dummy_content_api.module, line 358
API code allowing other modules to generate realistic dummy content. See the Realistic Dummy Content module for an example of how to use.

Code

function realistic_dummy_content_api_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;
}