c
This commit is contained in:
56
inc/Hura8/System/IDGenerator.php
Normal file
56
inc/Hura8/System/IDGenerator.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Hura8\System;
|
||||
|
||||
use Hidehalo\Nanoid\Client;
|
||||
|
||||
class IDGenerator
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $prefix - S = sale-order, P = purchase-order
|
||||
* @param int $increment_id result of IncrementIDModel/getId
|
||||
* @param int $length max length for the result-id, if length=6, prefix=P, $increment_id=20 => final id = P000020
|
||||
* @return string
|
||||
*/
|
||||
public static function createItemId(string $prefix, int $increment_id, $length = 6): string
|
||||
{
|
||||
$num_zeros = $length - strlen($increment_id);
|
||||
if($num_zeros > 0) {
|
||||
return join("", [$prefix, str_repeat("0", $num_zeros), $increment_id]);
|
||||
}
|
||||
|
||||
return join("", [$prefix, $increment_id]);
|
||||
}
|
||||
|
||||
|
||||
private static $nanoClientCache;
|
||||
|
||||
|
||||
/**
|
||||
* @description create string id using Nano ID
|
||||
* Collision Calculator: https://zelark.github.io/nano-id-cc/
|
||||
* @param int $length
|
||||
* @param bool $lower_case
|
||||
* @return string
|
||||
*/
|
||||
public static function createStringId(int $length = 15, bool $lower_case = false) : string {
|
||||
|
||||
if($lower_case) {
|
||||
$alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';
|
||||
} else {
|
||||
$alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
||||
}
|
||||
|
||||
if(isset(static::$nanoClientCache)) {
|
||||
return static::$nanoClientCache->formattedId($alphabet, $length);
|
||||
}
|
||||
|
||||
// create one
|
||||
$client = new Client();
|
||||
static::$nanoClientCache = $client;
|
||||
|
||||
return $client->formattedId($alphabet, $length);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user