├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── phpcs.xml ├── phpstan.neon ├── phpunit.xml ├── src ├── Torann │ └── Hashids │ │ ├── Facade │ │ └── Hashids.php │ │ └── HashidsServiceProvider.php └── config │ └── hashids.php └── tests └── .gitkeep /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .phpunit.result.cache 5 | .DS_Store 6 | .idea/* -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 7.4 5 | - 8.0 6 | 7 | before_script: 8 | - curl -s http://getcomposer.org/installer | php 9 | - php composer.phar install 10 | 11 | script: 12 | - ./vendor/bin/phpcs --standard=phpcs.xml src 13 | - ./vendor/bin/phpstan --level=0 --memory-limit=-1 --no-progress analyse src 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The BSD 2-Clause License 2 | Copyright (c) 2013, Daniel Stainback 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 6 | 7 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 8 | 9 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 10 | 11 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hashids for Laravel 2 | 3 | [![Latest Stable Version](https://poser.pugx.org/torann/hashids/v/stable.png)](https://packagist.org/packages/torann/hashids) [![Total Downloads](https://poser.pugx.org/torann/hashids/downloads.png)](https://packagist.org/packages/torann/hashids) 4 | 5 | This package uses the classes created by [hashids.org](http://www.hashids.org/ "http://www.hashids.org/") 6 | 7 | Generate hashes from numbers, like YouTube or Bitly. Use hashids when you do not want to expose your database ids to the user. 8 | 9 | ---------- 10 | 11 | ## Installation 12 | 13 | - [Hashids on Packagist](https://packagist.org/packages/torann/hashids) 14 | - [Hashids on GitHub](https://github.com/torann/laravel-hashids) 15 | 16 | ### Composer 17 | 18 | From the command line run: 19 | 20 | ```bash 21 | $ composer require torann/hashids 22 | ``` 23 | 24 | **Without Package Auto-Discovery** 25 | 26 | Once Hashids is installed you need to register the service provider and facade with the application. Open up `config/app.php` and find the `providers` and `aliases` keys. 27 | 28 | ```php 29 | 'providers' => [ 30 | Torann\Hashids\HashidsServiceProvider::class, 31 | ] 32 | 33 | 'aliases' => [ 34 | 'Hashids' => Torann\Hashids\Facade\Hashids::class, 35 | ] 36 | ``` 37 | 38 | ### Publish the configurations 39 | 40 | Run this on the command line from the root of your project: 41 | 42 | ``` 43 | $ php artisan vendor:publish --provider="Torann\Hashids\HashidsServiceProvider" 44 | ``` 45 | 46 | A configuration file will be publish to `config/hashids.php`. 47 | 48 | ## Usage 49 | 50 | Once you've followed all the steps and completed the installation you can use Hashids. 51 | 52 | ### Encoding 53 | 54 | You can simply encode a single id: 55 | 56 | ```php 57 | Hashids::encode(1); // Returns Ri7Bi 58 | ``` 59 | 60 | or multiple.. 61 | 62 | ```php 63 | Hashids::encode(1, 21, 12, 12, 666); // Returns MMtaUpSGhdA 64 | ``` 65 | 66 | ### Decoding 67 | 68 | ```php 69 | Hashids::decode(Ri7Bi); 70 | 71 | // Returns 72 | array (size=1) 73 | 0 => int 1 74 | ``` 75 | 76 | or multiple.. 77 | 78 | ```php 79 | Hashids::decode(MMtaUpSGhdA); 80 | 81 | // Returns 82 | array (size=5) 83 | 0 => int 1 84 | 1 => int 21 85 | 2 => int 12 86 | 3 => int 12 87 | 4 => int 666 88 | ``` 89 | 90 | All credit for Hashids goes to Ivan Akimov (@ivanakimov), thanks to for making it! 91 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "torann/hashids", 3 | "description": "Laravel package for Hashids", 4 | "keywords": [ 5 | "laravel", 6 | "hash", 7 | "hashid", 8 | "encode", 9 | "decode" 10 | ], 11 | "license": "BSD-2-Clause", 12 | "authors": [ 13 | { 14 | "name": "Daniel Stainback", 15 | "email": "torann@gmail.com" 16 | } 17 | ], 18 | "require": { 19 | "php": "^7.4|^8.0", 20 | "illuminate/support": "^7.0|^8.0|^9.0|^10.0|^11.0", 21 | "hashids/hashids": "~4.0" 22 | }, 23 | "require-dev": { 24 | "phpunit/phpunit": "^8.0", 25 | "mockery/mockery": "^1.3", 26 | "phpstan/phpstan": "^0.12.14", 27 | "squizlabs/php_codesniffer": "^3.5" 28 | }, 29 | "autoload": { 30 | "psr-4": { 31 | "Torann\\Hashids\\": "src/Torann/Hashids" 32 | } 33 | }, 34 | "extra": { 35 | "laravel": { 36 | "providers": [ 37 | "Torann\\Hashids\\HashidsServiceProvider" 38 | ], 39 | "aliases": { 40 | "Hashids": "Torann\\Hashids\\Facade\\Hashids" 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /phpcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | The PSR2 standard 4 | 5 | 6 | /resources/ 7 | /vendor/ 8 | /_[a-zA-Z0-9\._]+\.php 9 | /\.[a-zA-Z0-9\._]+\.php 10 | \.git 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /phpstan.neon: -------------------------------------------------------------------------------- 1 | parameters: 2 | excludes_analyse: 3 | - vendor 4 | - resources 5 | ignoreErrors: 6 | - '#Function config_path not found#' 7 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | ./tests/ 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/Torann/Hashids/Facade/Hashids.php: -------------------------------------------------------------------------------- 1 | isLumen() === false) { 20 | $this->publishes([ 21 | __DIR__ . '/../../config/hashids.php' => config_path('hashids.php'), 22 | ]); 23 | } 24 | } 25 | 26 | /** 27 | * Register the service provider. 28 | * 29 | * @return void 30 | */ 31 | public function register() 32 | { 33 | $this->app->bind('hashids', function ($app) { 34 | $config = $app->config->get('hashids'); 35 | 36 | return new Hashids( 37 | Arr::get($config, 'salt'), 38 | Arr::get($config, 'length', 0), 39 | Arr::get($config, 'alphabet') ?: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' 40 | ); 41 | }); 42 | } 43 | 44 | /** 45 | * Check if package is running under Lumen app 46 | * 47 | * @return bool 48 | */ 49 | protected function isLumen() 50 | { 51 | return Str::contains($this->app->version(), 'Lumen') === true; 52 | } 53 | 54 | /** 55 | * Get the services provided by the provider. 56 | * 57 | * @return array 58 | */ 59 | public function provides() 60 | { 61 | return [ 62 | 'hashids', 63 | ]; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/config/hashids.php: -------------------------------------------------------------------------------- 1 | null, 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Hash Length 21 | |-------------------------------------------------------------------------- 22 | | 23 | | By default, hashes are going to be the shortest possible. You can also 24 | | set the minimum hash length to obfuscate how large the number behind 25 | | the hash is. 26 | | 27 | */ 28 | 29 | 'length' => 0, 30 | 31 | /* 32 | |-------------------------------------------------------------------------- 33 | | Custom alphabet 34 | |-------------------------------------------------------------------------- 35 | | 36 | | The default alphabet contains all lowercase and uppercase letters 37 | | and numbers. If you'd like to make it longer, you can add more 38 | | characters like - @ $ % ^ & * ( ) [ ] { } 39 | | 40 | */ 41 | 42 | 'alphabet' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', 43 | 44 | ]; 45 | -------------------------------------------------------------------------------- /tests/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Torann/laravel-hashids/51d70eb2641a883da0d0ac73584f2e344490bd36/tests/.gitkeep --------------------------------------------------------------------------------