getDbCode($db).'-'.Str::random(config('ninja.key_length')); } return 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, $return_string_failure = false) { try { $hashids = new Hashids(config('ninja.hash_salt'), 10); $decoded_array = $hashids->decode($value); if(isset($decoded_array[0]) ?? false) { return $decoded_array[0]; } elseif($return_string_failure) { return "Invalid Primary Key"; } else { throw new \Exception('Invalid Primary Key'); } } catch (\Exception $e) { return response()->json(['error' => 'Invalid primary key'], 400); } /* try { $hashids = new Hashids(config('ninja.hash_salt'), 10); $decoded_array = $hashids->decode($value); if (! is_array($decoded_array)) { throw new \Exception('Invalid Primary Key'); } 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); } } }