mastersteelblade/discriminator

A simple package to handle basic discriminator manipulation for user management.

v1.0.2 2021-03-20 07:46 UTC

This package is auto-updated.

Last update: 2024-09-20 15:47:14 UTC


README

Latest Stable Version Total Downloads License

About

This is a simple utility class to allow developers to implement discriminators on users, similar to Discord. This allows multiple users to share the same nickname, while allowing accounts to be tied to, for example, email addresses for uniqueness.

Installation using Composer

composer require sonata-project/google-authenticator

Usage

Discriminator is very simple to use. Below are some examples, but first, make things easier on yourself:

use Steelblade/Discriminator/Discriminator;

If you use another package that has a class of the same name, substitute the classes below with the full namespace.

Generate a discriminator

Generating a discriminator can be done in one of two ways. First, you can simply create an object without passing a parameter in.

$discriminator = new Discriminator();

As no parameter has been passed, it will generate one automatically, in a range of 0 to 9999 inclusive.

You can then, if disired, get the integer value using

$intVal = $discriminator->get();

Alternatively, to generate a discriminator without instantiating, a static method exists to return an integer value.

$discriminator = Discriminator::generate();

Loading a user from a database

Lets say a you want to display a profile page for a user, and you retrieve their information from a database.

class User {
    private $ID;
    private $emailAddress;
    public $nickname;
    public $discriminator;

    ...

    public function login($emailAddress, $password) {
        // Retrieve the user from the database and verify their password. 
        if ($successfulLogin) {
            $this->emailAddress = $databaseRow['email'];
            $this->nickname = $databaseRow['nickname'];
            $this->discriminator = new Discriminator($databaseRow['discriminator']);
        }
    }
}

The discriminator reads the numeric value, and creates itself accordingly.

Displaying a discriminator

Discriminators can be included in strings. For example:

return "The discriminator for $user->nickname is $user->discriminator";
// The discriminator for Master Steelblade is 0451

Representations of discriminators add leading zeroes to make the length consistent.

You can also retrieve the string value statically, by providing an integer value:

$integer = 47;
$discriminator = Discriminator::format($integer);
// Returns 0047 as a string

License

Discriminator is provided under the MIT license.