Skip to content

Commit

Permalink
feat: convert decodes / encodes to casts
Browse files Browse the repository at this point in the history
  • Loading branch information
ScuffedNewt committed Jan 12, 2025
1 parent dc12faa commit 8e996c1
Show file tree
Hide file tree
Showing 31 changed files with 205 additions and 250 deletions.
2 changes: 0 additions & 2 deletions app/Helpers/AssetHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ function removeAsset(&$array, $asset, $quantity = 1) {
/**
* Get a clean version of the asset array to store in the database,
* where each asset is listed in [id => quantity] format.
* json_encode this and store in the data attribute.
*
* @param array $array
* @param bool $isCharacter
Expand All @@ -250,7 +249,6 @@ function getDataReadyAssets($array, $isCharacter = false) {
/**
* Retrieves the data associated with an asset array,
* basically reversing the above function.
* Use the data attribute after json_decode()ing it.
*
* @param array $array
*
Expand Down
2 changes: 1 addition & 1 deletion app/Helpers/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function create($type, $user, $data) {
$notification = Notification::create([
'user_id' => $user->id,
'notification_type_id' => Notification::getNotificationId($type),
'data' => json_encode($data),
'data' => $data,
'is_unread' => 1,
]);

Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/Admin/Users/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function postUserBasicInfo(Request $request, $name) {

$logData = ['old_name' => $user->name] + $data;
if ($user->update($data)) {
UserUpdateLog::create(['staff_id' => Auth::user()->id, 'user_id' => $user->id, 'data' => json_encode($logData), 'type' => 'Name/Rank Change']);
UserUpdateLog::create(['staff_id' => Auth::user()->id, 'user_id' => $user->id, 'data' => $logData, 'type' => 'Name/Rank Change']);
flash('Updated user\'s information successfully.')->success();
} else {
flash('Failed to update user\'s information.')->error();
Expand Down Expand Up @@ -154,7 +154,7 @@ public function postUserAlias(Request $request, $name, $id) {
return redirect()->back();
}

UserUpdateLog::create(['staff_id' => Auth::user()->id, 'user_id' => $user->id, 'data' => json_encode($logData), 'type' => 'Clear Alias']);
UserUpdateLog::create(['staff_id' => Auth::user()->id, 'user_id' => $user->id, 'data' => $logData, 'type' => 'Clear Alias']);
flash('Cleared user\'s alias successfully.')->success();
} else {
flash('Failed to clear user\'s alias.')->error();
Expand All @@ -177,7 +177,7 @@ public function postUserAccount(Request $request, $name) {
return redirect()->back();
}

UserUpdateLog::create(['staff_id' => Auth::user()->id, 'user_id' => $user->id, 'data' => json_encode(['is_fto' => $request->get('is_fto') ? 'Yes' : 'No']), 'type' => 'FTO Status Change']);
UserUpdateLog::create(['staff_id' => Auth::user()->id, 'user_id' => $user->id, 'data' => ['is_fto' => $request->get('is_fto') ? 'Yes' : 'No'], 'type' => 'FTO Status Change']);
flash('Updated user\'s account information successfully.')->success();
} else {
flash('Failed to update user\'s account information.')->error();
Expand Down Expand Up @@ -205,7 +205,7 @@ public function postUserBirthday(Request $request, $name) {
}

if ($service->updateBirthday($formatDate, $user)) {
UserUpdateLog::create(['staff_id' => Auth::user()->id, 'user_id' => $user->id, 'data' => json_encode($logData), 'type' => 'Birth Date Change']);
UserUpdateLog::create(['staff_id' => Auth::user()->id, 'user_id' => $user->id, 'data' => $logData, 'type' => 'Birth Date Change']);
flash('Birthday updated successfully!')->success();
} else {
foreach ($service->errors()->getMessages()['error'] as $error) {
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Comments/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ public function update(Request $request, Comment $comment) {
$comment->edits()->create([
'user_id' => Auth::user()->id,
'comment_id' => $comment->id,
'data' => json_encode([
'data' => [
'action' => 'edit',
'old_comment' => config('lorekeeper.settings.wysiwyg_comments') ? parse($comment->comment) : $comment->comment,
'new_comment' => config('lorekeeper.settings.wysiwyg_comments') ? parse($request->message) : $request->message,
]),
],
]);

$comment->update([
Expand Down
12 changes: 2 additions & 10 deletions app/Models/Character/CharacterDesignUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class CharacterDesignUpdate extends Model {
protected $casts = [
'submitted_at' => 'datetime',
'subtype_ids' => 'array',
'data' => 'array',
];

/**
Expand Down Expand Up @@ -206,15 +207,6 @@ public function scopeSortNewest($query) {
**********************************************************************************************/

/**
* Get the data attribute as an associative array.
*
* @return array
*/
public function getDataAttribute() {
return json_decode($this->attributes['data'], true);
}

/**
* Get the items (UserItem IDs) attached to this update request.
*
Expand Down Expand Up @@ -332,7 +324,7 @@ public function getUrlAttribute() {
* @return string
*/
public function getVoteDataAttribute() {
return collect(json_decode($this->attributes['vote_data'], true));
return collect($this->attributes['vote_data'], true);
}

/**********************************************************************************************
Expand Down
18 changes: 9 additions & 9 deletions app/Models/Character/CharacterItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ class CharacterItem extends Model {
*/
protected $table = 'character_items';

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'data' => 'array',
];

/**
* Whether the model contains timestamps to be saved and updated.
*
Expand Down Expand Up @@ -58,15 +67,6 @@ public function item() {
**********************************************************************************************/

/**
* Get the data attribute as an associative array.
*
* @return array
*/
public function getDataAttribute() {
return json_decode($this->attributes['data'], true);
}

/**
* Checks if the stack is transferrable.
*
Expand Down
20 changes: 11 additions & 9 deletions app/Models/Character/CharacterLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ class CharacterLog extends Model {
* @var string
*/
protected $table = 'character_log';

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'data' => 'array',
'change_log' => 'array',
];

/**
* Whether the model contains timestamps to be saved and updated.
*
Expand Down Expand Up @@ -74,13 +85,4 @@ public function getDisplayRecipientAliasAttribute() {
return '---';
}
}

/**
* Retrieves the changed data as an associative array.
*
* @return array
*/
public function getChangedDataAttribute() {
return json_decode($this->change_log, true);
}
}
19 changes: 10 additions & 9 deletions app/Models/Character/CharacterTransfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ class CharacterTransfer extends Model {
* @var string
*/
protected $table = 'character_transfers';

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'data' => 'array',
];

/**
* Whether the model contains timestamps to be saved and updated.
*
Expand Down Expand Up @@ -140,13 +150,4 @@ public function getIsActiveAttribute() {

return false;
}

/**
* Get the data attribute as an associative array.
*
* @return array
*/
public function getDataAttribute() {
return json_decode($this->attributes['data'], true);
}
}
28 changes: 9 additions & 19 deletions app/Models/Comment/CommentEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ class CommentEdit extends Model {
*/
protected $table = 'comment_edits';

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'data' => 'array',
];

/**
* Whether the model contains timestamps to be saved and updated.
*
Expand All @@ -48,23 +57,4 @@ public function comment() {
public function user() {
return $this->belongsTo(User::class);
}

/**********************************************************************************************
ATTRIBUTES
**********************************************************************************************/

/**
* Get the data attribute as an associative array.
*
* @return array
*/
public function getDataAttribute() {
if (!$this->id) {
return null;
}

return json_decode($this->attributes['data'], true);
}
}
28 changes: 10 additions & 18 deletions app/Models/Gallery/GallerySubmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ class GallerySubmission extends Model {
*/
protected $table = 'gallery_submissions';

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'data' => 'array',
'vote_data' => 'array',
];

/**
* Whether the model contains timestamps to be saved and updated.
*
Expand Down Expand Up @@ -318,24 +328,6 @@ public function getThumbnailUrlAttribute() {
return asset($this->imageDirectory.'/'.$this->thumbnailFileName);
}

/**
* Get the data attribute as an associative array.
*
* @return array
*/
public function getDataAttribute() {
return json_decode($this->attributes['data'], true);
}

/**
* Gets the voting data of the gallery submission.
*
* @return string
*/
public function getVoteDataAttribute() {
return collect(json_decode($this->attributes['vote_data'], true));
}

/**
* Get the title of the submission, with prefix.
*
Expand Down
23 changes: 10 additions & 13 deletions app/Models/Item/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ class Item extends Model {
* @var string
*/
protected $table = 'items';

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'data' => 'array',
];

/**
* Validation rules for creation.
*
Expand Down Expand Up @@ -285,19 +295,6 @@ public function getReferenceAttribute() {
return $this->reference_url;
}

/**
* Get the data attribute as an associative array.
*
* @return array
*/
public function getDataAttribute() {
if (!$this->id) {
return null;
}

return json_decode($this->attributes['data'], true);
}

/**
* Get the rarity attribute.
*
Expand Down
18 changes: 9 additions & 9 deletions app/Models/Item/ItemTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ class ItemTag extends Model {
*/
protected $table = 'item_tags';

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'data' => 'array',
];

/**********************************************************************************************
RELATIONS
Expand Down Expand Up @@ -102,15 +111,6 @@ public function getAdminUrlAttribute() {
return url('admin/data/items/tag/'.$this->item_id.'/'.$this->tag);
}

/**
* Get the data attribute as an associative array.
*
* @return array
*/
public function getDataAttribute() {
return json_decode($this->attributes['data'], true);
}

/**
* Get the service associated with this tag.
*
Expand Down
29 changes: 10 additions & 19 deletions app/Models/Loot/Loot.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ class Loot extends Model {
* @var string
*/
protected $table = 'loots';

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'data' => 'array',
];

/**
* Validation rules for creation.
*
Expand Down Expand Up @@ -78,23 +88,4 @@ public function reward() {

return null;
}

/**********************************************************************************************
ACCESSORS
**********************************************************************************************/

/**
* Get the data attribute as an associative array.
*
* @return array
*/
public function getDataAttribute() {
if (!$this->attributes['data']) {
return null;
}

return json_decode($this->attributes['data'], true);
}
}
Loading

0 comments on commit 8e996c1

Please sign in to comment.