-
Notifications
You must be signed in to change notification settings - Fork 9
Custom Drivers
You can create your own custom drivers easily by extending the BaseDriver class.
The only requirement that a driver class has is a public $field property that is the field name on the model to get the input from.
use TaylorNetwork\UsernameGenerator\Drivers\BaseDriver;
class CustomDriver extends BaseDriver
{
public $field = '[field name on model]';
}All drivers will execute the following actions to transform the input into the username.
The order in which these are done by default is:
- toAscii
- stripUnwantedCharacters
- convertCase
- collapseWhitespace
- addSeparator
- makeUnique
- checkMinLength
- checkMaxLength
Key: 'toAscii'
Converts input to ASCII string, if applicable.
Key: 'stripUnwantedCharacters'
Removes all characters from string not in 'allowed_characters' config setting.
Key: 'convertCase'
Converts the case to the 'case' config setting using Laravel String Helpers. If that fails, converting case is ignored.
Key: 'collapseWhitespace'
Converts all whitespace in input to single spaces, and removes whitespace from start and end.
Key: 'addSeparator'
Replaces all spaces with the 'separator' character in config.
Key: 'makeUnique'
Attempts to make the username unique.
Key: 'checkMinLength'
Checks to see if the username length adheres to the 'min_length' value in config. Then will either pad digits or throw an exception depending on the value of 'throw_exception_on_too_short'.
Key: 'checkMaxLength'
Checks to see if the username length adheres to the 'max_length' value in config. Then will either reduce the length or throw an exception depending on the value of 'throw_exception_on_too_long'.
You may add a hook to any of the above actions in your driver class by prefixing either 'before' or 'after' to any of the action names. If there are any actions that are the very first or last things you may also add those methods.
use TaylorNetwork\UsernameGenerator\Drivers\BaseDriver;
class CustomDriver extends BaseDriver
{
public $field = '[field name on model]';
public function first(string $text): string
{
return str_replace('*', ' ', $text);
}
}Will replace all * in the text with spaces and then perform username generation.
use TaylorNetwork\UsernameGenerator\Drivers\BaseDriver;
class CustomDriver extends BaseDriver
{
public $field = '[field name on model]';
public function last(string $text): string
{
return '*' . $text . '*';
}
}Will add * to the start and end of the generated username.
If you want to modify the input before converting the case:
use TaylorNetwork\UsernameGenerator\Drivers\BaseDriver;
class CustomDriver extends BaseDriver
{
public $field = '[field name on model]';
public function beforeConvertCase(string $text): string
{
return '123-' . $text;
}
}If you want to modify text after making it unique:
use TaylorNetwork\UsernameGenerator\Drivers\BaseDriver;
class CustomDriver extends BaseDriver
{
public $field = '[field name on model]';
public function beforeConvertCase(string $text): string
{
return '123-' . $text;
}
public function afterMakeUnique(string $text): string
{
return $text . '-auto';
}
}You must register your driver in config/username_generator.php in order to use it.
'drivers' => [
// other drivers...
'custom' => CustomDriver::class,
],Note:
Whichever driver is the first item registered will be used as the default driver if none is specified.
You may now use your custom driver by either using the setDriver method or the using* static method.
UsernameGenerator::usingCustom()->generate('test');
(new Generator)->setDriver('custom')->generate('test');