misuzu/src/News/NewsPostInfo.php

126 lines
3.3 KiB
PHP

<?php
namespace Misuzu\News;
use Carbon\CarbonImmutable;
use Index\Data\IDbResult;
class NewsPostInfo {
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,
) {}
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),
);
}
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);
}
public function isPublished(): bool {
return $this->scheduled <= time();
}
public function getCreatedTime(): int {
return $this->created;
}
public function getCreatedAt(): CarbonImmutable {
return CarbonImmutable::createFromTimestampUTC($this->created);
}
public function getUpdatedTime(): int {
return $this->updated;
}
public function getUpdatedAt(): CarbonImmutable {
return CarbonImmutable::createFromTimestampUTC($this->updated);
}
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);
}
}