|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pharaonic\Laravel\ShortURL; |
| 4 | + |
| 5 | +use Carbon\Carbon; |
| 6 | +use Exception; |
| 7 | +use Illuminate\Database\Eloquent\Factories\HasFactory; |
| 8 | +use Illuminate\Database\Eloquent\Model; |
| 9 | +use Illuminate\Support\Facades\Route; |
| 10 | + |
| 11 | +/** |
| 12 | + * Short URL Model |
| 13 | + * |
| 14 | + * @version 1.0 |
| 15 | + * @author Raggi <support@pharaonic.io> |
| 16 | + * @license http://opensource.org/licenses/mit-license.php MIT License |
| 17 | + */ |
| 18 | +class ShortURL extends Model |
| 19 | +{ |
| 20 | + use HasFactory; |
| 21 | + |
| 22 | + /** |
| 23 | + * The table associated with the model. |
| 24 | + * |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + protected $table = 'short_urls'; |
| 28 | + |
| 29 | + /** |
| 30 | + * The primary key for the model. |
| 31 | + * |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + protected $primaryKey = 'code'; |
| 35 | + |
| 36 | + /** |
| 37 | + * The "type" of the primary key Code. |
| 38 | + * |
| 39 | + * @var string |
| 40 | + */ |
| 41 | + protected $keyType = 'string'; |
| 42 | + |
| 43 | + /** |
| 44 | + * Indicates if the IDs are auto-incrementing. |
| 45 | + * |
| 46 | + * @var bool |
| 47 | + */ |
| 48 | + public $incrementing = false; |
| 49 | + |
| 50 | + /** |
| 51 | + * Table Columns |
| 52 | + * |
| 53 | + * @var array |
| 54 | + */ |
| 55 | + protected $fillable = ['code', 'type', 'data', 'user_id', 'expire_at']; |
| 56 | + |
| 57 | + /** |
| 58 | + * Dates |
| 59 | + * |
| 60 | + * @var array |
| 61 | + */ |
| 62 | + protected $dates = ['expire_at']; |
| 63 | + |
| 64 | + /** |
| 65 | + * Casts Columns |
| 66 | + * |
| 67 | + * @var array |
| 68 | + */ |
| 69 | + protected $casts = ['data' => 'array']; |
| 70 | + |
| 71 | + |
| 72 | + /////////////////////////////////////////////////////////////// |
| 73 | + // |
| 74 | + // PRIVATE ACTIONS |
| 75 | + // |
| 76 | + /////////////////////////////////////////////////////////////// |
| 77 | + |
| 78 | + /** |
| 79 | + * Generate Code |
| 80 | + * |
| 81 | + * @return string |
| 82 | + */ |
| 83 | + private function generateCode() |
| 84 | + { |
| 85 | + // Generate Code |
| 86 | + $code = random_bytes(config('Pharaonic.short-url.length', 10) / 2); |
| 87 | + $code = bin2hex($code); |
| 88 | + |
| 89 | + // Check Unique Code |
| 90 | + if (!$this->isUniqueCode($code)) return $this->generateCode(); |
| 91 | + |
| 92 | + return $code; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Check Code is Unique |
| 97 | + * |
| 98 | + * @return bool |
| 99 | + */ |
| 100 | + private function isUniqueCode(string $code) |
| 101 | + { |
| 102 | + return !self::where('code', $code)->exists(); |
| 103 | + } |
| 104 | + |
| 105 | + /////////////////////////////////////////////////////////////// |
| 106 | + // |
| 107 | + // Custom Attributes |
| 108 | + // |
| 109 | + /////////////////////////////////////////////////////////////// |
| 110 | + |
| 111 | + /** |
| 112 | + * Has been Expired |
| 113 | + * |
| 114 | + * @return bool |
| 115 | + */ |
| 116 | + public function getExpiredAttribute() |
| 117 | + { |
| 118 | + if (!$this->code || !$this->expire_at) return false; |
| 119 | + |
| 120 | + return $this->expire_at <= Carbon::now(); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Undocumented function |
| 125 | + * |
| 126 | + * @return void |
| 127 | + */ |
| 128 | + public function getURLAttribute() |
| 129 | + { |
| 130 | + if (!$this->code) return null; |
| 131 | + |
| 132 | + return route('shortURL', $this->code); |
| 133 | + } |
| 134 | + |
| 135 | + /////////////////////////////////////////////////////////////// |
| 136 | + // |
| 137 | + // PUBLIC ACTIONS |
| 138 | + // |
| 139 | + /////////////////////////////////////////////////////////////// |
| 140 | + |
| 141 | + /** |
| 142 | + * Get URL |
| 143 | + * |
| 144 | + * @return string |
| 145 | + */ |
| 146 | + public function __toString() |
| 147 | + { |
| 148 | + if (!$this->code) return null; |
| 149 | + |
| 150 | + return $this->url ?? null; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Generate Short URL |
| 155 | + * |
| 156 | + * @param string $route route or url |
| 157 | + * @param array|string|null $params route params or expire date |
| 158 | + * @param string|Carbon $expire expire date |
| 159 | + * @return ShortURL |
| 160 | + */ |
| 161 | + public function generate(string $route, $params = null, $expire = null) |
| 162 | + { |
| 163 | + $isURL = filter_var($route, FILTER_VALIDATE_URL) !== false; |
| 164 | + |
| 165 | + // Prepare Args |
| 166 | + if ($isURL && $params) $expire = $params; |
| 167 | + if (!$isURL && !Route::has($route)) throw new Exception("Route name has not been found!"); |
| 168 | + if ($expire && !($expire instanceof Carbon)) $expire = Carbon::parse($expire); |
| 169 | + |
| 170 | + |
| 171 | + // Create ShortURL |
| 172 | + return self::create([ |
| 173 | + 'code' => $this->generateCode(), |
| 174 | + 'type' => $isURL ? 'url' : 'route', |
| 175 | + 'data' => $isURL ? ['url' => $route] : ['route' => $route] + ($params ? ['params' => $params] : []), |
| 176 | + 'expire_at' => $expire, |
| 177 | + 'user_id' => auth()->user() ? auth()->user()->id : null |
| 178 | + ]); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Re-Generate Code |
| 183 | + * |
| 184 | + * @return ShortURL|null |
| 185 | + */ |
| 186 | + public function regenerate() |
| 187 | + { |
| 188 | + if (!$this->code) return null; |
| 189 | + |
| 190 | + $this->code = $this->generateCode(); |
| 191 | + $this->save(); |
| 192 | + |
| 193 | + return $this; |
| 194 | + } |
| 195 | +} |
0 commit comments