invoiceninja/app/Utils/Traits/MakesHash.php
David Bomba d430600e1e
Datatables, Base Model, Base Presenter (#2484)
* Fixes for datatables

* Implement a BaseModel

* Working on reusable header data model

* Working on adding session variables

* Clean up header data

* Random Data Seeder

* working on searching datatables across relationships.

* Working on transforming primary keys between client and server facinglogic

* Updated assets
2018-11-02 21:54:46 +11:00

59 lines
1.3 KiB
PHP

<?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
{
return strtolower(str_random(RANDOM_KEY_LENGTH));
}
/**
* Creates a simple alphanumeric Hash which is prepended with a encoded database prefix
* @param $db - Full database name
* @return string 01-asfas8df76a78f6a78dfsdf
*/
public function createDbHash($db) : string
{
return $this->getDbCode($db) . '-' . strtolower(str_random(RANDOM_KEY_LENGTH));
}
/**
* @param $db - Full database name
* @return string - hashed and encoded int 01,02,03,04
*/
public function getDbCode($db) : string
{
$hashids = new Hashids();
return $hashids->encode( str_replace( MultiDB::DB_PREFIX, "", $db ) );
}
public function encodePrimaryKey($value)
{
$hashids = new Hashids();
return $hashids->encode($value);
}
public function decodePrimaryKey($value)
{
$hashids = new Hashids();
$decoded_array = $hashids->decode($value);
return $decoded_array[0];
}
}