Git Product home page Git Product logo

shortuuid-php's Introduction

shortuuid-php

Build Status Build Status

The PHP implementation of shortuuid.

Installation

composer require pyyoshi/shortuuid-php

or add this library in your composer.json

{
    "require": {
        "pyyoshi/shortuuid-php": "*"
    }
}

Usage

import

require dirname(__DIR__) . "/vendor/autoload.php";
require_once 'ShortUUID/Autoloader.php';
\ShortUUID\Autoloader::register();

You can then generate a short UUID:

use ShortUUID\ShortUUID;

$su = new ShortUUID();
$su->uuid();
=> "rkQdp5ikpXjraCsrSaysaT"

If you prefer a version 5 UUID, you can pass a name (DNS or URL) to the call and it will be used as a namespace (uuid.NAMESPACE_DNS or uuid.NAMESPACE_URL) for the resulting UUID:

use ShortUUID\ShortUUID;

$su = new ShortUUID();
$su->uuid('example.com');
=> "wpsWLdLt9nscn2jbTD3uxe"
$su->uuid('http://www.example.com/');
=> "VSPugLzk4dD4WC7yfAQUzn"

To see the alphabet that is being used to generate new UUIDs:

use ShortUUID\ShortUUID;

$su = new ShortUUID();
$su->getAlphabet();
=> "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

If you want to use your own alphabet to generate UUIDs, use setAlphabet():

use ShortUUID\ShortUUID;

$su = new ShortUUID();
$su->setAlphabet('aaaaabcdefgh1230123');
$su->uuid();
=> "ee120aeh2h3bb010fdfedef2c03efcf3h1ca"

shortuuid-php will automatically sort and remove duplicates from your alphabet to ensure consistency:

use ShortUUID\ShortUUID;

$su = new ShortUUID();
$su->setAlphabet('aaaaabcdefgh1230123');
$su->getAlphabet();
=> "0123abcdefgh"

If the default 22 digits are too long for you, you can get shorter IDs by just truncating the string to the desired length. The IDs won't be universally unique any longer, but the probability of a collision will still be very low.

To serialize existing UUIDs, use encode() and decode():

use Ramsey\Uuid\Uuid;
use ShortUUID\ShortUUID;

$u = Uuid::uuid4();
$su = new ShortUUID();
$s = $su->encode($u);

$su->decode($s) == $u;
=> true

License

shortuuid-php is distributed under the BSD license.

shortuuid-php's People

Contributors

adriankeenan avatar hipio avatar inoas avatar pyyoshi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

shortuuid-php's Issues

[Composer][Stability problem] Please create a (new) release

If I currently do composer require pyyoshi/shortuuid-php I'm being greeted with a very nice error (which can be solved by updating your composer.json, see #1) and releasing a new version AFAIK.

Problem 1
    - pyyoshi/shortuuid-php 1.0.4 requires ramsey/uuid 3.0.1 -> satisfiable by ramsey/uuid[3.0.1].
    - Conclusion: don't install ramsey/uuid 3.0.1
    - pyyoshi/shortuuid-php 1.0.3 requires ramsey/uuid 3.0.0 -> satisfiable by ramsey/uuid[3.0.0].
    - Conclusion: don't install ramsey/uuid 3.0.0
    - Conclusion: remove ramsey/uuid-doctrine 1.2.0
    - Conclusion: don't install ramsey/uuid-doctrine 1.2.0
    - pyyoshi/shortuuid-php 1.0.2 requires ramsey/uuid 2.8.3 -> satisfiable by ramsey/uuid[2.8.3].
    - Conclusion: don't install ramsey/uuid 2.8.3
    - Installation request for ramsey/uuid (locked at 3.5.1) -> satisfiable by ramsey/uuid[3.5.1].
    - Installation request for ramsey/uuid-doctrine ^1.2 -> satisfiable by ramsey/uuid-doctrine[1.2.0].
    - pyyoshi/shortuuid-php 1.0.1 requires ramsey/uuid 2.8.1 -> satisfiable by ramsey/uuid[2.8.1].
    - Conclusion: don't install ramsey/uuid 2.8.1
    - ramsey/uuid-doctrine 1.2.0 requires ramsey/uuid ^3.0 -> satisfiable by ramsey/uuid[3.5.1, 3.0.0, 3.0.1, 3.1.0, 3.2.0, 3.3.0, 3.4.0, 3.4.1, 3.5.0].
    - don't install rhumsaa/uuid 2.8.0|don't install ramsey/uuid 3.1.0
    - don't install rhumsaa/uuid 2.8.0|don't install ramsey/uuid 3.2.0
    - don't install rhumsaa/uuid 2.8.0|don't install ramsey/uuid 3.3.0
    - don't install rhumsaa/uuid 2.8.0|don't install ramsey/uuid 3.4.0
    - don't install rhumsaa/uuid 2.8.0|don't install ramsey/uuid 3.4.1
    - don't install rhumsaa/uuid 2.8.0|don't install ramsey/uuid 3.5.0
    - don't install rhumsaa/uuid 2.8.0|don't install ramsey/uuid 3.5.1
    - don't install rhumsaa/uuid 2.8.0|remove ramsey/uuid 3.5.1
    - don't install rhumsaa/uuid 2.8.0|don't install ramsey/uuid 3.5.1
    - pyyoshi/shortuuid-php 1.0.0 requires rhumsaa/uuid 2.8 -> satisfiable by rhumsaa/uuid[2.8.0].
    - Installation request for pyyoshi/shortuuid-php ^1.0 -> satisfiable by pyyoshi/shortuuid-php[1.0.0, 1.0.1, 1.0.2, 1.0.3, 1.0.4].


Installation failed, reverting ./composer.json to its original content.```

[Symfony (and others?)] ClassNotFoundException

I keep getting a ClassNotFoundException even though I'm using a correct use-statement within my class. Any idea why this might happen?

  [Symfony\Component\Debug\Exception\ClassNotFoundException]       
  Attempted to load class "ShortUUID" from namespace "ShortUUID".  
  Did you forget a "use" statement for another namespace?    

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.