Laravel 9 Update (#1413)

Update to Laravel 9 and PHP 8+

Co-authored-by: B.Fatih KOZ <fatih.koz@gmail.com>
This commit is contained in:
Nabeel S
2022-03-14 11:45:18 -04:00
committed by GitHub
parent 00bf18c225
commit 12848091a2
340 changed files with 6130 additions and 4502 deletions

View File

@@ -3,9 +3,10 @@
namespace App\Models;
use App\Contracts\Model;
use App\Models\Casts\DistanceCast;
use App\Models\Casts\FuelCast;
use App\Models\Traits\HashIdTrait;
use App\Support\Units\Distance;
use App\Support\Units\Fuel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
/**
* @property string id
@@ -23,10 +24,12 @@ use App\Support\Units\Fuel;
class Acars extends Model
{
use HashIdTrait;
use HasFactory;
public $table = 'acars';
protected $keyType = 'string';
public $incrementing = false;
public $fillable = [
@@ -59,13 +62,13 @@ class Acars extends Model
'nav_type' => 'integer',
'lat' => 'float',
'lon' => 'float',
'distance' => 'integer',
'distance' => DistanceCast::class,
'heading' => 'integer',
'altitude' => 'float',
'vs' => 'float',
'gs' => 'float',
'transponder' => 'integer',
'fuel' => 'float',
'fuel' => FuelCast::class,
'fuel_flow' => 'float',
];
@@ -73,38 +76,6 @@ class Acars extends Model
'pirep_id' => 'required',
];
/**
* Set the distance unit, convert to our internal default unit
*
* @param $value
*/
public function setDistanceAttribute($value): void
{
if ($value instanceof Distance) {
$this->attributes['distance'] = $value->toUnit(
config('phpvms.internal_units.distance')
);
} else {
$this->attributes['distance'] = $value;
}
}
/**
* Set the amount of fuel
*
* @param $value
*/
public function setFuelAttribute($value)
{
if ($value instanceof Fuel) {
$this->attributes['fuel'] = $value->toUnit(
config('phpvms.internal_units.fuel')
);
} else {
$this->attributes['fuel'] = $value;
}
}
/**
* FKs
*/

View File

@@ -3,10 +3,13 @@
namespace App\Models;
use App\Contracts\Model;
use App\Models\Casts\FuelCast;
use App\Models\Enums\AircraftStatus;
use App\Models\Traits\ExpensableTrait;
use App\Models\Traits\FilesTrait;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Znck\Eloquent\Traits\BelongsToThrough;
/**
@@ -32,9 +35,10 @@ use Znck\Eloquent\Traits\BelongsToThrough;
*/
class Aircraft extends Model
{
use BelongsToThrough;
use ExpensableTrait;
use FilesTrait;
use BelongsToThrough;
use HasFactory;
public $table = 'aircraft';
@@ -50,6 +54,7 @@ class Aircraft extends Model
'flight_time',
'mtow',
'zfw',
'fuel_onboard',
'status',
'state',
];
@@ -58,11 +63,12 @@ class Aircraft extends Model
* The attributes that should be casted to native types.
*/
protected $casts = [
'subfleet_id' => 'integer',
'mtow' => 'float',
'zfw' => 'float',
'flight_time' => 'float',
'state' => 'integer',
'subfleet_id' => 'integer',
'mtow' => 'float',
'zfw' => 'float',
'flight_time' => 'float',
'fuel_onboard' => FuelCast::class,
'state' => 'integer',
];
/**
@@ -78,43 +84,51 @@ class Aircraft extends Model
];
/**
* @return string
* @return Attribute
*/
public function getIdentAttribute(): string
public function active(): Attribute
{
return $this->registration.' ('.$this->icao.')';
return Attribute::make(
get: fn ($_, $attr) => $attr['status'] === AircraftStatus::ACTIVE
);
}
/**
* See if this aircraft is active
*
* @return bool
* @return Attribute
*/
public function getActiveAttribute(): bool
public function icao(): Attribute
{
return $this->status === AircraftStatus::ACTIVE;
return Attribute::make(
set: fn ($value) => strtoupper($value)
);
}
/**
* Capitalize the ICAO when set
*
* @param $icao
* @return Attribute
*/
public function setIcaoAttribute($icao): void
public function ident(): Attribute
{
$this->attributes['icao'] = strtoupper($icao);
return Attribute::make(
get: fn ($_, $attrs) => $attrs['registration'].' ('.$attrs['icao'].')'
);
}
/**
* Return the landing time in carbon format if provided
* Return the landing time
*
* @return Carbon|null
* @return Attribute
*/
public function getLandingTimeAttribute()
public function landingTime(): Attribute
{
if (array_key_exists('landing_time', $this->attributes) && filled($this->attributes['landing_time'])) {
return new Carbon($this->attributes['landing_time']);
}
return Attribute::make(
get: function ($_, $attrs) {
if (array_key_exists('landing_time', $attrs) && filled($attrs['landing_time'])) {
return new Carbon($attrs['landing_time']);
}
return $attrs['landing_time'];
}
);
}
/**

View File

@@ -6,11 +6,11 @@ use App\Contracts\Model;
use App\Models\Enums\JournalType;
use App\Models\Traits\FilesTrait;
use App\Models\Traits\JournalTrait;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Str;
/**
* Class Airline
*
* @property mixed id
* @property string code
* @property string icao
@@ -23,6 +23,7 @@ use Illuminate\Support\Str;
class Airline extends Model
{
use FilesTrait;
use HasFactory;
use JournalTrait;
public $table = 'airlines';
@@ -70,34 +71,43 @@ class Airline extends Model
/**
* For backwards compatibility
*/
public function getCodeAttribute()
public function code(): Attribute
{
if ($this->iata && $this->iata !== '') {
return $this->iata;
}
return $this->icao;
return Attribute::make(
get: function ($_, $attrs) {
if ($this->iata && $this->iata !== '') {
return $this->iata;
}
return $this->icao;
}
);
}
/**
* Capitalize the IATA code when set
*
* @param $iata
*/
public function setIataAttribute($iata)
public function iata(): Attribute
{
$this->attributes['iata'] = Str::upper($iata);
return Attribute::make(
set: fn ($iata) => Str::upper($iata)
);
}
/**
* Capitalize the ICAO when set
*
* @param $icao
*/
public function setIcaoAttribute($icao): void
public function icao(): Attribute
{
$this->attributes['icao'] = Str::upper($icao);
return Attribute::make(
set: fn ($icao) => Str::upper($icao)
);
}
/*
* FKs
*/
public function subfleets()
{
return $this->hasMany(Subfleet::class, 'airline_id');

View File

@@ -5,6 +5,8 @@ namespace App\Models;
use App\Contracts\Model;
use App\Models\Traits\ExpensableTrait;
use App\Models\Traits\FilesTrait;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
/**
* Class Airport
@@ -28,11 +30,14 @@ class Airport extends Model
{
use ExpensableTrait;
use FilesTrait;
use HasFactory;
public $table = 'airports';
protected $keyType = 'string';
public $incrementing = false;
public $timestamps = false;
protected $fillable = [
@@ -76,26 +81,31 @@ class Airport extends Model
'ground_handling_cost' => 'nullable|numeric',
'fuel_100ll_cost' => 'nullable|numeric',
'fuel_jeta_cost' => 'nullable|numeric',
'fuel_mogas_cost' => 'nullable|numeric',
'fuel_mogas_cost' => 'nullable|numeric',
];
/**
* @param $icao
* Capitalize the ICAO
*/
public function setIcaoAttribute($icao)
public function icao(): Attribute
{
$icao = strtoupper($icao);
$this->attributes['id'] = $icao;
$this->attributes['icao'] = $icao;
return Attribute::make(
set: fn ($icao) => [
'id' => strtoupper($icao),
'icao' => strtoupper($icao),
]
);
}
/**
* @param $iata
* Capitalize the IATA code
*/
public function setIataAttribute($iata): void
public function iata(): Attribute
{
$iata = strtoupper($iata);
$this->attributes['iata'] = $iata;
return Attribute::make(
set: fn ($iata) => strtoupper($iata)
);
}
/**
@@ -104,28 +114,25 @@ class Airport extends Model
*
* @return string
*/
public function getFullNameAttribute(): string
public function fullName(): Attribute
{
return $this->icao.' - '.$this->name;
return Attribute::make(
get: fn ($_, $attrs) => $this->icao.' - '.$this->name
);
}
/**
* Shorthand for getting the timezone
* Shortcut for timezone
*
* @return string
* @return Attribute
*/
public function getTzAttribute(): string
public function tz(): Attribute
{
return $this->attributes['timezone'];
}
/**
* Shorthand for setting the timezone
*
* @param $value
*/
public function setTzAttribute($value): void
{
$this->attributes['timezone'] = $value;
return Attribute::make(
get: fn ($_, $attrs) => $attrs['timezone'],
set: fn ($value) => [
'timezone' => $value,
]
);
}
}

View File

@@ -3,16 +3,23 @@
namespace App\Models;
use App\Contracts\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
/**
* The Award model
*
* @property mixed id
* @property string name
* @property string description
* @property string title
* @property string image
* @property mixed ref_model
* @property mixed|null ref_model_params
*/
class Award extends Model
{
use HasFactory;
public $table = 'awards';
protected $fillable = [

View File

@@ -0,0 +1,50 @@
<?php
namespace App\Models\Casts;
use Carbon\Carbon;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
/**
* Cast into a Carbon DateTime instance
*/
class CarbonCast implements CastsAttributes
{
/**
* Transform the attribute from the underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
*
* @return mixed
*/
public function get($model, string $key, $value, array $attributes)
{
if ($value instanceof Carbon) {
return $value;
}
return new Carbon($value);
}
/**
* Transform the attribute to its underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
*
* @return mixed
*/
public function set($model, string $key, $value, array $attributes)
{
if ($value instanceof Carbon) {
return $value->toIso8601ZuluString();
}
return $value;
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace App\Models\Casts;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
class CommaDelimitedCast implements CastsAttributes
{
/**
* Transform the attribute from the underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
*
* @return mixed
*/
public function get($model, string $key, $value, array $attributes)
{
if (empty(trim($value))) {
return [];
}
return explode(',', $value);
}
/**
* Transform the attribute to its underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
*
* @return mixed
*/
public function set($model, string $key, $value, array $attributes)
{
if (is_array($value)) {
return implode(',', $value);
}
return trim($value);
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace App\Models\Casts;
use App\Support\Units\Distance;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use PhpUnitsOfMeasure\Exception\NonNumericValue;
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
class DistanceCast implements CastsAttributes
{
/**
* Transform the attribute from the underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
*
* @return mixed
*/
public function get($model, string $key, $value, array $attributes)
{
if ($value instanceof Distance) {
return $value;
}
try {
return new Distance($value, config('phpvms.internal_units.distance'));
} catch (NonNumericValue $e) {
} catch (NonStringUnitName $e) {
return $value;
}
return $value;
}
/**
* Transform the attribute to its underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
*
* @return mixed
*/
public function set($model, string $key, $value, array $attributes)
{
if ($value instanceof Distance) {
return $value->toUnit(config('phpvms.internal_units.distance'));
}
return $value;
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace App\Models\Casts;
use App\Support\Units\Fuel;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use PhpUnitsOfMeasure\Exception\NonNumericValue;
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
class FuelCast implements CastsAttributes
{
/**
* Transform the attribute from the underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
*
* @return mixed
*/
public function get($model, string $key, $value, array $attributes)
{
if ($value instanceof Fuel) {
return $value;
}
try {
return Fuel::make($value, config('phpvms.internal_units.fuel'));
} catch (NonNumericValue $e) {
} catch (NonStringUnitName $e) {
return $value;
}
return $value;
}
/**
* Transform the attribute to its underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
*
* @return mixed
*/
public function set($model, string $key, $value, array $attributes)
{
if ($value instanceof Fuel) {
return $value->toUnit(config('phpvms.internal_units.fuel'));
}
return $value;
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace App\Models\Casts;
use App\Support\Money;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
class MoneyCast implements CastsAttributes
{
/**
* Transform the attribute from the underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
*
* @return mixed
*/
public function get($model, string $key, $value, array $attributes)
{
if ($value instanceof Money) {
return $value;
}
return new Money($value);
}
/**
* Transform the attribute to its underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
*
* @return mixed
*/
public function set($model, string $key, $value, array $attributes)
{
$value = ($value instanceof Money)
? $value
: new Money($value);
return $value ? (int) $value->getAmount() : null;
}
}

View File

@@ -9,7 +9,7 @@ class ActiveState extends Enum
public const INACTIVE = 0;
public const ACTIVE = 1;
public static $labels = [
public static array $labels = [
self::ACTIVE => 'common.active',
self::INACTIVE => 'common.inactive',
];

View File

@@ -10,7 +10,7 @@ class AircraftState extends Enum
public const IN_USE = 1;
public const IN_AIR = 2;
public static $labels = [
public static array $labels = [
self::PARKED => 'On Ground',
self::IN_USE => 'In Use',
self::IN_AIR => 'In Air',

View File

@@ -13,7 +13,7 @@ class AircraftStatus extends Enum
public const SCRAPPED = 'C';
public const WRITTEN_OFF = 'W';
public static $labels = [
public static array $labels = [
self::ACTIVE => 'aircraft.status.active',
self::MAINTENANCE => 'aircraft.status.maintenance',
self::STORED => 'aircraft.status.stored',

View File

@@ -17,7 +17,7 @@ class Days extends Enum
public const SATURDAY = 1 << 5;
public const SUNDAY = 1 << 6;
public static $labels = [
public static array $labels = [
self::MONDAY => 'common.days.mon',
self::TUESDAY => 'common.days.tues',
self::WEDNESDAY => 'common.days.wed',
@@ -27,7 +27,7 @@ class Days extends Enum
self::SUNDAY => 'common.days.sun',
];
public static $codes = [
public static array $codes = [
'M' => self::MONDAY,
'T' => self::TUESDAY,
'W' => self::WEDNESDAY,

View File

@@ -10,13 +10,13 @@ class ExpenseType extends Enum
public const DAILY = 'D';
public const MONTHLY = 'M';
protected static $labels = [
protected static array $labels = [
self::FLIGHT => 'expenses.type.flight',
self::DAILY => 'expenses.type.daily',
self::MONTHLY => 'expenses.type.monthly',
];
protected static $codes = [
protected static array $codes = [
self::FLIGHT => 'F',
self::DAILY => 'D',
self::MONTHLY => 'M',

View File

@@ -9,7 +9,7 @@ class FareType extends Enum
public const PASSENGER = 0;
public const CARGO = 1;
public static $labels = [
public static array $labels = [
self::PASSENGER => 'Passenger',
self::CARGO => 'Cargo',
];

View File

@@ -22,7 +22,7 @@ class FlightType extends Enum
public const MILITARY = 'W';
public const TECHNICAL_STOP = 'X';
protected static $labels = [
protected static array $labels = [
self::SCHED_PAX => 'flights.type.pass_scheduled',
self::SCHED_CARGO => 'flights.type.cargo_scheduled',
self::CHARTER_PAX_ONLY => 'flights.type.charter_pass_only',

View File

@@ -10,7 +10,7 @@ class FuelType extends Enum
public const JET_A = 1;
public const MOGAS = 2;
public static $labels = [
public static array $labels = [
self::LOW_LEAD => '100LL',
self::JET_A => 'JET A',
self::MOGAS => 'MOGAS',

View File

@@ -14,7 +14,7 @@ class ImportExportType extends Enum
public const FLIGHTS = 6;
public const SUBFLEETS = 7;
public static $labels = [
public static array $labels = [
self::AIRLINE => 'airline',
self::AIRCRAFT => 'aircraft',
self::AIRPORT => 'airport',

View File

@@ -30,7 +30,7 @@ class NavaidType extends Enum
*
* @var array
*/
public static $labels = [
public static array $labels = [
self::VOR => 'VOR',
self::VOR_DME => 'VOR DME',
self::LOC => 'Localizer',

View File

@@ -9,7 +9,7 @@ class PageType extends Enum
public const PAGE = 0;
public const LINK = 1;
public static $labels = [
public static array $labels = [
self::PAGE => 'Page',
self::LINK => 'Link',
];

View File

@@ -9,7 +9,7 @@ class PirepSource extends Enum
public const MANUAL = 0;
public const ACARS = 1;
protected static $labels = [
protected static array $labels = [
self::MANUAL => 'pireps.source_types.manual',
self::ACARS => 'pireps.source_types.acars',
];

View File

@@ -15,7 +15,7 @@ class PirepState extends Enum
public const REJECTED = 6;
public const PAUSED = 7;
protected static $labels = [
protected static array $labels = [
self::IN_PROGRESS => 'pireps.state.in_progress',
self::PENDING => 'pireps.state.pending',
self::ACCEPTED => 'pireps.state.accepted',

View File

@@ -36,7 +36,7 @@ class PirepStatus extends Enum
public const EMERG_DESCENT = 'EMG';
public const PAUSED = 'PSD';
protected static $labels = [
protected static array $labels = [
self::INITIATED => 'pireps.status.initialized',
self::SCHEDULED => 'pireps.status.scheduled',
self::BOARDING => 'pireps.status.boarding',

View File

@@ -13,7 +13,7 @@ class UserState extends Enum
public const SUSPENDED = 4;
public const DELETED = 5;
protected static $labels = [
protected static array $labels = [
self::PENDING => 'user.state.pending',
self::ACTIVE => 'user.state.active',
self::REJECTED => 'user.state.rejected',

View File

@@ -3,22 +3,26 @@
namespace App\Models;
use App\Contracts\Model;
use App\Models\Casts\CommaDelimitedCast;
use App\Models\Traits\ReferenceTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
/**
* @property int airline_id
* @property float amount
* @property string name
* @property string type
* @property string flight_type
* @property string ref_model
* @property string ref_model_id
* @property bool charge_to_user
* @property int airline_id
* @property float amount
* @property string name
* @property string type
* @property string flight_type
* @property string ref_model
* @property string ref_model_id
* @property bool charge_to_user
* @property Airline $airline
*
* @mixin \Illuminate\Database\Eloquent\Builder
*/
class Expense extends Model
{
use HasFactory;
use ReferenceTrait;
public $table = 'expenses';
@@ -36,6 +40,10 @@ class Expense extends Model
'active',
];
public $casts = [
'flight_type' => CommaDelimitedCast::class,
];
public static $rules = [
'active' => 'bool',
'airline_id' => 'integer',
@@ -44,34 +52,6 @@ class Expense extends Model
'charge_to_user' => 'bool',
];
/**
* flight_type is stored a comma delimited list in table. Retrieve it as an array
*
* @return array
*/
public function getFlightTypeAttribute()
{
if (empty(trim($this->attributes['flight_type']))) {
return [];
}
return explode(',', $this->attributes['flight_type']);
}
/**
* Make sure the flight type is stored a comma-delimited list in the table
*
* @param string $value
*/
public function setFlightTypeAttribute($value)
{
if (is_array($value)) {
$this->attributes['flight_type'] = implode(',', $value);
} else {
$this->attributes['flight_type'] = trim($value);
}
}
/**
* Foreign Keys
*/

View File

@@ -3,6 +3,7 @@
namespace App\Models;
use App\Contracts\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
/**
* @property string name
@@ -17,6 +18,8 @@ use App\Contracts\Model;
*/
class Fare extends Model
{
use HasFactory;
public $table = 'fares';
protected $fillable = [

View File

@@ -5,6 +5,7 @@ namespace App\Models;
use App\Contracts\Model;
use App\Models\Traits\HashIdTrait;
use App\Models\Traits\ReferenceTrait;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
@@ -26,9 +27,11 @@ class File extends Model
public $table = 'files';
protected $keyType = 'string';
public $incrementing = false;
protected $fillable = [
'id',
'name',
'description',
'disk',
@@ -51,52 +54,64 @@ class File extends Model
/**
* Return the file extension
*
* @return string
* @return Attribute
*/
public function getExtensionAttribute(): string
public function extension(): Attribute
{
if (!$this->pathinfo) {
$this->pathinfo = pathinfo($this->path);
}
return Attribute::make(
get: function ($_, $attrs) {
if (!$this->pathinfo) {
$this->pathinfo = pathinfo($this->path);
}
return $this->pathinfo['extension'];
return $this->pathinfo['extension'];
}
);
}
/**
* Get just the filename
*
* @return string
* @return Attribute
*/
public function getFilenameAttribute(): string
public function filename(): Attribute
{
if (!$this->pathinfo) {
$this->pathinfo = pathinfo($this->path);
}
return Attribute::make(
get: function ($_, $attrs) {
if (!$this->pathinfo) {
$this->pathinfo = pathinfo($this->path);
}
return $this->pathinfo['filename'].'.'.$this->pathinfo['extension'];
return $this->pathinfo['filename'].'.'.$this->pathinfo['extension'];
}
);
}
/**
* Get the full URL to this attribute
*
* @return string
* @return Attribute
*/
public function getUrlAttribute(): string
public function url(): Attribute
{
if (Str::startsWith($this->path, 'http')) {
return $this->path;
}
return Attribute::make(
get: function ($_, $attrs) {
if (Str::startsWith($this->path, 'http')) {
return $this->path;
}
$disk = $this->disk ?? config('filesystems.public_files');
$disk = $this->disk ?? config('filesystems.public_files');
// If the disk isn't stored in public (S3 or something),
// just pass through the URL call
if ($disk !== 'public') {
return Storage::disk(config('filesystems.public_files'))
->url($this->path);
}
// If the disk isn't stored in public (S3 or something),
// just pass through the URL call
if ($disk !== 'public') {
return Storage::disk(config('filesystems.public_files'))
->url($this->path);
}
// Otherwise, figure out the public URL and save there
return public_asset(Storage::disk('public')->url($this->path));
// Otherwise, figure out the public URL and save there
return public_asset(Storage::disk('public')->url($this->path));
}
);
}
}

View File

@@ -3,10 +3,13 @@
namespace App\Models;
use App\Contracts\Model;
use App\Models\Casts\DistanceCast;
use App\Models\Enums\Days;
use App\Models\Traits\HashIdTrait;
use App\Support\Units\Distance;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Collection;
/**
@@ -23,8 +26,8 @@ use Illuminate\Support\Collection;
* @property Collection fares
* @property Collection subfleets
* @property int days
* @property int distance
* @property int planned_distance
* @property Distance distance
* @property Distance planned_distance
* @property int flight_time
* @property string route
* @property string dpt_time
@@ -48,14 +51,17 @@ use Illuminate\Support\Collection;
class Flight extends Model
{
use HashIdTrait;
use HasFactory;
public $table = 'flights';
/** The form wants this */
public $hours;
public $minutes;
protected $keyType = 'string';
public $incrementing = false;
protected $fillable = [
@@ -91,7 +97,7 @@ class Flight extends Model
'flight_number' => 'integer',
'days' => 'integer',
'level' => 'integer',
'distance' => 'float',
'distance' => DistanceCast::class,
'flight_time' => 'integer',
'start_date' => 'date',
'end_date' => 'date',
@@ -139,36 +145,24 @@ class Flight extends Model
/**
* Get the flight ident, e.,g JBU1900/C.nn/L.yy
*/
public function getIdentAttribute(): string
public function ident(): Attribute
{
$flight_id = optional($this->airline)->code;
$flight_id .= $this->flight_number;
return Attribute::make(
get: function ($_, $attrs) {
$flight_id = optional($this->airline)->code;
$flight_id .= $this->flight_number;
if (filled($this->route_code)) {
$flight_id .= '/C.'.$this->route_code;
}
if (filled($this->route_code)) {
$flight_id .= '/C.'.$this->route_code;
}
if (filled($this->route_leg)) {
$flight_id .= '/L.'.$this->route_leg;
}
if (filled($this->route_leg)) {
$flight_id .= '/L.'.$this->route_leg;
}
return $flight_id;
}
/**
* Set the distance unit, convert to our internal default unit
*
* @param $value
*/
public function setDistanceAttribute($value): void
{
if ($value instanceof Distance) {
$this->attributes['distance'] = $value->toUnit(
config('phpvms.internal_units.distance')
);
} else {
$this->attributes['distance'] = $value;
}
return $flight_id;
}
);
}
/**
@@ -202,20 +196,25 @@ class Flight extends Model
* Set the days parameter. If an array is passed, it's
* AND'd together to create the mask value
*
* @param array|int $val
* @return Attribute
*/
public function setDaysAttribute($val): void
public function days(): Attribute
{
if (\is_array($val)) {
$val = Days::getDaysMask($val);
}
return Attribute::make(
set: function ($value) {
if (\is_array($value)) {
$value = Days::getDaysMask($value);
}
$this->attributes['days'] = $val;
return $value;
}
);
}
/**
* Relationship
/*
* Relationships
*/
public function airline()
{
return $this->belongsTo(Airline::class, 'airline_id');

View File

@@ -3,6 +3,7 @@
namespace App\Models;
use App\Contracts\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
/**
* Class FlightField
@@ -33,11 +34,15 @@ class FlightField extends Model
/**
* When setting the name attribute, also set the slug
*
* @param $name
* @return Attribute
*/
public function setNameAttribute($name): void
public function name(): Attribute
{
$this->attributes['name'] = $name;
$this->attributes['slug'] = str_slug($name);
return Attribute::make(
set: fn ($name) => [
'name' => $name,
'slug' => str_slug($name),
]
);
}
}

View File

@@ -3,6 +3,7 @@
namespace App\Models;
use App\Contracts\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
/**
* Class FlightFieldValue
@@ -25,12 +26,18 @@ class FlightFieldValue extends Model
public static $rules = [];
/**
* @param $name
* When setting the name attribute, also set the slug
*
* @return Attribute
*/
public function setNameAttribute($name): void
public function name(): Attribute
{
$this->attributes['name'] = $name;
$this->attributes['slug'] = str_slug($name);
return Attribute::make(
set: fn ($name) => [
'name' => $name,
'slug' => str_slug($name),
]
);
}
/**

View File

@@ -7,8 +7,10 @@
namespace App\Models;
use App\Contracts\Model;
use App\Models\Casts\MoneyCast;
use App\Support\Money;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
/**
* Holds various journals, depending on the morphed_type and morphed_id columns
@@ -25,6 +27,8 @@ use Carbon\Carbon;
*/
class Journal extends Model
{
use HasFactory;
protected $table = 'journals';
protected $fillable = [
@@ -36,6 +40,10 @@ class Journal extends Model
'morphed_id',
];
public $casts = [
'balance' => MoneyCast::class,
];
protected $dates = [
'created_at',
'deleted_at',
@@ -100,34 +108,6 @@ class Journal extends Model
$this->save();
}
/**
* @param $value
*
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
*
* @return Money
*/
public function getBalanceAttribute($value): Money
{
return new Money($value);
}
/**
* @param $value
*
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
*/
public function setBalanceAttribute($value): void
{
$value = ($value instanceof Money)
? $value
: new Money($value);
$this->attributes['balance'] = $value ? (int) $value->getAmount() : null;
}
/**
* @param Journal $object
*

View File

@@ -8,6 +8,7 @@ namespace App\Models;
use App\Contracts\Model;
use App\Models\Traits\ReferenceTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
/**
* @property string id UUID type
@@ -23,6 +24,7 @@ use App\Models\Traits\ReferenceTrait;
*/
class JournalTransaction extends Model
{
use HasFactory;
use ReferenceTrait;
protected $table = 'journal_transactions';

View File

@@ -3,13 +3,19 @@
namespace App\Models;
use App\Contracts\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Navdata extends Model
{
use HasFactory;
public $table = 'navdata';
protected $keyType = 'string';
public $timestamps = false;
public $incrementing = false;
protected $fillable = [
@@ -31,10 +37,12 @@ class Navdata extends Model
/**
* Make sure the ID is in all caps
*
* @param $id
* @return Attribute
*/
public function setIdAttribute($id): void
public function id(): Attribute
{
$this->attributes['id'] = strtoupper($id);
return Attribute::make(
set: fn ($id) => strtoupper($id)
);
}
}

View File

@@ -3,6 +3,7 @@
namespace App\Models;
use App\Contracts\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
/**
@@ -14,6 +15,7 @@ use Illuminate\Notifications\Notifiable;
*/
class News extends Model
{
use HasFactory;
use Notifiable;
public $table = 'news';

View File

@@ -5,6 +5,7 @@ namespace App\Models;
use App\Contracts\Model;
use App\Exceptions\UnknownPageType;
use App\Models\Enums\PageType;
use Illuminate\Database\Eloquent\Casts\Attribute;
/**
* @property int id
@@ -52,16 +53,20 @@ class Page extends Model
*
* @throws \App\Exceptions\UnknownPageType
*/
public function getUrlAttribute(): string
public function url(): Attribute
{
if ($this->type === PageType::PAGE) {
return url(route('frontend.pages.show', ['slug' => $this->slug]));
}
return Attribute::make(
get: function ($value, $attrs) {
if ($this->type === PageType::PAGE) {
return url(route('frontend.pages.show', ['slug' => $this->slug]));
}
if ($this->type === PageType::LINK) {
return $this->link;
}
if ($this->type === PageType::LINK) {
return $this->link;
}
throw new UnknownPageType($this);
throw new UnknownPageType($this);
}
);
}
}

View File

@@ -5,6 +5,9 @@ namespace App\Models;
use App\Contracts\Model;
use App\Events\PirepStateChange;
use App\Events\PirepStatusChange;
use App\Models\Casts\CarbonCast;
use App\Models\Casts\DistanceCast;
use App\Models\Casts\FuelCast;
use App\Models\Enums\AcarsType;
use App\Models\Enums\PirepFieldSource;
use App\Models\Enums\PirepState;
@@ -12,6 +15,8 @@ use App\Models\Traits\HashIdTrait;
use App\Support\Units\Distance;
use App\Support\Units\Fuel;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Collection;
use Kleemans\AttributeEvents;
@@ -37,10 +42,10 @@ use Kleemans\AttributeEvents;
* @property int block_time
* @property int flight_time In minutes
* @property int planned_flight_time
* @property float block_fuel
* @property float fuel_used
* @property float distance
* @property float planned_distance
* @property Fuel block_fuel
* @property Fuel fuel_used
* @property Distance distance
* @property Distance planned_distance
* @property int level
* @property string route
* @property int score
@@ -63,15 +68,18 @@ class Pirep extends Model
{
use AttributeEvents;
use HashIdTrait;
use HasFactory;
use Notifiable;
public $table = 'pireps';
protected $keyType = 'string';
public $incrementing = false;
/** The form wants this */
public $hours;
public $minutes;
protected $fillable = [
@@ -116,18 +124,21 @@ class Pirep extends Model
'airline_id' => 'integer',
'aircraft_id' => 'integer',
'level' => 'integer',
'distance' => 'float',
'planned_distance' => 'float',
'distance' => DistanceCast::class,
'planned_distance' => DistanceCast::class,
'block_time' => 'integer',
'block_off_time' => CarbonCast::class,
'block_on_time' => CarbonCast::class,
'flight_time' => 'integer',
'planned_flight_time' => 'integer',
'zfw' => 'float',
'block_fuel' => 'float',
'fuel_used' => 'float',
'block_fuel' => FuelCast::class,
'fuel_used' => FuelCast::class,
'landing_rate' => 'float',
'score' => 'integer',
'source' => 'integer',
'state' => 'integer',
'submitted_at' => CarbonCast::class,
];
public static $rules = [
@@ -217,193 +228,112 @@ class Pirep extends Model
/**
* Get the flight ident, e.,g JBU1900/C.nn/L.yy
*/
public function getIdentAttribute(): string
public function ident(): Attribute
{
$flight_id = optional($this->airline)->code;
$flight_id .= $this->flight_number;
return Attribute::make(
get: function ($value, $attrs) {
$flight_id = optional($this->airline)->code;
$flight_id .= $this->flight_number;
if (filled($this->route_code)) {
$flight_id .= '/C.'.$this->route_code;
}
if (filled($this->route_code)) {
$flight_id .= '/C.'.$this->route_code;
}
if (filled($this->route_leg)) {
$flight_id .= '/L.'.$this->route_leg;
}
if (filled($this->route_leg)) {
$flight_id .= '/L.'.$this->route_leg;
}
return $flight_id;
}
/**
* Return the block off time in carbon format
*
* @return Carbon|null
*/
public function getBlockOffTimeAttribute()
{
if (array_key_exists('block_off_time', $this->attributes)) {
return new Carbon($this->attributes['block_off_time']);
}
}
/**
* Return the block on time
*
* @return Carbon|null
*/
public function getBlockOnTimeAttribute()
{
if (array_key_exists('block_on_time', $this->attributes)) {
return new Carbon($this->attributes['block_on_time']);
}
}
/**
* Return the block on time
*
* @return Carbon
*/
public function getSubmittedAtAttribute()
{
if (array_key_exists('submitted_at', $this->attributes)) {
return new Carbon($this->attributes['submitted_at']);
}
}
/**
* Set the distance unit, convert to our internal default unit
*
* @param $value
*/
public function setDistanceAttribute($value): void
{
if ($value instanceof Distance) {
$this->attributes['distance'] = $value->toUnit(
config('phpvms.internal_units.distance')
);
} else {
$this->attributes['distance'] = $value;
}
return $flight_id;
}
);
}
/**
* Return if this PIREP can be edited or not
*/
public function getReadOnlyAttribute(): bool
public function readOnly(): Attribute
{
return \in_array($this->state, static::$read_only_states, true);
return Attribute::make(
get: fn ($_, $attrs) => \in_array($this->state, static::$read_only_states, true)
);
}
/**
* Return the flight progress in a percent.
*/
public function getProgressPercentAttribute()
public function progressPercent(): Attribute
{
$distance = $this->distance;
return Attribute::make(
get: function ($_, $attrs) {
$distance = $attrs['distance'];
$upper_bound = $distance;
if ($this->planned_distance) {
$upper_bound = $this->planned_distance;
}
$upper_bound = $distance;
if ($attrs['planned_distance']) {
$upper_bound = $attrs['planned_distance'];
}
$upper_bound = empty($upper_bound) ? 1 : $upper_bound;
$distance = empty($distance) ? $upper_bound : $distance;
$upper_bound = empty($upper_bound) ? 1 : $upper_bound;
$distance = empty($distance) ? $upper_bound : $distance;
return round(($distance / $upper_bound) * 100, 0);
return round(($distance / $upper_bound) * 100);
}
);
}
/**
* Get the pirep_fields and then the pirep_field_values and
* merge them together. If a field value doesn't exist then add in a fake one
*/
public function getFieldsAttribute()
public function fields(): Attribute
{
$custom_fields = PirepField::all();
$field_values = PirepFieldValue::where('pirep_id', $this->id)
->orderBy('created_at', 'asc')
->get();
return Attribute::make(
get: function ($_, $attrs) {
$custom_fields = PirepField::all();
$field_values = PirepFieldValue::where('pirep_id', $this->id)
->orderBy('created_at', 'asc')
->get();
// Merge the field values into $fields
foreach ($custom_fields as $field) {
$has_value = $field_values->firstWhere('slug', $field->slug);
if (!$has_value) {
$field_values->push(new PirepFieldValue([
'pirep_id' => $this->id,
'name' => $field->name,
'slug' => $field->slug,
'value' => '',
'source' => PirepFieldSource::MANUAL,
]));
// Merge the field values into $fields
foreach ($custom_fields as $field) {
$has_value = $field_values->firstWhere('slug', $field->slug);
if (!$has_value) {
$field_values->push(
new PirepFieldValue([
'pirep_id' => $this->id,
'name' => $field->name,
'slug' => $field->slug,
'value' => '',
'source' => PirepFieldSource::MANUAL,
])
);
}
}
return $field_values;
}
}
return $field_values;
}
/**
* Set the amount of block fuel
*
* @param $value
*/
public function setBlockFuelAttribute($value): void
{
if ($value instanceof Fuel) {
$this->attributes['block_fuel'] = $value->toUnit(
config('phpvms.internal_units.fuel')
);
} else {
$this->attributes['block_fuel'] = $value;
}
}
/**
* Set the amount of fuel used
*
* @param $value
*/
public function setFuelUsedAttribute($value): void
{
if ($value instanceof Fuel) {
$this->attributes['fuel_used'] = $value->toUnit(
config('phpvms.internal_units.fuel')
);
} else {
$this->attributes['fuel_used'] = $value;
}
}
/**
* Set the distance unit, convert to our internal default unit
*
* @param $value
*/
public function setPlannedDistanceAttribute($value): void
{
if ($value instanceof Distance) {
$this->attributes['planned_distance'] = $value->toUnit(
config('phpvms.internal_units.distance')
);
} else {
$this->attributes['planned_distance'] = $value;
}
);
}
/**
* Do some cleanup on the route
*
* @param $route
* @return Attribute
*/
public function setRouteAttribute($route): void
public function route(): Attribute
{
$route = strtoupper(trim($route));
$this->attributes['route'] = $route;
return Attribute::make(
set: fn ($route) => strtoupper(trim($route))
);
}
/**
* Return if this is cancelled or not
*/
public function getCancelledAttribute(): bool
public function cancelled(): Attribute
{
return $this->state === PirepState::CANCELLED;
return Attribute::make(
get: fn ($_, $attrs) => $this->state === PirepState::CANCELLED
);
}
/**

View File

@@ -3,12 +3,15 @@
namespace App\Models;
use App\Contracts\Model;
use Carbon\Carbon;
/**
* @property string $pirep_id
* @property string $comment
* @property int $user_id
* @property Pirep $pirep
* @property User $user
* @property Carbon $created_at
*/
class PirepComment extends Model
{

View File

@@ -3,6 +3,7 @@
namespace App\Models;
use App\Contracts\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
/**
* @property string name
@@ -30,11 +31,15 @@ class PirepField extends Model
/**
* When setting the name attribute, also set the slug
*
* @param $name
* @return Attribute
*/
public function setNameAttribute($name): void
public function name(): Attribute
{
$this->attributes['name'] = $name;
$this->attributes['slug'] = str_slug($name);
return Attribute::make(
set: fn ($name) => [
'name' => $name,
'slug' => str_slug($name),
]
);
}
}

View File

@@ -4,6 +4,7 @@ namespace App\Models;
use App\Contracts\Model;
use App\Models\Enums\PirepFieldSource;
use Illuminate\Database\Eloquent\Casts\Attribute;
/**
* @property string pirep_id
@@ -35,23 +36,31 @@ class PirepFieldValue extends Model
'source' => 'integer',
];
/**
* When setting the name attribute, also set the slug
*
* @return Attribute
*/
public function name(): Attribute
{
return Attribute::make(
set: fn ($name) => [
'name' => $name,
'slug' => str_slug($name),
]
);
}
/**
* If it was filled in from ACARS, then it's read only
*
* @return bool
*/
public function getReadOnlyAttribute()
public function readOnly(): Attribute
{
return $this->source === PirepFieldSource::ACARS;
}
/**
* @param $name
*/
public function setNameAttribute($name): void
{
$this->attributes['name'] = $name;
$this->attributes['slug'] = str_slug($name);
return Attribute::make(
get: fn ($_, $attrs) => $this->source === PirepFieldSource::ACARS
);
}
/**

View File

@@ -3,6 +3,7 @@
namespace App\Models;
use App\Contracts\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
/**
* @property string name
@@ -15,6 +16,8 @@ use App\Contracts\Model;
*/
class Rank extends Model
{
use HasFactory;
public $table = 'ranks';
protected $fillable = [

View File

@@ -2,6 +2,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Laratrust\Models\LaratrustRole;
/**
@@ -15,6 +16,8 @@ use Laratrust\Models\LaratrustRole;
*/
class Role extends LaratrustRole
{
use HasFactory;
protected $fillable = [
'id',
'name',

View File

@@ -3,6 +3,7 @@
namespace App\Models;
use App\Contracts\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
/**
* @property string id
@@ -12,6 +13,7 @@ use App\Contracts\Model;
* @property string group
* @property string type
* @property string options
* @property int order
* @property string description
*/
class Setting extends Model
@@ -40,9 +42,9 @@ class Setting extends Model
/**
* @param $key
*
* @return mixed
* @return string
*/
public static function formatKey($key)
public static function formatKey($key): string
{
return str_replace('.', '_', strtolower($key));
}
@@ -50,21 +52,24 @@ class Setting extends Model
/**
* Force formatting the key
*
* @param $id
* @return Attribute
*/
public function setIdAttribute($id): void
public function id(): Attribute
{
$id = strtolower($id);
$this->attributes['id'] = self::formatKey($id);
return Attribute::make(
get: fn ($id, $attrs) => self::formatKey(strtolower($id))
);
}
/**
* Set the key to lowercase
*
* @param $key
* @return Attribute
*/
public function setKeyAttribute($key): void
public function key(): Attribute
{
$this->attributes['key'] = strtolower($key);
return Attribute::make(
set: fn ($key) => strtolower($key)
);
}
}

View File

@@ -3,6 +3,7 @@
namespace App\Models;
use App\Contracts\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Collection;
/**
@@ -25,6 +26,8 @@ use Illuminate\Support\Collection;
*/
class SimBrief extends Model
{
use HasFactory;
public $table = 'simbrief';
public $incrementing = false;

View File

@@ -6,6 +6,8 @@ use App\Contracts\Model;
use App\Models\Enums\AircraftStatus;
use App\Models\Traits\ExpensableTrait;
use App\Models\Traits\FilesTrait;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
/**
* @property int id
@@ -26,6 +28,7 @@ class Subfleet extends Model
{
use ExpensableTrait;
use FilesTrait;
use HasFactory;
public $fillable = [
'airline_id',
@@ -64,12 +67,13 @@ class Subfleet extends Model
];
/**
* @param $type
* @return Attribute
*/
public function setTypeAttribute($type)
public function type(): Attribute
{
$type = str_replace([' ', ','], ['-', ''], $type);
$this->attributes['type'] = $type;
return Attribute::make(
set: fn ($type) => str_replace([' ', ','], ['-', ''], $type)
);
}
/**

View File

@@ -4,6 +4,8 @@ namespace App\Models;
use App\Models\Enums\JournalType;
use App\Models\Traits\JournalTrait;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laratrust\Traits\LaratrustUserTrait;
@@ -51,10 +53,11 @@ use Staudenmeir\EloquentHasManyDeep\HasRelationships;
*/
class User extends Authenticatable
{
use HasFactory;
use HasRelationships;
use JournalTrait;
use LaratrustUserTrait;
use Notifiable;
use HasRelationships;
public $table = 'users';
@@ -128,67 +131,86 @@ class User extends Authenticatable
];
/**
* @return string
* Format the pilot ID/ident
*
* @return Attribute
*/
public function getIdentAttribute(): string
public function ident(): Attribute
{
$length = setting('pilots.id_length');
$ident_code = filled(setting('pilots.id_code')) ? setting('pilots.id_code') : optional($this->airline)->icao;
return Attribute::make(
get: function ($_, $attrs) {
$length = setting('pilots.id_length');
$ident_code = filled(setting('pilots.id_code')) ? setting(
'pilots.id_code'
) : optional($this->airline)->icao;
return $ident_code.str_pad($this->pilot_id, $length, '0', STR_PAD_LEFT);
return $ident_code.str_pad($attrs['pilot_id'], $length, '0', STR_PAD_LEFT);
}
);
}
/**
* Return a "privatized" version of someones name - First name full, rest of the names are initials
* Return a "privatized" version of someones name - First and middle names full, last name initials
*
* @return string
* @return Attribute
*/
public function getNamePrivateAttribute(): string
public function namePrivate(): Attribute
{
$name_parts = explode(' ', $this->attributes['name']);
$count = count($name_parts);
if ($count === 1) {
return $name_parts[0];
}
return Attribute::make(
get: function ($_, $attrs) {
$name_parts = explode(' ', $attrs['name']);
$count = count($name_parts);
if ($count === 1) {
return $name_parts[0];
}
$first_name = $name_parts[0];
$last_name = $name_parts[$count - 1];
$gdpr_name = '';
$last_name = $name_parts[$count - 1];
$loop_count = 0;
return $first_name.' '.mb_substr($last_name, 0, 1);
while ($loop_count < ($count - 1)) {
$gdpr_name .= $name_parts[$loop_count].' ';
$loop_count++;
}
$gdpr_name .= mb_substr($last_name, 0, 1);
return mb_convert_case($gdpr_name, MB_CASE_TITLE);
}
);
}
/**
* Shorthand for getting the timezone
* Shortcut for timezone
*
* @return string
* @return Attribute
*/
public function getTzAttribute(): string
public function tz(): Attribute
{
return $this->timezone;
}
/**
* Shorthand for setting the timezone
*
* @param $value
*/
public function setTzAttribute($value)
{
$this->attributes['timezone'] = $value;
return Attribute::make(
get: fn ($_, $attrs) => $attrs['timezone'],
set: fn ($value) => [
'timezone' => $value,
]
);
}
/**
* Return a File model
*/
public function getAvatarAttribute()
public function avatar(): Attribute
{
if (!$this->attributes['avatar']) {
return null;
}
return Attribute::make(
get: function ($_, $attrs) {
if (!$attrs['avatar']) {
return null;
}
return new File([
'path' => $this->attributes['avatar'],
]);
return new File([
'path' => $attrs['avatar'],
]);
}
);
}
/**
@@ -211,10 +233,12 @@ class User extends Authenticatable
public function resolveAvatarUrl()
{
$avatar = $this->getAvatarAttribute();
/** @var File $avatar */
$avatar = $this->avatar;
if (empty($avatar)) {
return $this->gravatar();
}
return $avatar->url;
}
@@ -276,11 +300,19 @@ class User extends Authenticatable
public function typeratings()
{
return $this->belongsToMany(Typerating::class, 'typerating_user', 'user_id', 'typerating_id');
return $this->belongsToMany(
Typerating::class,
'typerating_user',
'user_id',
'typerating_id'
);
}
public function rated_subfleets()
{
return $this->hasManyDeep(Subfleet::class, ['typerating_user', Typerating::class, 'typerating_subfleet']);
return $this->hasManyDeep(
Subfleet::class,
['typerating_user', Typerating::class, 'typerating_subfleet']
);
}
}

View File

@@ -3,6 +3,7 @@
namespace App\Models;
use App\Contracts\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
/**
* @property string name
@@ -40,10 +41,12 @@ class UserField extends Model
/**
* Get the slug so we can use it in forms
*
* @return string
* @return Attribute
*/
public function getSlugAttribute(): string
public function slug(): Attribute
{
return str_slug($this->name, '_');
return Attribute::make(
get: fn ($_, $attrs) => str_slug($attrs['name'], '_')
);
}
}

View File

@@ -3,6 +3,7 @@
namespace App\Models;
use App\Contracts\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
/**
* @property string name
@@ -25,9 +26,11 @@ class UserFieldValue extends Model
/**
* Return related field's name along with field values
*/
public function getNameAttribute(): string
public function name(): Attribute
{
return optional($this->field)->name;
return Attribute::make(
get: fn ($_, $attrs) => optional($this->field)->name
);
}
/**