├── src ├── UuidServiceProvider.php └── UuidDriver.php ├── LICENSE.md ├── composer.json └── README.md /src/UuidServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bind('hashid.driver.uuid', UuidDriver::class); 24 | } 25 | 26 | /** 27 | * Get the services provided by the provider. 28 | * 29 | * @return array 30 | */ 31 | public function provides() 32 | { 33 | return ['hashid.driver.uuid']; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2017-2022 Elf Sundae 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "elfsundae/laravel-hashid-uuid", 3 | "type": "library", 4 | "description": "Shorten UUID encoding for Laravel Hashid.", 5 | "keywords": ["uuid", "short", "laravel", "hashid"], 6 | "homepage": "https://github.com/ElfSundae/laravel-hashid-uuid", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Elf Sundae", 11 | "email": "elf.sundae@gmail.com", 12 | "homepage": "https://0x123.com" 13 | } 14 | ], 15 | "require": { 16 | "php": "~7.1|~8.0", 17 | "elfsundae/laravel-hashid": "~1.0", 18 | "ramsey/uuid": "~3.6|~4.0" 19 | }, 20 | "require-dev": { 21 | "mockery/mockery": "~1.0", 22 | "phpunit/phpunit": "~5.7|~6.0|~7.0|~8.0|~9.0" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "ElfSundae\\Laravel\\Hashid\\": "src/" 27 | } 28 | }, 29 | "autoload-dev": { 30 | "psr-4": { 31 | "ElfSundae\\Laravel\\Hashid\\Test\\": "tests/" 32 | } 33 | }, 34 | "scripts": { 35 | "test": "vendor/bin/phpunit" 36 | }, 37 | "extra": { 38 | "laravel": { 39 | "providers": [ 40 | "ElfSundae\\Laravel\\Hashid\\UuidServiceProvider" 41 | ] 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/UuidDriver.php: -------------------------------------------------------------------------------- 1 | manager = $manager; 36 | 37 | if (isset($config['connection'])) { 38 | $this->connection = $config['connection']; 39 | } else { 40 | throw new InvalidArgumentException('A connection name must be specified.'); 41 | } 42 | } 43 | 44 | /** 45 | * Encode the data. 46 | * 47 | * @param string|\Ramsey\Uuid\Uuid $data 48 | * @return string 49 | */ 50 | public function encode($data) 51 | { 52 | $uuid = $data instanceof Uuid ? $data : Uuid::fromString($data); 53 | 54 | return $this->getConnection()->encode($uuid->getBytes()); 55 | } 56 | 57 | /** 58 | * Decode the data. 59 | * 60 | * @param string $data 61 | * @return \Ramsey\Uuid\Uuid 62 | */ 63 | public function decode($data) 64 | { 65 | try { 66 | return Uuid::fromBytes($this->getConnection()->decode($data)); 67 | } catch (Exception $e) { 68 | unset($e); 69 | } 70 | 71 | return Uuid::fromString(Uuid::NIL); 72 | } 73 | 74 | /** 75 | * Get the hashid connection instance. 76 | * 77 | * @return mixed 78 | */ 79 | protected function getConnection() 80 | { 81 | return $this->manager->connection($this->connection); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Hashid UUID 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/elfsundae/laravel-hashid-uuid.svg?style=flat-square)](https://packagist.org/packages/elfsundae/laravel-hashid-uuid) 4 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 5 | [![tests](https://github.com/ElfSundae/laravel-hashid-uuid/actions/workflows/tests.yml/badge.svg)](https://github.com/ElfSundae/laravel-hashid-uuid/actions/workflows/tests.yml) 6 | [![StyleCI](https://styleci.io/repos/110262872/shield)](https://styleci.io/repos/110262872) 7 | [![SymfonyInsight Grade](https://img.shields.io/symfony/i/grade/57a207e1-f852-42c4-8260-a078b7dff9df?style=flat-square)](https://insight.symfony.com/projects/57a207e1-f852-42c4-8260-a078b7dff9df) 8 | [![Quality Score](https://img.shields.io/scrutinizer/g/ElfSundae/laravel-hashid-uuid.svg?style=flat-square)](https://scrutinizer-ci.com/g/ElfSundae/laravel-hashid-uuid) 9 | [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/ElfSundae/laravel-hashid-uuid/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/ElfSundae/laravel-hashid-uuid/?branch=master) 10 | [![Total Downloads](https://img.shields.io/packagist/dt/elfsundae/laravel-hashid.svg?style=flat-square)](https://packagist.org/packages/elfsundae/laravel-hashid) 11 | 12 | This is a plugin package for [Laravel Hashid][hashid], it provides an `uuid` hashid driver to shorten your [UUID] encoding. 13 | 14 | ## Installation 15 | 16 | You can install this package using the [Composer](https://getcomposer.org) manager: 17 | 18 | ```sh 19 | $ composer require elfsundae/laravel-hashid-uuid 20 | ``` 21 | 22 | For Lumen or earlier Laravel than v5.5, you need to register the service provider manually: 23 | 24 | ```php 25 | ElfSundae\Laravel\Hashid\UuidServiceProvider::class, 26 | ``` 27 | 28 | ## Configuration 29 | 30 | - Driver name: `uuid` 31 | - Configuration: 32 | - `connection` : The hashid connection name for encoding and decoding. 33 | 34 | **Example:** 35 | 36 | ```php 37 | 'connections' => [ 38 | 39 | 'uuid_base62' => [ 40 | 'driver' => 'uuid', 41 | 'connection' => 'base62', 42 | ], 43 | 44 | 'uuid_hashids' => [ 45 | 'driver' => 'uuid', 46 | 'connection' => 'hashids_string', 47 | ], 48 | 49 | // ... 50 | ], 51 | ``` 52 | 53 | ## Usage 54 | 55 | - `encode($data)` accepts a [`Ramsey\Uuid\Uuid`][uuid] instance or an UUID string. 56 | - `decode($data)` returns a [`Ramsey\Uuid\Uuid`][uuid] instance. 57 | 58 | **Example:** 59 | 60 | ```php 61 | use Ramsey\Uuid\Uuid; 62 | 63 | $string = 'cd79e206-c715-11e7-891d-8bf37117635e'; 64 | $uuid = Uuid::fromString($string); 65 | $hex = $uuid->getHex(); // "cd79e206c71511e7891d8bf37117635e" 66 | 67 | // Encode using the original connections: 68 | hashid_encode($uuid, 'base62'); // "1mUcj8TfpKB7vEBlRecZ4PADhnE1UbBg2L9n3PQOSFqzYZHwj" 69 | hashid_encode($uuid, 'hashids_string'); // "Wr3xrA2WWEh4K1LBBV6vhXL592VVQoSKWnpQB5vkt9DkZxDp6Lsjz945vnRl" 70 | 71 | // Encode using the "uuid" driver: 72 | hashid_encode($uuid, 'uuid_base62'); // "6Fj7unqIaNKkq5zbJo1HJ8" 73 | hashid_encode($string, 'uuid_base62'); // "6Fj7unqIaNKkq5zbJo1HJ8" 74 | hashid_encode($hex, 'uuid_base62'); // "6Fj7unqIaNKkq5zbJo1HJ8" 75 | hashid_encode($uuid, 'uuid_hashids'); // "kp2wZkXOBzSMNA3nPxNzSOZJ701" 76 | 77 | // Decode 78 | $decoded = hashid_decode('6Fj7unqIaNKkq5zbJo1HJ8', 'uuid_base62'); 79 | (string) $decoded; // "cd79e206-c715-11e7-891d-8bf37117635e" 80 | $decoded->getDateTime()->format('Y-m-d H:i:s'); // "2017-11-11 19:23:31" 81 | 82 | // Decoding failure 83 | (string) hashid_decode('foobar', 'uuid_base62'); // "00000000-0000-0000-0000-000000000000" 84 | ``` 85 | 86 | ## Testing 87 | 88 | ```sh 89 | $ composer test 90 | ``` 91 | 92 | ## License 93 | 94 | This package is open-sourced software licensed under the [MIT License](LICENSE.md). 95 | 96 | [hashid]: https://github.com/ElfSundae/laravel-hashid 97 | [uuid]: https://github.com/ramsey/uuid 98 | --------------------------------------------------------------------------------