resq/src/Service/HashGenerator.php

32 lines
685 B
PHP

<?php
namespace App\Service;
use Hashids\Hashids;
class HashGenerator
{
// TODO: set in env file and set in constructor
protected $salt = 'salt that should be in the env file 230498';
protected $length = 15;
public function getHash($id)
{
$hi = new Hashids($this->salt, $this->length);
return $hi->encode($id);
}
public function getID($hash)
{
$hi = new Hashids($this->salt, $this->length);
$id_array = $hi->decode($hash);
// return null if unable to decode aka invalid hash
if (empty($id_array))
return null;
// first one should be the id
return $id_array[0];
}
}