Skip to content

Method Reference

Sam Taylor edited this page Aug 2, 2022 · 1 revision

Generator Class

The Generator class can be instantiated with no parameters where it will use all the default config or you can optionally pass an array of config values to specfically override.

$generator = new Generator();

$generator = new Generator([
	'separator' => '_',
]);

Generate Method

Signature: generate(?string $text = null): string

Description:

Generates a username based on input using the config loaded into the instance.

Usage:

// Generate based on input
$generator->generate('test user');

// Generate random username from dictionary
$generator->generate();

GenerateFor Method

Signature: generateFor(object $model): string

Description:

Generates a username based on input from the model given using the config loaded into the instance, it will try to guess the best driver to use based on fields present on the model.

Usage:

$generator->generateFor(User::first());

SetDriver Method

Signature: setDriver(string $driverKey): self

Description:

Set the driver to use, can pass either the fully-qualified class name or key from config.

Usage:

$generator->setDriver('email');

GetDriver Method

Signature: getDriver(): BaseDriver

Description:

Gets the instance of the driver the Generator is using.

Usage:

$generator->getDriver();

UsernameGenerator Facade

Driver Classes

FindSimilarUsernames Trait

GeneratesUsernames Trait

LoadsConfig Trait

SetConfig Method

Signature: setConfig(mixed $key, mixed $value = null): self

Description:

Set config items. Can provide via associative array, or a key/value pair.

Usage:

// Array
$class->setConfig([
	'separator' => '_',
]);

// Key/Value Pair
$class->setConfig('separator', '_');