42 lines
1.1 KiB
PHP
Executable file
42 lines
1.1 KiB
PHP
Executable file
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Database\Factories\UserDishRecurrenceFactory;
|
|
use DishPlanner\UserDish\Exceptions\InvalidRecurrenceTypeException;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|
|
|
class UserDishRecurrence extends Model
|
|
{
|
|
/** @use HasFactory<UserDishRecurrenceFactory> */
|
|
use HasFactory;
|
|
|
|
protected $fillable = ['user_dish_id', 'recurrence_id', 'recurrence_type'];
|
|
|
|
protected $casts = [];
|
|
|
|
public function recurrence(): MorphTo
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
|
|
public function dishUser(): BelongsTo
|
|
{
|
|
return $this->belongsTo(UserDish::class, 'user_dish_id', 'id');
|
|
}
|
|
|
|
/**
|
|
* @throws InvalidRecurrenceTypeException
|
|
*/
|
|
public function getValue(): int
|
|
{
|
|
return match ($this->recurrence_type) {
|
|
WeeklyRecurrence::class => $this->recurrence->weekday->value,
|
|
MinimumRecurrence::class => $this->recurrence->days,
|
|
default => throw new InvalidRecurrenceTypeException()
|
|
};
|
|
}
|
|
}
|