Documentation

RandomCodeGenerator

Generates a random code in base36

Table of Contents

$bytes Number of bytes to use. int
$invalidChars A list of characters that are prohibited in generated codes. string[]
__construct() Create a new Random Code Generator mixed
generate() Generate a random base36 code. string

Properties

$invalidChars

A list of characters that are prohibited in generated codes.

private string[] $invalidChars = ['1', 'I', '0', 'O', 'S', '5']
This will generate a new code each time one of these characters is encountered in a generated code.

Methods

__construct()

Create a new Random Code Generator

public __construct( [ $bytes : int = 5 ] , ...$invalidChars : string ) : mixed
Parameters
$bytes : int = 5

number of bytes to use when generating a code. Defaults to 5 bytes.

$invalidChars : string

One or more invalid characters. If a generated code contains one of the listed characters, a new code is generated until one without an invalid character is generated. Don't add too many characters here... I've added an arbitrary limit of 8 characters that can be omitted. This defaults to ['1','I','0','O']

Tags
throws
InvalidArgumentException

if bytes is less than one

Return values
mixed

generate()

Generate a random base36 code.

public generate( [ $split : int = 3 ] ) : string
Parameters
$split : int = 3

Split the generated code every $split characters with a hyphen.

Return values
string

code

Search results