From 0dacfc31ace1b624e7003679cd3b590f639649db Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Sun, 17 Mar 2024 14:15:06 -0400 Subject: [PATCH] Use built in casts --- app/Models/AuditLog.php | 3 +-- app/Models/Backup.php | 7 ++++--- app/Models/DatabaseHost.php | 4 ++-- app/Models/EggVariable.php | 4 ++-- app/Models/Model.php | 19 ------------------- app/Models/RecoveryToken.php | 6 ++++-- app/Models/ServerVariable.php | 4 ++-- 7 files changed, 15 insertions(+), 32 deletions(-) diff --git a/app/Models/AuditLog.php b/app/Models/AuditLog.php index 4b0d092b3..127838712 100644 --- a/app/Models/AuditLog.php +++ b/app/Models/AuditLog.php @@ -26,12 +26,11 @@ class AuditLog extends Model protected $table = 'audit_logs'; - protected bool $immutableDates = true; - protected $casts = [ 'is_system' => 'bool', 'device' => 'array', 'metadata' => 'array', + 'created_at' => 'immutable_datetime', ]; protected $guarded = [ diff --git a/app/Models/Backup.php b/app/Models/Backup.php index 35780dba9..88ba501a0 100644 --- a/app/Models/Backup.php +++ b/app/Models/Backup.php @@ -36,15 +36,16 @@ class Backup extends Model protected $table = 'backups'; - protected bool $immutableDates = true; - protected $casts = [ 'id' => 'int', 'is_successful' => 'bool', 'is_locked' => 'bool', 'ignored_files' => 'array', 'bytes' => 'int', - 'completed_at' => 'datetime', + 'completed_at' => 'immutable_datetime', + 'created_at' => 'immutable_datetime', + 'updated_at' => 'immutable_datetime', + 'deleted_at' => 'immutable_datetime', ]; protected $attributes = [ diff --git a/app/Models/DatabaseHost.php b/app/Models/DatabaseHost.php index 3a72fff1a..da34dcf29 100644 --- a/app/Models/DatabaseHost.php +++ b/app/Models/DatabaseHost.php @@ -25,8 +25,6 @@ class DatabaseHost extends Model */ public const RESOURCE_NAME = 'database_host'; - protected bool $immutableDates = true; - /** * The table associated with the model. */ @@ -51,6 +49,8 @@ class DatabaseHost extends Model 'id' => 'integer', 'max_databases' => 'integer', 'node_id' => 'integer', + 'created_at' => 'immutable_datetime', + 'updated_at' => 'immutable_datetime', ]; /** diff --git a/app/Models/EggVariable.php b/app/Models/EggVariable.php index b3a442ced..6de71c83b 100644 --- a/app/Models/EggVariable.php +++ b/app/Models/EggVariable.php @@ -38,8 +38,6 @@ class EggVariable extends Model */ public const RESERVED_ENV_NAMES = 'SERVER_MEMORY,SERVER_IP,SERVER_PORT,ENV,HOME,USER,STARTUP,SERVER_UUID,UUID'; - protected bool $immutableDates = true; - /** * The table associated with the model. */ @@ -57,6 +55,8 @@ class EggVariable extends Model 'egg_id' => 'integer', 'user_viewable' => 'bool', 'user_editable' => 'bool', + 'created_at' => 'immutable_datetime', + 'updated_at' => 'immutable_datetime', ]; public static array $validationRules = [ diff --git a/app/Models/Model.php b/app/Models/Model.php index ea20103ab..c3ed88a36 100644 --- a/app/Models/Model.php +++ b/app/Models/Model.php @@ -19,11 +19,6 @@ abstract class Model extends IlluminateModel { use HasFactory; - /** - * Set to true to return immutable Carbon date instances from the model. - */ - protected bool $immutableDates = false; - /** * Determines if the model should undergo data validation before it is saved * to the database. @@ -160,18 +155,4 @@ abstract class Model extends IlluminateModel throw new ValidationException($validator); } } - - /** - * Return a timestamp as DateTime object. - * - * @param mixed $value - */ - protected function asDateTime($value): Carbon|CarbonImmutable - { - if (!$this->immutableDates) { - return parent::asDateTime($value); - } - - return parent::asDateTime($value)->toImmutable(); - } } diff --git a/app/Models/RecoveryToken.php b/app/Models/RecoveryToken.php index c879bac09..c4fe8c8ae 100644 --- a/app/Models/RecoveryToken.php +++ b/app/Models/RecoveryToken.php @@ -20,12 +20,14 @@ class RecoveryToken extends Model public $timestamps = true; - protected bool $immutableDates = true; - public static array $validationRules = [ 'token' => 'required|string', ]; + protected $casts = [ + 'created_at' => 'immutable_datetime', + ]; + public function user(): BelongsTo { return $this->belongsTo(User::class); diff --git a/app/Models/ServerVariable.php b/app/Models/ServerVariable.php index 3cb363468..2d3023824 100644 --- a/app/Models/ServerVariable.php +++ b/app/Models/ServerVariable.php @@ -22,8 +22,6 @@ class ServerVariable extends Model */ public const RESOURCE_NAME = 'server_variable'; - protected bool $immutableDates = true; - protected $table = 'server_variables'; protected $guarded = ['id', 'created_at', 'updated_at']; @@ -31,6 +29,8 @@ class ServerVariable extends Model protected $casts = [ 'server_id' => 'integer', 'variable_id' => 'integer', + 'created_at' => 'immutable_datetime', + 'updated_at' => 'immutable_datetime', ]; public static array $validationRules = [