Add attributes

This commit is contained in:
James Cole
2023-01-02 06:44:52 +01:00
parent 3c1f44f554
commit 0429f50e5c
3 changed files with 67 additions and 13 deletions

View File

@@ -1,4 +1,5 @@
<?php <?php
/** /**
* AvailableBudget.php * AvailableBudget.php
* Copyright (c) 2019 james@firefly-iii.org * Copyright (c) 2019 james@firefly-iii.org
@@ -24,6 +25,7 @@ namespace FireflyIII\Models;
use Eloquent; use Eloquent;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
@@ -34,17 +36,17 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/** /**
* FireflyIII\Models\AvailableBudget * FireflyIII\Models\AvailableBudget
* *
* @property int $id * @property int $id
* @property Carbon|null $created_at * @property Carbon|null $created_at
* @property Carbon|null $updated_at * @property Carbon|null $updated_at
* @property Carbon|null $deleted_at * @property Carbon|null $deleted_at
* @property int $user_id * @property int $user_id
* @property int $transaction_currency_id * @property int $transaction_currency_id
* @property string $amount * @property string $amount
* @property Carbon $start_date * @property Carbon $start_date
* @property Carbon $end_date * @property Carbon $end_date
* @property-read TransactionCurrency $transactionCurrency * @property-read TransactionCurrency $transactionCurrency
* @property-read User $user * @property-read User $user
* @method static \Illuminate\Database\Eloquent\Builder|AvailableBudget newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|AvailableBudget newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|AvailableBudget newQuery() * @method static \Illuminate\Database\Eloquent\Builder|AvailableBudget newQuery()
* @method static Builder|AvailableBudget onlyTrashed() * @method static Builder|AvailableBudget onlyTrashed()
@@ -61,7 +63,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @method static Builder|AvailableBudget withTrashed() * @method static Builder|AvailableBudget withTrashed()
* @method static Builder|AvailableBudget withoutTrashed() * @method static Builder|AvailableBudget withoutTrashed()
* @mixin Eloquent * @mixin Eloquent
* @property int|null $user_group_id * @property int|null $user_group_id
* @method static \Illuminate\Database\Eloquent\Builder|AvailableBudget whereUserGroupId($value) * @method static \Illuminate\Database\Eloquent\Builder|AvailableBudget whereUserGroupId($value)
*/ */
class AvailableBudget extends Model class AvailableBudget extends Model
@@ -88,7 +90,7 @@ class AvailableBudget extends Model
/** /**
* Route binder. Converts the key in the URL to the specified object (or throw 404). * Route binder. Converts the key in the URL to the specified object (or throw 404).
* *
* @param string $value * @param string $value
* *
* @return AvailableBudget * @return AvailableBudget
* @throws NotFoundHttpException * @throws NotFoundHttpException
@@ -96,7 +98,7 @@ class AvailableBudget extends Model
public static function routeBinder(string $value): AvailableBudget public static function routeBinder(string $value): AvailableBudget
{ {
if (auth()->check()) { if (auth()->check()) {
$availableBudgetId = (int) $value; $availableBudgetId = (int)$value;
/** @var User $user */ /** @var User $user */
$user = auth()->user(); $user = auth()->user();
/** @var AvailableBudget $availableBudget */ /** @var AvailableBudget $availableBudget */
@@ -125,4 +127,14 @@ class AvailableBudget extends Model
{ {
return $this->belongsTo(User::class); return $this->belongsTo(User::class);
} }
/**
* @return Attribute
*/
protected function amount(): Attribute
{
return Attribute::make(
get: fn ($value) => (string)$value,
);
}
} }

View File

@@ -25,6 +25,7 @@ namespace FireflyIII\Models;
use Eloquent; use Eloquent;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
@@ -99,4 +100,24 @@ class CurrencyExchangeRate extends Model
{ {
return $this->belongsTo(User::class); return $this->belongsTo(User::class);
} }
/**
* @return Attribute
*/
protected function rate(): Attribute
{
return Attribute::make(
get: fn($value) => (string) $value,
);
}
/**
* @return Attribute
*/
protected function userRate(): Attribute
{
return Attribute::make(
get: fn($value) => (string) $value,
);
}
} }

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Models; namespace FireflyIII\Models;
use Eloquent; use Eloquent;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -164,4 +165,24 @@ class RecurrenceTransaction extends Model
{ {
return $this->belongsTo(TransactionType::class); return $this->belongsTo(TransactionType::class);
} }
/**
* @return Attribute
*/
protected function amount(): Attribute
{
return Attribute::make(
get: fn($value) => (string) $value,
);
}
/**
* @return Attribute
*/
protected function foreignAmount(): Attribute
{
return Attribute::make(
get: fn($value) => (string) $value,
);
}
} }