Giter Club home page Giter Club logo

username-suggester's Introduction

Username Suggester

A package to suggest usernames from taylornetwork/laravel-username-generator

This would be useful if you want to show your users a list of suggested usernames based on their entry if their selection is unavailable.

Install

Via composer

$ composer require taylornetwork/username-suggester

Publish Config

This will publish to config/username_suggester.php

$ php artisan vendor:publish --provider="TaylorNetwork\UsernameSuggester\UsernameSuggesterProvider"

Set Up

You will need to follow the quickstart guide for taylornetwork/laravel-username-generator.

At minimum your User model needs to use the TaylorNetwork\UsernameGenerator\FindSimilarUsernames (see set up)

Defaults

The suggester will generate 3 unique usernames based on the given name. It will use the increment driver which will use the TaylorNetwork\UsernameGenerator\Generator class to convert the name to a username and then add incrementing numbers on the end to make them unique.

If no name is entered it will generate random usernames, same as TaylorNetwork\UsernameGenerator\Generator

Usage

Available Methods

suggest

The suggest() method accepts an optional parameter of the name to suggest usernames for.

$suggester = new \TaylorNetwork\UsernameSuggester\Suggester();

$suggester->suggest(); // Returns a collection of random unique usernames

$suggester->suggest('test user'); // Returns a collection of unique usernames based on the name 'test user'

setDriver

This will allow you to set a different driver than the default.

$suggester->setDriver('random');

This method returns an instance of the Suggester class, so you're able to chain methods.

setAmount

This will allow you to set a different amount of suggestions than the default.

$suggester->setAmount(10);

This method returns an instance of the Suggester class, so you're able to chain methods.

setGeneratorConfig

This will allow you to override the TaylorNetwork\UsernameGenerator\Generator config.

$suggester->setGeneratorConfig([
    'separator' => '*',
]);

Same as setAmount and setDriver this method also returns the Suggester instance.

Example

Basic with Random Usernames

$suggester->suggest();

Basic with Entered Name

$suggester->suggest('test user');

Different Driver and Amount

$suggester->setDriver('random')->setAmount(5)->suggest('test user');

This will use the Random driver to append random numbers after the username and generate 5 usernames.

Using the Facade

A UsernameSuggester facade is included so all the methods can be accessed that way.

UsernameSuggester::suggest();

Custom Drivers

You can create any custom drivers by extending the TaylorNetwork\UsernameSuggester\Drivers\BaseDriver class.

namespace App\SuggesterDrivers;

use TaylorNetwork\UsernameSuggester\Drivers\BaseDriver;

class CustomDriver extends BaseDriver
{
    public function makeUnique(string $username): string
    {
        return $username;
    }
}

The only requirement is that you implement the makeUnique method to make the username unique in some way.

You'll also need to register the driver in app/username_suggester.php

'drivers' => [
    'custom' => CustomDriver::class,
    // ...
],

You can access it using the key you set.

UsernameSuggester::setDriver('custom')->suggest();

username-suggester's People

Contributors

itssamtaylor avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

username-suggester's Issues

Can not be installed

hi,
i can't install this package
laravel 9.16
i installed taylornetwork/laravel-username-generator

composer require taylornetwork/username-suggester

i get this error:

Your requirements could not be resolved to an installable set of packages.

Problem 1
- Root composer.json requires taylornetwork/username-suggester ^0.1.0 -> satisfiable by taylornetwork/username-suggester[0.1].
- taylornetwork/username-suggester 0.1 requires illuminate/support ^8.58 -> found illuminate/support[v8.58.0, ..., 8.x-dev] but these were not loaded, likely b
ecause it conflicts with another require.

You can also try re-running composer require with an explicit version constraint, e.g. "composer require taylornetwork/username-suggester:*" to figure out if any v
ersion is installable, or "composer require taylornetwork/username-suggester:^2.1" if you know which you need.

Installation failed, reverting ./composer.json and ./composer.lock to their original content.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.