2018-10-26 15:53:29 +11:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Utils\Traits;
|
|
|
|
|
|
|
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
|
use Hashids\Hashids;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class MakesHash
|
|
|
|
|
* @package App\Utils\Traits
|
|
|
|
|
*/
|
|
|
|
|
trait MakesHash
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Creates a simple alphanumeric Hash
|
|
|
|
|
* @return string - asd89f7as89df6asf78as6fds
|
|
|
|
|
*/
|
|
|
|
|
public function createHash() : string
|
|
|
|
|
{
|
2019-04-24 12:34:39 +10:00
|
|
|
return str_random(config('ninja.key_length'));
|
2018-10-26 15:53:29 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a simple alphanumeric Hash which is prepended with a encoded database prefix
|
2019-04-18 15:01:40 +10:00
|
|
|
*
|
2018-10-26 15:53:29 +11:00
|
|
|
* @param $db - Full database name
|
|
|
|
|
* @return string 01-asfas8df76a78f6a78dfsdf
|
|
|
|
|
*/
|
|
|
|
|
public function createDbHash($db) : string
|
|
|
|
|
{
|
2019-04-24 12:34:39 +10:00
|
|
|
return $this->getDbCode($db) . '-' . str_random(config('ninja.key_length'));
|
2018-10-26 15:53:29 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $db - Full database name
|
|
|
|
|
* @return string - hashed and encoded int 01,02,03,04
|
|
|
|
|
*/
|
|
|
|
|
public function getDbCode($db) : string
|
|
|
|
|
{
|
2018-11-11 00:24:36 +11:00
|
|
|
$hashids = new Hashids('', 10);
|
2018-10-26 15:53:29 +11:00
|
|
|
|
|
|
|
|
return $hashids->encode( str_replace( MultiDB::DB_PREFIX, "", $db ) );
|
|
|
|
|
}
|
2018-11-02 21:54:46 +11:00
|
|
|
|
2018-11-27 18:24:26 +11:00
|
|
|
public function encodePrimaryKey($value) : string
|
2018-11-02 21:54:46 +11:00
|
|
|
{
|
2018-11-11 00:24:36 +11:00
|
|
|
$hashids = new Hashids('', 10);
|
2018-11-02 21:54:46 +11:00
|
|
|
|
|
|
|
|
return $hashids->encode($value);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-27 18:24:26 +11:00
|
|
|
public function decodePrimaryKey($value) : string
|
2018-11-02 21:54:46 +11:00
|
|
|
{
|
2019-04-03 12:17:21 +11:00
|
|
|
try{
|
|
|
|
|
$hashids = new Hashids('', 10);
|
2018-11-02 21:54:46 +11:00
|
|
|
|
2019-04-03 12:17:21 +11:00
|
|
|
$decoded_array = $hashids->decode($value);
|
2018-11-02 21:54:46 +11:00
|
|
|
|
2019-04-03 12:17:21 +11:00
|
|
|
return $decoded_array[0];
|
|
|
|
|
}
|
|
|
|
|
catch(\Exception $e)
|
|
|
|
|
{
|
|
|
|
|
return response()->json(['error'=>'Invalid primary key'],400);
|
|
|
|
|
}
|
2018-11-02 21:54:46 +11:00
|
|
|
}
|
2018-10-26 15:53:29 +11:00
|
|
|
}
|