district5/random

District5 Randomness Library

2.0.0 2024-04-29 17:33 UTC

This package is auto-updated.

Last update: 2024-12-07 16:45:42 UTC


README

Generate... well... random stuff.

Composer:

{
    "repositories":[
        {
            "type": "vcs",
            "url": "git@github.com:district5/php-random.git"
        }
    ],
    "require": {
        "district5/random": ">=2.0.0"
    }
}
  • Installing for development environments:
    • $ composer install
  • Installing for production environments. Ignoring the dev dependencies and optimise the autoloader:
    • $ composer install --no-dev -o

Unit testing:

  • Install with composer, ensuring you include the dev details:
    • $ composer install
  • Run the phpunit binary:
    • $ ./vendor/bin/phpunit

Common uses:

UUID / GUID Pseudo Random

$includeHyphens = true;

// fast UUID generator
$uuid = \District5\Random\Uuid::simple($includeHyphens);

// slower but higher entropy UUID generator
$cost = 64;
$uuid = \District5\Random\Uuid::highEntropy($cost, $includeHyphens);

String Pseudo Random

$length = 16;

// generate an alphanumeric string
$str = \District5\Random\Strings::alphanumeric($length);

// generates an alphabetical lowercase string
$str = \District5\Random\Strings::alphabeticLowercase($length);

// generates a hex string
$str = \District5\Random\Strings::hex($length);

// generates a string using a set of allowable characters
$str = \District5\Random\Strings::fromStringOfAllowableCharacters('abcd1234', $length);

// generates a string with full custom control
$allowAlphabetical = true;
$allowNumeric = true;
$allowSpecial = true;
$excludeAmbiguous = false;
$toIgnore = ['1', 'i', 'I', '0', 'O', 'o'];
$forceEachType = true;
$str = \District5\Random\Strings::custom(
    $length,
    $allowAlphabetical,
    $allowNumeric,
    $allowSpecial,
    $excludeAmbiguous,
    $ignoreCharacters,
    $forceEachType
);

Password Pseudo Random

// generate an 8 character single password with allowed characters including alphabetical, numerical and special
$password = \District5\Random\Password::single(8);

// generate a list of 5 x 8 character passwords
$passwords = \District5\Random\Password::multiple(5, 8);

Integer Pseudo Random

// Between 0 and 10
$int = \District5\Random\Integer::inRange(0, 10);

// generate an integer between 1 and mt_getrandmax()
$int = \District5\Random\Integer::positive();

// generate an integer between 1 and 55
$int = \District5\Random\Integer::positive(55);

// generate an integer between -1 and negative mt_getrandmax()
$int = \District5\Random\Integer::negative();

// generate an integer between -1 and -55
$int = \District5\Random\Integer::negative(-55);

Array Pseudo Random

These functions work for both default keyed arrays and associative style keyed arrays, or a combination of both.

$data = ['foo', 'bar', 'dog', 'cat'];
$key = \District5\Random\Arrays::randomKey($data);
// $key would be in the range of 0-3 inclusive

$data = ['a' => 'foo', 'b' => 'bar', 'c' => 'dog', 'd' => 'cat'];
$value = \District5\Random\Arrays::randomItem($data);
// $value would be one of foo|bar|dog|cat

Dates(string) and DateTime Pseudo Random

// generates a pseudo random DateTime between the epoch and now
$randomDateTime = \District5\Random\Dates::dateTime();

// generates a pseudo random DateTime between DateTime 1 and DateTime 2.
$randomDateTime = \District5\Random\Dates::dateTimeBetweenDateTimes($dt1, $dt2);

// generate a pseudo random date string between the epoch and now with default formatting of Y-m-d (optional argument defaulted to Y-m-d)
$randomDateStr = \District5\Random\Dates::date('Y-m-d');

// generates a pseudo random date string between DateTime 1 and DateTime 2.
$randomDateStr = \District5\Random\Dates::dateBetweenDateTimes($dt1, $dt2, 'Y-m-d');