Files
Roardom 8b27413fe8 refactor: make laravel eloquent models final
We never plan to extend a model, and value composition over inheritance. We need to add the AllowDynamicProperties warning to prevent PHPStan errors that arise with pivot relations (https://www.github.com/larastan/larastan/issues/2256).
2025-10-29 10:19:39 +00:00

116 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <hdinnovations@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Models;
use App\Traits\Auditable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use AllowDynamicProperties;
/**
* App\Models\Poll.
*
* @property int $id
* @property int $user_id
* @property string $title
* @property int $multiple_choice
* @property \Illuminate\Support\Carbon|null $expires_at
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
*/
#[AllowDynamicProperties]
final class Poll extends Model
{
use Auditable;
/** @use HasFactory<\Database\Factories\PollFactory> */
use HasFactory;
/**
* The attributes that aren't mass assignable.
*
* @var string[]
*/
protected $guarded = [];
/**
* Get the attributes that should be cast.
*
* @return array{expires_at: 'datetime'}
*/
protected function casts(): array
{
return [
'expires_at' => 'datetime',
];
}
/**
* Get the user that created the poll.
*
* @return BelongsTo<User, $this>
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class)->withDefault([
'username' => 'System',
'id' => User::SYSTEM_USER_ID,
]);
}
/**
* Get all options for the poll.
*
* @return HasMany<Option, $this>
*/
public function options(): HasMany
{
return $this->hasMany(Option::class);
}
/**
* Get all the users that voted on the poll.
*
* @return BelongsToMany<User, $this>
*/
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class, 'voters')->withTimestamps();
}
/**
* Geth all the votes for the poll.
*
* @return HasMany<Voter, $this>
*/
public function votes(): HasMany
{
return $this->hasMany(Voter::class);
}
/**
* Set the poll's title.
*/
public function setTitleAttribute(string $title): void
{
$this->attributes['title'] = $title;
}
}