1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Utils/Traits/MakesHash.php
David Bomba ba75a44eb8
Laravel 7.x Shift (#40)
* Adopt Laravel coding style

The Laravel framework adopts the PSR-2 coding style with some additions.
Laravel apps *should* adopt this coding style as well.

However, Shift allows you to customize the adopted coding style by
adding your own [PHP CS Fixer][1] `.php_cs` config to your project.

You may use [Shift's .php_cs][2] file as a base.

[1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer
[2]: https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200

* Shift bindings

PHP 5.5.9+ adds the new static `class` property which provides the fully qualified class name. This is preferred over using class name strings as these references are checked by the parser.

* Shift core files

* Shift to Throwable

* Add laravel/ui dependency

* Unindent vendor mail templates

* Shift config files

* Default config files

In an effort to make upgrading the constantly changing config files
easier, Shift defaulted them so you can review the commit diff for
changes. Moving forward, you should use ENV variables or create a
separate config file to allow the core config files to remain
automatically upgradeable.

* Shift Laravel dependencies

* Shift cleanup

* Upgrade to Laravel 7

Co-authored-by: Laravel Shift <shift@laravelshift.com>
2020-09-06 19:38:10 +10:00

91 lines
2.3 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Utils\Traits;
use App\Exceptions\ModelNotFoundException;
use App\Libraries\MultiDB;
use Hashids\Hashids;
/**
* Class MakesHash.
*/
trait MakesHash
{
/**
* Creates a simple alphanumeric Hash.
* @return string - asd89f7as89df6asf78as6fds
*/
public function createHash() : string
{
return \Illuminate\Support\Str::random(config('ninja.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).'-'.\Illuminate\Support\Str::random(config('ninja.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(config('ninja.hash_salt'), 10);
return $hashids->encode(str_replace(MultiDB::DB_PREFIX, '', $db));
}
public function encodePrimaryKey($value) : string
{
$hashids = new Hashids(config('ninja.hash_salt'), 10);
return $hashids->encode($value);
}
public function decodePrimaryKey($value) : string
{
try {
$hashids = new Hashids(config('ninja.hash_salt'), 10);
$decoded_array = $hashids->decode($value);
if (! is_array($decoded_array)) {
throw new ModelNotFoundException('Resource not found', 1);
}
return $decoded_array[0];
} catch (\Exception $e) {
return response()->json(['error'=>'Invalid primary key'], 400);
}
}
public function transformKeys($keys)
{
if (is_array($keys)) {
foreach ($keys as &$value) {
$value = $this->decodePrimaryKey($value);
}
return $keys;
} else {
return $this->decodePrimaryKey($keys);
}
}
}