Created
October 23, 2017 12:30
-
-
Save XavRsl/fe570d22e39f257fb19491e4128e4ed0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services; | |
use Hashids\Hashids; | |
use Illuminate\Database\Eloquent\Model; | |
class ModelHasher | |
{ | |
/** | |
* Store Hashids object. | |
* | |
* @var Hashids | |
*/ | |
protected $hashids; | |
/** | |
* Default length of generated hash. | |
* | |
* @var int | |
*/ | |
protected $hashLength = 40; | |
public function __construct() | |
{ | |
$this->hashids = new Hashids(config('app.key'), $this->hashLength); | |
} | |
/** | |
* Generate a token from a Model id. | |
* | |
* @param Model $model | |
* @return string | |
*/ | |
public function encode(Model $model) | |
{ | |
return $this->hashids->encode($model->id); | |
} | |
/** | |
* Decode hash into a Model id. | |
* | |
* @param $hash | |
* @return mixed | |
*/ | |
public function decode($hash) | |
{ | |
return head($this->hashids->decode($hash)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment