Spaces:
Running
Running
File size: 1,607 Bytes
907b200 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | <?php
namespace App\Models;
use Carbon\Carbon;
class NewsArticle
{
public int $id;
public string $title;
public string $summary;
public string $source;
public string $url;
public Carbon $publishedAt;
public bool $isSaved;
public function __construct(array $data)
{
$this->id = $data['id'] ?? 0;
$this->title = $data['title'];
$this->summary = $data['summary'];
$this->source = $data['source'];
$this->url = $data['url'];
$this->publishedAt = $data['published_at'] instanceof Carbon
? $data['published_at']
: Carbon::parse($data['published_at']);
$this->isSaved = $data['is_saved'] ?? false;
}
public static function fromModel(News $news, bool $isSaved = false): self
{
return new self([
'id' => $news->id,
'title' => $news->title,
'summary' => $news->summary,
'source' => $news->source,
'url' => $news->url,
'published_at' => $news->published_at,
'is_saved' => $isSaved,
]);
}
public function withSaved(bool $isSaved): self
{
$this->isSaved = $isSaved;
return $this;
}
public function toArray(): array
{
return [
'id' => $this->id,
'title' => $this->title,
'summary' => $this->summary,
'source' => $this->source,
'url' => $this->url,
'published_at' => $this->publishedAt->toIso8601String(),
'is_saved' => $this->isSaved,
];
}
}
|