Coder Social home page Coder Social logo

laravel-hashid-uuid's Introduction

Latest Version on Packagist Software License Build Status StyleCI SensioLabsInsight Quality Score Code Coverage

This is a plugin package for Laravel Hashid, it provides an uuid hashid driver to shorten your UUID encoding.

Installation

You can install this package using the Composer manager:

$ composer require elfsundae/laravel-hashid-uuid

For Lumen or earlier Laravel than v5.5, you need to register the service provider manually:

ElfSundae\Laravel\Hashid\UuidServiceProvider::class,

Configuration

  • Driver name: uuid
  • Configuration:
    • connection : The hashid connection name for encoding and decoding.

Example:

'connections' => [

    'uuid_base62' => [
        'driver' => 'uuid',
        'connection' => 'base62',
    ],

    'uuid_hashids' => [
        'driver' => 'uuid',
        'connection' => 'hashids_string',
    ],

    // ...
],

Usage

Example:

use Ramsey\Uuid\Uuid;

$string = 'cd79e206-c715-11e7-891d-8bf37117635e';
$uuid = Uuid::fromString($string);
$hex = $uuid->getHex();                 // "cd79e206c71511e7891d8bf37117635e"

// Encode using the original connections:
hashid_encode($uuid, 'base62');         // "1mUcj8TfpKB7vEBlRecZ4PADhnE1UbBg2L9n3PQOSFqzYZHwj"
hashid_encode($uuid, 'hashids_string'); // "Wr3xrA2WWEh4K1LBBV6vhXL592VVQoSKWnpQB5vkt9DkZxDp6Lsjz945vnRl"

// Encode using the "uuid" driver:
hashid_encode($uuid, 'uuid_base62');    // "6Fj7unqIaNKkq5zbJo1HJ8"
hashid_encode($string, 'uuid_base62');  // "6Fj7unqIaNKkq5zbJo1HJ8"
hashid_encode($hex, 'uuid_base62');     // "6Fj7unqIaNKkq5zbJo1HJ8"
hashid_encode($uuid, 'uuid_hashids');   // "kp2wZkXOBzSMNA3nPxNzSOZJ701"

// Decode
$decoded = hashid_decode('6Fj7unqIaNKkq5zbJo1HJ8', 'uuid_base62');
(string) $decoded;                               // "cd79e206-c715-11e7-891d-8bf37117635e"
$decoded->getDateTime()->format('Y-m-d H:i:s');  // "2017-11-11 19:23:31"

// Decoding failure
(string) hashid_decode('foobar', 'uuid_base62'); // "00000000-0000-0000-0000-000000000000"

Testing

$ composer test

License

This package is open-sourced software licensed under the MIT License.

laravel-hashid-uuid's People

Contributors

cbljj avatar elfsundae avatar vpratfr avatar

Watchers

 avatar  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.