Coder Social home page Coder Social logo

base64uid's Introduction

Latest Stable Version Total Downloads Build Status Coverage Status Scrutinizer Code Quality SensioLabs Insight StyleCI License

Base64 UID

Generate UID like YouTube.

Introduction

The library generates a unique identifier consisting of 64 characters and a length of 10 characters (you can change the length of the identifier). This gives us a lot of combinations.

64^10 = 2^60 = 1 152 921 504 606 846 976 (combinations)

To represent this number, imagine that in order to get all possible values of identifiers with a length of 10 characters and generating an ID every microsecond, it takes 36 559 years.

UUID works on the same principle, but its main drawback is that it's too long. It is not convenient to use it as a public identifier, for example in the URL.

Due to the fact that Base64 UID uses 64 chars instead of 36, the identifier turns out to be noticeably shorter. Also you have the opportunity to manage the long identifier and the number of possible values. This will optimize the length of the identifier for your business requirements.

Installation

Pretty simple with Composer, run:

composer require gpslab/base64uid

Usage

use GpsLab\Component\Base64UID\Base64UID;

$uid = Base64UID::generate(); // iKtwBpOH2E

With length 6 chars

// 64^6 = 68 719 476 736 (combinations)
$uid = Base64UID::generate(6); // nWzfgA

The floating-length identifier will give more unique identifiers.

// 64^10 + 64^9 + 64^8 = 1 171 217 378 093 039 616 (combinations)
$uid = Base64UID::generate(random_int(8, 10));

You can customize charset.

$charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/';
$uid = Base64UID::generate(11, $charset);

$charset = '0123456789abcdef';
$uid = Base64UID::generate(11, $charset);

Domain-driven design (DDD)

How to usage in your domain.

For example create a ArticleId ValueObject:

class ArticleId
{
    private $id;

    public function __construct(string $id)
    {
        $this->id = $id;
    }

    public function id()
    {
        return $this->id;
    }
}

Repository interface for Article:

interface ArticleRepository
{
    public function nextId();

    // more methods ...
}

Concrete repository for Article:

use GpsLab\Component\Base64UID\Base64UID;

class ConcreteArticleRepository implements ArticleRepository
{
    public function nextId()
    {
        return new ArticleId(Base64UID::generate());
    }

    // more methods ...
}

Now we can create a new entity with ArticleId:

$article = new Article(
    $repository->nextId(),
    // more article parameters ...
);

License

This bundle is under the MIT license. See the complete license in the file: LICENSE

base64uid's People

Contributors

peter-gribanov avatar

Watchers

 avatar

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.