class Random (View source)

Defines a utility class for creating random data.

Constants

MAXIMUM_TRIES

The maximum number of times name() and string() can loop.

This prevents infinite loops if the length of the random value is very small.

Properties

protected array $strings

A list of unique strings generated by string().

protected array $names

A list of unique names generated by name().

Methods

string
string(int $length = 8, bool $unique = FALSE, callable $validator = NULL)

Generates a random string of ASCII characters of codes 32 to 126.

string
name(int $length = 8, bool $unique = FALSE)

Generates a random string containing letters and numbers.

string
word(int $length)

Generate a string that looks like a word (letters only, alternating consonants and vowels).

object
object(int $size = 4)

Generates a random PHP object.

string
sentences(int $min_word_count, bool $capitalize = FALSE)

Generates sentences Latin words, often used as placeholder text.

string
paragraphs(int $paragraph_count = 12)

Generate paragraphs separated by double new line.

string
image(string $destination, string $min_resolution, string $max_resolution)

Create a placeholder image.

Details

string string(int $length = 8, bool $unique = FALSE, callable $validator = NULL)

Generates a random string of ASCII characters of codes 32 to 126.

The generated string includes alpha-numeric characters and common miscellaneous characters. Use this method when testing general input where the content is not restricted.

Parameters

int $length

Length of random string to generate.

bool $unique

(optional) If TRUE ensures that the random string returned is unique. Defaults to FALSE.

callable $validator

(optional) A callable to validate the string. Defaults to NULL.

Return Value

string

Randomly generated string.

See also

Random::name

string name(int $length = 8, bool $unique = FALSE)

Generates a random string containing letters and numbers.

The string will always start with a letter. The letters may be upper or lower case. This method is better for restricted inputs that do not accept certain characters. For example, when testing input fields that require machine readable values (i.e. without spaces and non-standard characters) this method is best.

Parameters

int $length

Length of random string to generate.

bool $unique

(optional) If TRUE ensures that the random string returned is unique. Defaults to FALSE.

Return Value

string

Randomly generated string.

See also

Random::string

string word(int $length)

Generate a string that looks like a word (letters only, alternating consonants and vowels).

Parameters

int $length

The desired word length.

Return Value

string

object object(int $size = 4)

Generates a random PHP object.

Parameters

int $size

The number of random keys to add to the object.

Return Value

object

The generated object, with the specified number of random keys. Each key has a random string value.

string sentences(int $min_word_count, bool $capitalize = FALSE)

Generates sentences Latin words, often used as placeholder text.

Parameters

int $min_word_count

The minimum number of words in the return string. Total word count can slightly exceed provided this value in order to deliver sentences of random length.

bool $capitalize

Uppercase all the words in the string.

Return Value

string

Nonsense latin words which form sentence(s).

string paragraphs(int $paragraph_count = 12)

Generate paragraphs separated by double new line.

Parameters

int $paragraph_count

The number of paragraphs to create. Defaults to 12.

Return Value

string

string image(string $destination, string $min_resolution, string $max_resolution)

Create a placeholder image.

Parameters

string $destination

The absolute file path where the image should be stored.

string $min_resolution

The minimum resolution for the image. For example, '400x300'.

string $max_resolution

The maximum resolution for the image. For example, '800x600'.

Return Value

string

Path to image file.