Changeset View
Changeset View
Standalone View
Standalone View
src/app/SignupCode.php
Show All 9 Lines | |||||
* | * | ||||
* @property string $code | * @property string $code | ||||
* @property array $data | * @property array $data | ||||
* @property \Carbon\Carbon $expires_at | * @property \Carbon\Carbon $expires_at | ||||
* @property string $short_code | * @property string $short_code | ||||
*/ | */ | ||||
class SignupCode extends Model | class SignupCode extends Model | ||||
{ | { | ||||
// Note: Removed '0', 'O', '1', 'I' as problematic with some fonts | |||||
public const SHORTCODE_CHARS = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ'; | |||||
public const SHORTCODE_LENGTH = 5; | public const SHORTCODE_LENGTH = 5; | ||||
public const CODE_LENGTH = 32; | public const CODE_LENGTH = 32; | ||||
// Code expires after so many hours | // Code expires after so many hours | ||||
public const CODE_EXP_HOURS = 24; | public const CODE_EXP_HOURS = 24; | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | class SignupCode extends Model | ||||
/** | /** | ||||
* Generate a short code (for human). | * Generate a short code (for human). | ||||
* | * | ||||
* @return string | * @return string | ||||
*/ | */ | ||||
public static function generateShortCode(): string | public static function generateShortCode(): string | ||||
{ | { | ||||
$code_length = env('SIGNUP_CODE_LENGTH', self::SHORTCODE_LENGTH); | $code_length = env('SIGNUP_CODE_LENGTH', self::SHORTCODE_LENGTH); | ||||
$code_chars = env('SIGNUP_CODE_CHARS', self::SHORTCODE_CHARS); | |||||
$random = []; | |||||
for ($i = 1; $i <= $code_length; $i++) { | return \App\Utils::randStr($code_length); | ||||
$random[] = $code_chars[rand(0, strlen($code_chars) - 1)]; | |||||
} | |||||
shuffle($random); | |||||
return implode('', $random); | |||||
} | } | ||||
} | } |