fedi-feed-router/app/Models/Article.php
2025-07-03 21:17:13 +02:00

80 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use App\Events\ArticleFetched;
use Database\Factories\ArticleFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Carbon;
/**
* @method static firstOrCreate(string[] $array)
* @property integer $id
* @property string $url
* @property bool|null $is_valid
* @property Carbon|null $validated_at
* @property Carbon $created_at
* @property Carbon $updated_at
* @property ArticlePublication $articlePublication
*/
class Article extends Model
{
/** @use HasFactory<ArticleFactory> */
use HasFactory;
protected $fillable = [
'url',
'title',
'description',
'is_valid',
'is_duplicate',
'fetched_at',
'validated_at',
];
public function casts(): array
{
return [
'is_valid' => 'boolean',
'is_duplicate' => 'boolean',
'fetched_at' => 'datetime',
'validated_at' => 'datetime',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
}
public function isValid(): bool
{
if (is_null($this->validated_at)) {
return false;
}
if (is_null($this->is_valid)) {
return false;
}
return $this->is_valid;
}
public function articlePublication(): HasOne
{
return $this->hasOne(ArticlePublication::class);
}
public function articlePublications(): HasMany
{
return $this->hasMany(ArticlePublication::class);
}
protected static function booted(): void
{
static::created(function ($article) {
echo "Article::created event fired for article ID {$article->id}\n";
event(new ArticleFetched($article));
});
}
}