misuzu/src/News/NewsPostInfo.php

127 lines
3.3 KiB
PHP
Raw Normal View History

2023-07-15 17:02:46 +00:00
<?php
namespace Misuzu\News;
use Carbon\CarbonImmutable;
2023-07-15 17:02:46 +00:00
use Index\Data\IDbResult;
class NewsPostInfo {
2024-02-07 00:04:45 +00:00
public function __construct(
private string $id,
private string $categoryId,
private ?string $userId,
private ?string $commentsSectionId,
private bool $featured,
private string $title,
private string $body,
private int $scheduled,
private int $created,
private int $updated,
private ?int $deleted,
) {}
2023-07-15 17:02:46 +00:00
2024-02-07 00:04:45 +00:00
public static function fromResult(IDbResult $result): NewsPostInfo {
return new NewsPostInfo(
id: $result->getString(0),
categoryId: $result->getString(1),
userId: $result->getStringOrNull(2),
commentsSectionId: $result->getStringOrNull(3),
featured: $result->getBoolean(4),
title: $result->getString(5),
body: $result->getString(6),
scheduled: $result->getInteger(7),
created: $result->getInteger(8),
updated: $result->getInteger(9),
deleted: $result->getIntegerOrNull(10),
);
2023-07-15 17:02:46 +00:00
}
public function getId(): string {
return $this->id;
}
public function getCategoryId(): string {
return $this->categoryId;
}
public function hasUserId(): bool {
return $this->userId !== null;
}
public function getUserId(): ?string {
return $this->userId;
}
public function hasCommentsCategoryId(): bool {
return $this->commentsSectionId !== null;
}
public function getCommentsCategoryId(): string {
return $this->commentsSectionId;
}
public function getCommentsCategoryName(): string {
return sprintf('news-%s', $this->id);
}
public function isFeatured(): bool {
return $this->featured;
}
public function getTitle(): string {
return $this->title;
}
public function getBody(): string {
return $this->body;
}
public function getFirstParagraph(): string {
$index = mb_strpos($this->body, "\n");
return $index === false ? $this->body : mb_substr($this->body, 0, $index);
}
public function getScheduledTime(): int {
return $this->scheduled;
}
public function getScheduledAt(): CarbonImmutable {
return CarbonImmutable::createFromTimestampUTC($this->scheduled);
2023-07-15 17:02:46 +00:00
}
public function isPublished(): bool {
return $this->scheduled <= time();
}
public function getCreatedTime(): int {
return $this->created;
}
public function getCreatedAt(): CarbonImmutable {
return CarbonImmutable::createFromTimestampUTC($this->created);
2023-07-15 17:02:46 +00:00
}
public function getUpdatedTime(): int {
return $this->updated;
}
public function getUpdatedAt(): CarbonImmutable {
return CarbonImmutable::createFromTimestampUTC($this->updated);
2023-07-15 17:02:46 +00:00
}
public function isEdited(): bool {
return $this->updated > $this->created;
}
public function isDeleted(): bool {
return $this->deleted !== null;
}
public function getDeletedTime(): ?int {
return $this->deleted;
}
public function getDeletedAt(): ?CarbonImmutable {
return $this->deleted === null ? null : CarbonImmutable::createFromTimestampUTC($this->deleted);
2023-07-15 17:02:46 +00:00
}
}