Skip to content

Instantly share code, notes, and snippets.

@XavRsl
Created October 23, 2017 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XavRsl/fe570d22e39f257fb19491e4128e4ed0 to your computer and use it in GitHub Desktop.
Save XavRsl/fe570d22e39f257fb19491e4128e4ed0 to your computer and use it in GitHub Desktop.
<?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