misuzu/src/Forum/ForumPostInfo.php

92 lines
2.8 KiB
PHP
Raw Normal View History

2023-08-28 01:17:34 +00:00
<?php
namespace Misuzu\Forum;
use Misuzu\Parsers\Parser;
use Carbon\CarbonImmutable;
use Index\XDateTime;
2024-10-05 02:40:29 +00:00
use Index\Db\DbResult;
2023-08-28 01:17:34 +00:00
class ForumPostInfo {
2024-02-07 00:04:45 +00:00
public function __construct(
public private(set) string $id,
public private(set) string $topicId,
public private(set) string $categoryId,
public private(set) ?string $userId,
public private(set) string $remoteAddress,
public private(set) string $body,
public private(set) int $parser,
public private(set) bool $shouldDisplaySignature,
public private(set) int $createdTime,
public private(set) ?int $editedTime,
public private(set) ?int $deletedTime,
2024-02-07 00:04:45 +00:00
) {}
2024-10-05 02:40:29 +00:00
public static function fromResult(DbResult $result): ForumPostInfo {
2024-02-07 00:04:45 +00:00
return new ForumPostInfo(
id: $result->getString(0),
topicId: $result->getString(1),
categoryId: $result->getString(2),
userId: $result->getStringOrNull(3),
remoteAddress: $result->getString(4),
2024-02-07 00:04:45 +00:00
body: $result->getString(5),
parser: $result->getInteger(6),
shouldDisplaySignature: $result->getBoolean(7),
createdTime: $result->getInteger(8),
editedTime: $result->getIntegerOrNull(9),
deletedTime: $result->getIntegerOrNull(10),
2024-02-07 00:04:45 +00:00
);
2023-08-28 01:17:34 +00:00
}
public bool $isBodyPlain {
get => $this->parser === Parser::PLAIN;
2023-08-28 01:17:34 +00:00
}
public bool $isBodyBBCode {
get => $this->parser === Parser::BBCODE;
2023-08-28 01:17:34 +00:00
}
public bool $isBodyMarkdown {
get => $this->parser === Parser::MARKDOWN;
2023-08-28 01:17:34 +00:00
}
public CarbonImmutable $createdAt {
get => CarbonImmutable::createFromTimestampUTC($this->createdTime);
2023-08-28 01:17:34 +00:00
}
private static ?CarbonImmutable $markAsEditedThreshold = null;
public bool $shouldMarkAsEdited {
get {
self::$markAsEditedThreshold ??= new CarbonImmutable('-5 minutes');
2023-08-28 01:17:34 +00:00
return XDateTime::compare($this->createdAt, self::$markAsEditedThreshold) < 0;
}
2023-08-28 01:17:34 +00:00
}
public bool $edited {
get => $this->editedTime !== null;
2023-08-28 01:17:34 +00:00
}
public ?CarbonImmutable $editedAt {
get => $this->editedTime === null ? null : CarbonImmutable::createFromTimestampUTC($this->editedTime);
2023-08-28 01:17:34 +00:00
}
private static ?CarbonImmutable $canBeDeletedThreshold = null;
public bool $canBeDeleted {
get {
self::$canBeDeletedThreshold ??= new CarbonImmutable('-1 week');
2023-08-28 01:17:34 +00:00
return XDateTime::compare($this->createdAt, self::$canBeDeletedThreshold) >= 0;
}
2023-08-28 01:17:34 +00:00
}
public bool $deleted {
get => $this->deletedTime !== null;
2023-08-28 01:17:34 +00:00
}
public ?CarbonImmutable $deletedAt {
get => $this->deletedTime === null ? null : CarbonImmutable::createFromTimestampUTC($this->deletedTime);
2023-08-28 01:17:34 +00:00
}
}