Skip to content

Commit

Permalink
Merge pull request #425 from range-of-motion/424-use-upgraded-definit…
Browse files Browse the repository at this point in the history
…ion-of-accessors-in-models

Use upgraded definition of accessors in models
  • Loading branch information
range-of-motion authored Oct 16, 2023
2 parents ea74685 + c38e075 commit 90057dc
Show file tree
Hide file tree
Showing 16 changed files with 265 additions and 75 deletions.
25 changes: 15 additions & 10 deletions app/Models/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use Exception;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
Expand Down Expand Up @@ -32,23 +33,27 @@ public function transaction()
}

// Accessors
public function getFileB64Attribute()
protected function fileB64(): Attribute
{
$file = Storage::get($this->file_path);
return Attribute::make(function () {
$file = Storage::get($this->file_path);

if (!$file) {
return null;
}
if (!$file) {
return null;
}

$type = pathinfo($this->file_path, PATHINFO_EXTENSION);
$type = pathinfo($this->file_path, PATHINFO_EXTENSION);

return 'data:image/' . $type . ';base64,' . base64_encode($file);
return 'data:image/' . $type . ';base64,' . base64_encode($file);
});
}

public function getFileTypeAttribute()
protected function fileType(): Attribute
{
$parts = explode('.', $this->file_path);
return Attribute::make(function () {
$parts = explode('.', $this->file_path);

return $parts[count($parts) - 1];
return $parts[count($parts) - 1];
});
}
}
13 changes: 7 additions & 6 deletions app/Models/Budget.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Helper;
use App\Repositories\BudgetRepository;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand Down Expand Up @@ -32,18 +33,18 @@ public function tag()
}

// Accessors
public function getFormattedAmountAttribute()
protected function formattedAmount(): Attribute
{
return Helper::formatNumber($this->amount / 100);
return Attribute::make(fn () => Helper::formatNumber($this->amount / 100));
}

public function getSpentAttribute()
protected function spent(): Attribute
{
return (new BudgetRepository())->getSpentById($this->id);
return Attribute::make(fn () => (new BudgetRepository())->getSpentById($this->id));
}

public function getFormattedSpentAttribute()
protected function formattedSpent(): Attribute
{
return Helper::formatNumber($this->spent / 100);
return Attribute::make(fn () => Helper::formatNumber($this->spent / 100));
}
}
2 changes: 1 addition & 1 deletion app/Models/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Currency extends Model
// Accessors
protected function isoLowercased(): Attribute
{
return Attribute::make(fn (mixed $value, array $attributes) => strtolower($attributes['iso']));
return Attribute::make(fn () => strtolower($this->iso));
}

// Scopes
Expand Down
12 changes: 6 additions & 6 deletions app/Models/Earning.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use App\Events\TransactionCreated;
use App\Events\TransactionDeleted;
use App\Helper;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand All @@ -28,16 +30,14 @@ class Earning extends Model
];

// Accessors
public function getFormattedAmountAttribute()
protected function formattedAmount(): Attribute
{
return Helper::formatNumber($this->amount / 100);
return Attribute::make(fn () => Helper::formatNumber($this->amount / 100));
}

public function getFormattedHappenedOnAttribute()
protected function formattedHappenedOn(): Attribute
{
$secondsDifference = strtotime(date('Y-m-d')) - strtotime($this->happened_on);

return ($secondsDifference / 60 / 60 / 24) . ' days ago';
return Attribute::make(fn () => Carbon::now()->diffInDays(Carbon::parse($this->happened_on)) . ' days ago');
}

// Relations
Expand Down
25 changes: 15 additions & 10 deletions app/Models/Recurring.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Events\RecurringCreated;
use App\Events\RecurringDeleted;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand Down Expand Up @@ -33,22 +34,26 @@ class Recurring extends Model
];

// Accessors
public function getDueDaysAttribute()
protected function dueDays(): Attribute
{
if ($this->starts_on <= date('Y-m-d') && ($this->ends_on >= date('Y-m-d') || !$this->ends_on)) {
if (date('j') > $this->day) {
return date('t') - date('j') + $this->day;
}
return Attribute::make(function () {
if ($this->starts_on <= date('Y-m-d') && ($this->ends_on >= date('Y-m-d') || !$this->ends_on)) {
if (date('j') > $this->day) {
return date('t') - date('j') + $this->day;
}

return $this->day - date('j');
}
return $this->day - date('j');
}

return 0;
return 0;
});
}

public function getStatusAttribute()
protected function status(): Attribute
{
return $this->starts_on <= date('Y-m-d') && ($this->ends_on >= date('Y-m-d') || !$this->ends_on);
return Attribute::make(function () {
return $this->starts_on <= date('Y-m-d') && ($this->ends_on >= date('Y-m-d') || !$this->ends_on);
});
}

// Relations
Expand Down
5 changes: 3 additions & 2 deletions app/Models/Space.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand Down Expand Up @@ -65,9 +66,9 @@ public function activities()
}

// Accessors
public function getAbbreviatedNameAttribute(): string
protected function abbreviatedName(): Attribute
{
return Str::limit($this->name, 3);
return Attribute::make(fn () => Str::limit($this->name, 3));
}

//
Expand Down
25 changes: 14 additions & 11 deletions app/Models/SpaceInvite.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

Expand Down Expand Up @@ -38,20 +39,22 @@ public function inviter()
}

// Accessors
public function getStatusAttribute(): string
protected function status(): Attribute
{
if ($this->accepted === null) {
return 'Pending';
}
return Attribute::make(function () {
if ($this->accepted === null) {
return 'Pending';
}

if ($this->accepted === true) {
return 'Accepted (' . date('d-m', strtotime($this->updated_at)) . ')';
}
if ($this->accepted === true) {
return 'Accepted (' . date('d-m', strtotime($this->updated_at)) . ')';
}

if ($this->accepted === false) {
return 'Denied (' . date('d-m', strtotime($this->updated_at)) . ')';
}
if ($this->accepted === false) {
return 'Denied (' . date('d-m', strtotime($this->updated_at)) . ')';
}

return 'Unknown';
return 'Unknown';
});
}
}
12 changes: 6 additions & 6 deletions app/Models/Spending.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use App\Events\TransactionCreated;
use App\Events\TransactionDeleted;
use App\Helper;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand All @@ -30,16 +32,14 @@ class Spending extends Model
];

// Accessors
public function getFormattedAmountAttribute()
protected function formattedAmount(): Attribute
{
return Helper::formatNumber($this->amount / 100);
return Attribute::make(fn () => Helper::formatNumber($this->amount / 100));
}

public function getFormattedHappenedOnAttribute()
protected function formattedHappenedOn(): Attribute
{
$secondsDifference = strtotime(date('Y-m-d')) - strtotime($this->happened_on);

return ($secondsDifference / 60 / 60 / 24) . ' days ago';
return Attribute::make(fn () => Carbon::now()->diffInDays(Carbon::parse($this->happened_on)) . ' days ago');
}

// Relations
Expand Down
7 changes: 5 additions & 2 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
Expand Down Expand Up @@ -49,9 +50,11 @@ public static function getValidationRulesForPasswordReset(): array
}

// Accessors
public function getAvatarAttribute($avatar)
protected function avatar(): Attribute
{
return $avatar ? '/storage/avatars/' . $avatar : 'https://via.placeholder.com/250';
return Attribute::make(
fn (?string $value) => $value ? '/storage/avatars/' . $value : 'https://via.placeholder.com/250'
);
}

// Relations
Expand Down
17 changes: 6 additions & 11 deletions app/Models/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Exceptions\WidgetInvalidPropertyValueException;
use App\Exceptions\WidgetMissingPropertyException;
use App\Exceptions\WidgetUnknownTypeException;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand All @@ -30,23 +31,17 @@ public function user()
* Accessors
*/

public function getPropertiesAttribute($value)
{
return json_decode($value);
}

/**
* Mutators
*/

public function setPropertiesAttribute($value)
protected function properties(): Attribute
{
/**
* Using accessor and mutator for this attribute because the "object" cast
* doesn't use JSON_FORCE_OBJECT
*/

$this->attributes['properties'] = json_encode($value, JSON_FORCE_OBJECT);
return Attribute::make(
get: fn ($value) => json_decode($value),
set: fn ($value) => json_encode($value, JSON_FORCE_OBJECT),
);
}

/**
Expand Down
82 changes: 82 additions & 0 deletions tests/Unit/Models/BudgetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Tests\Unit\Models;

use App\Models\Budget;
use App\Models\Space;
use App\Models\Spending;
use App\Models\Tag;
use Tests\TestCase;

class BudgetTest extends TestCase
{
public function testFormattedAmountAttribute(): void
{
$budget = Budget::factory()
->create(['amount' => 750]);

$this->assertEquals('7.50', $budget->formatted_amount);
}

public function testSpentAttribute(): void
{
$space = Space::factory()
->create();

$tag = Tag::factory()
->create([
'space_id' => $space->id,
]);

$budget = Budget::factory()
->create([
'tag_id' => $tag->id,
'period' => 'monthly',
'amount' => 750,
]);

Spending::factory()
->create([
'space_id' => $space->id,
'tag_id' => $tag->id,
'happened_on' => date('Y-m-d'),
'amount' => 100,
]);

// Set session accordingly
$this->withSession(['space_id' => $space->id]);

$this->assertEquals(100, $budget->spent);
}

public function testFormattedSpentAttribute(): void
{
$space = Space::factory()
->create();

$tag = Tag::factory()
->create([
'space_id' => $space->id,
]);

$budget = Budget::factory()
->create([
'tag_id' => $tag->id,
'period' => 'monthly',
'amount' => 750,
]);

Spending::factory()
->create([
'space_id' => $space->id,
'tag_id' => $tag->id,
'happened_on' => date('Y-m-d'),
'amount' => 100,
]);

// Set session accordingly
$this->withSession(['space_id' => $space->id]);

$this->assertEquals('1.00', $budget->formatted_spent);
}
}
Loading

0 comments on commit 90057dc

Please sign in to comment.