Skip to content

Commit

Permalink
Hash is generated
Browse files Browse the repository at this point in the history
  • Loading branch information
aaqibmehran committed Aug 10, 2021
1 parent 5962473 commit db1adc8
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions src/UniqueStringGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ private function getFieldType($table, $field)
return ['type' => $fieldType, 'length' => $fieldLength];
}

private function generateCode($length){
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}

return $randomString;
}

public static function generate($configArr)
{
if (!array_key_exists('table', $configArr) || $configArr['table'] == '') {
Expand Down Expand Up @@ -83,36 +94,24 @@ public static function generate($configArr)
}

$prefixLength = strlen($configArr['prefix']);

$idLength = $length - $prefixLength;
$whereString = '';

if (array_key_exists('where', $configArr)) {
$whereString .= " WHERE ";
foreach ($configArr['where'] as $row) {
$whereString .= $row[0] . "=" . $row[1] . " AND ";
try {
$all_models = DB::table($table)->select($field)->get();
$generated_code = (new self)->generateCode($idLength);
while ($all_models->contains($generated_code)) {
$generated_code = (new self)->generateCode($idLength);
}
}
$whereString = rtrim($whereString, 'AND ');


$totalQuery = sprintf("SELECT count(%s) total FROM %s %s", $field, $configArr['table'], $whereString);
$total = DB::select($totalQuery);
if ($generated_code) {
return $prefix.$generated_code;

if ($total[0]->total) {
if ($resetOnPrefixChange) {
$maxQuery = sprintf("SELECT MAX(%s) AS maxid FROM %s WHERE %s LIKE %s", $field, $table, $field, "'" . $prefix . "%'");
} else {
$maxQuery = sprintf("SELECT MAX(%s) AS maxid FROM %s", $field, $table);
return $prefix.$generated_code;
}

$queryResult = DB::select($maxQuery);
$maxFullId = $queryResult[0]->maxid;

$maxId = substr($maxFullId, $prefixLength, $idLength);
return $prefix . str_pad((int)$maxId + 1, $idLength, '0', STR_PAD_LEFT);

} else {
return $prefix . str_pad(1, $idLength, '0', STR_PAD_LEFT);
} catch (Exception $e){
throw new Exception($e->getMessage());
}
}
}

0 comments on commit db1adc8

Please sign in to comment.