Git Product home page Git Product logo

nanoid-php's Introduction

Nanoid-php

Build Status

A tiny (179 bytes), secure URL-friendly unique string ID generator for JavaScript

Safe. It uses cryptographically strong random APIs and guarantees a proper distribution of symbols.

Small. Only 179 bytes (minified and gzipped). No dependencies. It uses Size Limit to control size.

Compact. It uses more symbols than UUID (A-Za-z0-9_-) and has the same number of unique options in just 21 symbols instead of 36.

Thanks awesome ai and his nanoid, this package is a copy in PHP! If you like nanoid and you want to use it in PHP, try me :D

Install

Via Composer

$composer require hidehalo/nanoid-php

Usage

Normal

The main module uses URL-friendly symbols (A-Za-z0-9_-) and returns an ID with 21 characters (to have the same collisions probability as UUID v4).

use Hidehalo\Nanoid\Client;
use Hidehalo\Nanoid\GeneratorInterface;

$client = new Client();

# default random generator
echo $client->generateId($size = 21);
# more safer random generator
echo $client->generateId($size = 21, $mode = Client::MODE_DYNAMIC);

Custom Alphabet or Length

echo $client->formattedId($alphabet = '0123456789abcdefg', $size = 21);

Alphabet must contain 256 symbols or less. Otherwise, the generator will not be secure.

Custom Random Bytes Generator

# PS: anonymous class is new feature when PHP_VERSION >= 7.0
echo $client->formattedId($alphabet = '0123456789abcdefg', $size = 21,
new class implements GeneratorInterface {
    /**
     * @inheritDoc
     */
    public function random($size)
    {
        //TODO: implemenation ...
    }
});

random callback must accept the array size and return an array with random numbers.

If you want to use the same URL-friendly symbols with format, you can get default alphabet from the url module:

Please see CoreInterface::random(...) for the core random API prototype and notes

Examples

Please see Examples for more information on detailed usage.

Change log

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

Testing

$composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Notice

If you have any issues, just feel free and open it in this repository, thx!

Credits

License

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

nanoid-php's People

Contributors

hidehalo avatar andrewminion-luminfire avatar gabrielsosa-stensul avatar igusev avatar

Watchers

James Cloos 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.