From a95fc0fbd1f18394a43681e6aecea168f1482420 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sat, 4 Dec 2021 10:59:03 +0100 Subject: [PATCH] Models: Use explicit casts and default attribute values --- src/Models/News.php | 7 +++++++ src/Models/NewsComment.php | 6 ++++++ src/Models/OAuth.php | 5 +++++ src/Models/Shifts/Schedule.php | 7 +++++++ src/Models/Shifts/ScheduleShift.php | 6 ++++++ src/Models/User/HasUserModel.php | 5 +++++ src/Models/User/State.php | 16 +++++++++++++--- src/Models/User/UsesUserModel.php | 1 + src/Models/Worklog.php | 1 + 9 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/Models/News.php b/src/Models/News.php index 2ccb4114..131edd08 100644 --- a/src/Models/News.php +++ b/src/Models/News.php @@ -42,10 +42,17 @@ class News extends BaseModel /** @var array */ protected $casts = [ + 'user_id' => 'integer', 'is_meeting' => 'boolean', 'is_pinned' => 'boolean', ]; + /** @var array Default attributes */ + protected $attributes = [ + 'is_meeting' => false, + 'is_pinned' => false, + ]; + /** @var array */ protected $fillable = [ 'title', diff --git a/src/Models/NewsComment.php b/src/Models/NewsComment.php index 5adc5498..e0ccedb4 100644 --- a/src/Models/NewsComment.php +++ b/src/Models/NewsComment.php @@ -34,6 +34,12 @@ class NewsComment extends BaseModel /** @var bool Enable timestamps */ public $timestamps = true; + /** @var string[] */ + protected $casts = [ + 'user_id' => 'integer', + 'news_id' => 'integer', + ]; + /** @var string[] */ protected $fillable = [ 'news_id', diff --git a/src/Models/OAuth.php b/src/Models/OAuth.php index ad1e9fc8..f0d1fb37 100644 --- a/src/Models/OAuth.php +++ b/src/Models/OAuth.php @@ -34,6 +34,11 @@ class OAuth extends BaseModel /** @var bool Enable timestamps */ public $timestamps = true; + /** @var string[] */ + protected $casts = [ + 'user_id' => 'integer', + ]; + /** @var string[] */ protected $dates = [ 'expires_at', diff --git a/src/Models/Shifts/Schedule.php b/src/Models/Shifts/Schedule.php index 9c718a25..9e478c02 100644 --- a/src/Models/Shifts/Schedule.php +++ b/src/Models/Shifts/Schedule.php @@ -37,6 +37,13 @@ class Schedule extends BaseModel /** @var bool enable timestamps */ public $timestamps = true; + /** @var string[] */ + protected $casts = [ + 'shift_type' => 'integer', + 'minutes_before' => 'integer', + 'minutes_after' => 'integer', + ]; + /** @var array Values that are mass assignable */ protected $fillable = [ 'name', diff --git a/src/Models/Shifts/ScheduleShift.php b/src/Models/Shifts/ScheduleShift.php index 368c60b1..8bfd726b 100644 --- a/src/Models/Shifts/ScheduleShift.php +++ b/src/Models/Shifts/ScheduleShift.php @@ -28,6 +28,12 @@ class ScheduleShift extends BaseModel /** @var array Values that are mass assignable */ protected $fillable = ['shift_id', 'schedule_id', 'guid']; + /** @var array */ + protected $casts = [ + 'shift_id' => 'integer', + 'schedule_id' => 'integer', + ]; + /** * @return BelongsTo */ diff --git a/src/Models/User/HasUserModel.php b/src/Models/User/HasUserModel.php index 2b7fdda9..47fbd14d 100644 --- a/src/Models/User/HasUserModel.php +++ b/src/Models/User/HasUserModel.php @@ -16,6 +16,11 @@ abstract class HasUserModel extends BaseModel 'user_id', ]; + /** @var string[] */ + protected $casts = [ + 'user_id' => 'integer', + ]; + /** The relationships that should be touched on save */ protected $touches = ['user']; } diff --git a/src/Models/User/State.php b/src/Models/User/State.php index 10576e27..d6ffbfc1 100644 --- a/src/Models/User/State.php +++ b/src/Models/User/State.php @@ -28,13 +28,18 @@ class State extends HasUserModel /** @var string The table associated with the model */ protected $table = 'users_state'; - /** @var array The attributes that should be mutated to dates */ - protected $dates = [ - 'arrival_date', + /** @var array Default attributes */ + protected $attributes = [ + 'arrived' => false, + 'active' => false, + 'force_active' => false, + 'got_shirt' => false, + 'got_voucher' => 0, ]; /** @var array */ protected $casts = [ + 'user_id' => 'integer', 'arrived' => 'boolean', 'active' => 'boolean', 'force_active' => 'boolean', @@ -42,6 +47,11 @@ class State extends HasUserModel 'got_voucher' => 'integer', ]; + /** @var array The attributes that should be mutated to dates */ + protected $dates = [ + 'arrival_date', + ]; + /** The attributes that are mass assignable */ protected $fillable = [ 'user_id', diff --git a/src/Models/User/UsesUserModel.php b/src/Models/User/UsesUserModel.php index 5d07589a..f0309816 100644 --- a/src/Models/User/UsesUserModel.php +++ b/src/Models/User/UsesUserModel.php @@ -15,6 +15,7 @@ use Illuminate\Database\Query\Builder as QueryBuilder; trait UsesUserModel { // protected $fillable = ['user_id']; + // protected $casts = ['user_id' => 'integer]; /** * @return BelongsTo diff --git a/src/Models/Worklog.php b/src/Models/Worklog.php index 290b3da3..3d9cf12a 100644 --- a/src/Models/Worklog.php +++ b/src/Models/Worklog.php @@ -43,6 +43,7 @@ class Worklog extends BaseModel protected $casts = [ 'user_id' => 'integer', 'creator_id' => 'integer', + 'hours' => 'float', ]; /** The attributes that are mass assignable. */