2020-11-02 16:18:06 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-01-07 19:00:30 +01:00
|
|
|
use Kwaadpepper\Serial\SerialCaster;
|
2020-11-02 16:18:06 +01:00
|
|
|
|
|
|
|
|
class Codes extends Model
|
|
|
|
|
{
|
2022-01-07 19:00:30 +01:00
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'code_used', 'code_exported',
|
|
|
|
|
'date_creation'
|
|
|
|
|
];
|
2021-08-18 17:08:02 +02:00
|
|
|
|
2022-01-07 19:00:30 +01:00
|
|
|
/**
|
|
|
|
|
* Indicates if the model should be timestamped.
|
|
|
|
|
*
|
|
|
|
|
* @var boolean
|
|
|
|
|
*/
|
2020-11-02 16:18:06 +01:00
|
|
|
public $timestamps = false;
|
2020-11-04 11:49:43 +01:00
|
|
|
|
2022-01-07 19:00:30 +01:00
|
|
|
// * RELATIONS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Has many Participation relation
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
*/
|
2021-08-18 17:08:02 +02:00
|
|
|
public function participations()
|
|
|
|
|
{
|
2022-01-07 19:00:30 +01:00
|
|
|
return $this->hasMany(Participation::class, 'code_unique_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// * FUNCTIONS
|
|
|
|
|
/**
|
|
|
|
|
* Generate code_unique attribute
|
|
|
|
|
*
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
public function getCodeUniqueAttribute()
|
|
|
|
|
{
|
|
|
|
|
if (!$this->id or !\is_integer($this->id) or $this->id < 1) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return self::intToSerial($this->id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate a serial from an int.
|
|
|
|
|
*
|
|
|
|
|
* @param integer $int
|
|
|
|
|
* @return string The serial
|
|
|
|
|
* @throws SerialCasterException If an error happens du to config.
|
|
|
|
|
*/
|
|
|
|
|
public static function intToSerial(int $int): string
|
|
|
|
|
{
|
|
|
|
|
return SerialCaster::encode(
|
|
|
|
|
$int,
|
|
|
|
|
\abs(\crc32(config('app.key'))),
|
|
|
|
|
app(Settings::class)->code_length,
|
|
|
|
|
config('codes.codeCharList')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate a serial from an int.
|
|
|
|
|
*
|
|
|
|
|
* @param string $serial
|
|
|
|
|
* @return integer The ID represented
|
|
|
|
|
* @throws SerialCasterException If an error happens due to config or wrong serial is given.
|
|
|
|
|
*/
|
|
|
|
|
public static function serialToInt(string $serial): int
|
|
|
|
|
{
|
|
|
|
|
return SerialCaster::decode(
|
|
|
|
|
$serial,
|
|
|
|
|
\abs(\crc32(config('app.key'))),
|
|
|
|
|
config('codes.codeCharList')
|
|
|
|
|
);
|
2020-11-04 11:49:43 +01:00
|
|
|
}
|
2020-11-02 16:18:06 +01:00
|
|
|
}
|