misuzu/src/News/NewsPostInfo.php
2025-02-16 00:19:42 +00:00

74 lines
2.2 KiB
PHP

<?php
namespace Misuzu\News;
use Carbon\CarbonImmutable;
use Index\Db\DbResult;
class NewsPostInfo {
public function __construct(
public private(set) string $id,
public private(set) string $categoryId,
public private(set) ?string $userId,
public private(set) bool $featured,
public private(set) string $title,
public private(set) string $body,
public private(set) int $scheduledTime,
public private(set) int $createdTime,
public private(set) int $updatedTime,
public private(set) ?int $deletedTime,
) {}
public static function fromResult(DbResult $result): NewsPostInfo {
return new NewsPostInfo(
id: $result->getString(0),
categoryId: $result->getString(1),
userId: $result->getStringOrNull(2),
featured: $result->getBoolean(3),
title: $result->getString(4),
body: $result->getString(5),
scheduledTime: $result->getInteger(6),
createdTime: $result->getInteger(7),
updatedTime: $result->getInteger(8),
deletedTime: $result->getIntegerOrNull(9),
);
}
public string $commentsCategoryName {
get => sprintf('news-%s', $this->id);
}
public string $firstParagraph {
get {
$index = mb_strpos($this->body, "\n");
return $index === false ? $this->body : mb_substr($this->body, 0, $index);
}
}
public CarbonImmutable $scheduledAt {
get => CarbonImmutable::createFromTimestampUTC($this->scheduledTime);
}
public bool $published {
get => $this->scheduledTime <= time();
}
public CarbonImmutable $createdAt {
get => CarbonImmutable::createFromTimestampUTC($this->createdTime);
}
public CarbonImmutable $updatedAt {
get => CarbonImmutable::createFromTimestampUTC($this->updatedTime);
}
public bool $edited {
get => $this->updatedTime > $this->createdTime;
}
public bool $deleted {
get => $this->deletedTime !== null;
}
public ?CarbonImmutable $deletedAt {
get => $this->deletedTime === null ? null : CarbonImmutable::createFromTimestampUTC($this->deletedTime);
}
}