grantholle/laravel-username-generator

Generate a random, kid-safe username.

2.1.0 2024-03-28 15:54 UTC

README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

Generate a username with flexible configuration options. For now the nouns are animal names.

Installation

You can install the package via composer:

composer require grantholle/laravel-username-generator

You can optionally publish the config file with:

php artisan vendor:publish --tag="laravel-username-generator-config"

By default, it will generate a username studly case with 1 adjective, 1 noun and no numeric prefix. You can modify the config file with your preferred numbers:

return [
    'adjectives' => 2,
    'nouns' => 1,
    'digits' => 2,
    // The casing leverages Laravel's string helper functions:
    // "lower", "upper", "studly", "kebab", "camel", "snake", "slug"
    'casing' => 'slug',
];

Usage

You can leverage your config setup to generate a username based on those options. Using the above configuration as an example, this would generate a username as adjective-adjective-noun-##:

use GrantHolle\UsernameGenerator\Username;

$username = Username::make();
// grave-tame-tiger-60

Or, if you want to configure your username on the fly, you can use a fluent API to build your username:

use GrantHolle\UsernameGenerator\Username;

$username = (new Username)
    ->withAdjectiveCount(2)
    ->withNounCount(2)
    ->withDigitCount(4)
    ->withCasing('snake')
    ->generate();
// gentle_wan_chimpanzee_sandpiper8828

Command

This also comes with a make:username command to generate a username from the command line:

# This will use what's in your configuration file
php artisan make:username
# personal-unrealistic-eland-30

You can pass in a number of options to change how the username is generated:

php artisan make:username --count 2 --digits 8 --casing studly
# OrdinaryHerring02683641
# WittyGoat89531555
Options:
  -c, --casing[=CASING]          The casing to use: "lower", "upper", "studly", "kebab", "camel", "snake", or "slug".
  -d, --digits[=DIGITS]          The number of digits to use for a prefix.
  -a, --adjectives[=ADJECTIVES]  The number of adjectives to use.
  -N, --nouns[=NOUNS]            The number of nouns to use.
  -C, --count[=COUNT]            The number of usernames to generate. [default: "1"]

Testing

composer test
composer analyse

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Want to add a noun or adjective? Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.