fedi-feed-router/app/Models/Article.php

80 lines
1.8 KiB
PHP
Raw Normal View History

2025-06-29 09:37:49 +02:00
<?php
namespace App\Models;
2025-06-29 09:48:45 +02:00
use App\Events\ArticleFetched;
2025-06-29 09:37:49 +02:00
use Database\Factories\ArticleFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2025-06-30 18:18:30 +02:00
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
2025-06-29 19:46:50 +02:00
use Illuminate\Support\Carbon;
2025-06-29 09:37:49 +02:00
/**
* @method static firstOrCreate(string[] $array)
2025-06-29 19:46:50 +02:00
* @property integer $id
* @property string $url
* @property bool|null $is_valid
* @property Carbon|null $validated_at
* @property Carbon $created_at
* @property Carbon $updated_at
2025-06-30 18:18:30 +02:00
* @property ArticlePublication $articlePublication
2025-06-29 09:37:49 +02:00
*/
class Article extends Model
{
/** @use HasFactory<ArticleFactory> */
use HasFactory;
protected $fillable = [
'url',
2025-06-29 21:20:45 +02:00
'title',
'description',
2025-06-29 19:46:50 +02:00
'is_valid',
2025-06-30 19:54:43 +02:00
'is_duplicate',
2025-06-29 21:20:45 +02:00
'fetched_at',
2025-06-29 19:46:50 +02:00
'validated_at',
2025-06-29 09:37:49 +02:00
];
public function casts(): array
{
return [
2025-06-30 19:54:43 +02:00
'is_valid' => 'boolean',
'is_duplicate' => 'boolean',
2025-06-29 21:20:45 +02:00
'fetched_at' => 'datetime',
2025-06-29 19:46:50 +02:00
'validated_at' => 'datetime',
2025-06-29 09:37:49 +02:00
'created_at' => 'datetime',
2025-06-29 17:13:18 +02:00
'updated_at' => 'datetime',
2025-06-29 09:37:49 +02:00
];
}
2025-06-29 09:48:45 +02:00
2025-06-29 19:46:50 +02:00
public function isValid(): bool
{
if (is_null($this->validated_at)) {
return false;
}
if (is_null($this->is_valid)) {
return false;
}
return $this->is_valid;
}
2025-06-30 18:18:30 +02:00
public function articlePublication(): HasOne
{
return $this->hasOne(ArticlePublication::class);
}
public function articlePublications(): HasMany
{
return $this->hasMany(ArticlePublication::class);
}
2025-06-29 19:46:50 +02:00
protected static function booted(): void
2025-06-29 09:48:45 +02:00
{
static::created(function ($article) {
event(new ArticleFetched($article));
});
}
2025-06-29 09:37:49 +02:00
}