Page MenuHomePhorge

SignupCode.php
No OneTemporary

Authored By
Unknown
Size
2 KB
Referenced Files
None
Subscribers
None

SignupCode.php

<?php
namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
/**
* The eloquent definition of a SignupCode.
*
* @property string $code
* @property array $data
* @property \Carbon\Carbon $expires_at
* @property string $short_code
*/
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 CODE_LENGTH = 32;
// Code expires after so many hours
public const CODE_EXP_HOURS = 24;
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'code';
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
/**
* The "type" of the auto-incrementing ID.
*
* @var string
*/
protected $keyType = 'string';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['code', 'short_code', 'data', 'expires_at'];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = ['data' => 'array'];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['expires_at'];
/**
* Check if code is expired.
*
* @return bool True if code is expired, False otherwise
*/
public function isExpired()
{
// @phpstan-ignore-next-line
return $this->expires_at ? Carbon::now()->gte($this->expires_at) : false;
}
/**
* Generate a short code (for human).
*
* @return string
*/
public static function generateShortCode(): string
{
$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++) {
$random[] = $code_chars[rand(0, strlen($code_chars) - 1)];
}
shuffle($random);
return implode('', $random);
}
}

File Metadata

Mime Type
text/x-php
Expires
Sun, Apr 5, 11:35 PM (1 w, 5 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
c6/db/003d0db8f1233c22e5aee3ef5bde
Default Alt Text
SignupCode.php (2 KB)

Event Timeline