misuzu/src/Comments/CommentsPostInfo.php

88 lines
2.9 KiB
PHP

<?php
namespace Misuzu\Comments;
use Carbon\CarbonImmutable;
use Index\Db\DbResult;
class CommentsPostInfo {
public function __construct(
public private(set) string $id,
public private(set) string $categoryId,
public private(set) ?string $userId,
public private(set) ?string $replyingTo,
public private(set) string $body,
public private(set) int $createdTime,
public private(set) ?int $pinnedTime,
public private(set) ?int $updatedTime,
public private(set) ?int $deletedTime,
public private(set) int $repliesCount,
public private(set) int $votesCount,
public private(set) int $votesPositive,
public private(set) int $votesNegative,
) {}
public static function fromResult(
DbResult $result,
bool $includeRepliesCount = false,
bool $includeVotesCount = false
): CommentsPostInfo {
$args = [];
$count = 0;
$args[] = $result->getString($count); // id
$args[] = $result->getString(++$count); // categoryId
$args[] = $result->getStringOrNull(++$count); // userId
$args[] = $result->getStringOrNull(++$count); // replyingTo
$args[] = $result->getString(++$count); // body
$args[] = $result->getInteger(++$count); // createdTime
$args[] = $result->getIntegerOrNull(++$count); // pinnedTime
$args[] = $result->getIntegerOrNull(++$count); // updatedTime
$args[] = $result->getIntegerOrNull(++$count); // deletedTime
$args[] = $includeRepliesCount ? $result->getInteger(++$count) : 0;
if($includeVotesCount) {
$args[] = $result->getInteger(++$count); // votesCount
$args[] = $result->getInteger(++$count); // votesPositive
$args[] = $result->getInteger(++$count); // votesNegative
} else {
$args[] = 0;
$args[] = 0;
$args[] = 0;
}
return new CommentsPostInfo(...$args);
}
public bool $isReply {
get => $this->replyingTo !== null;
}
public CarbonImmutable $createdAt {
get => CarbonImmutable::createFromTimestampUTC($this->createdTime);
}
public ?CarbonImmutable $pinnedAt {
get => $this->pinnedTime === null ? null : CarbonImmutable::createFromTimestampUTC($this->pinnedTime);
}
public bool $pinned {
get => $this->pinnedTime !== null;
}
public ?CarbonImmutable $updatedAt {
get => $this->updatedTime === null ? null : CarbonImmutable::createFromTimestampUTC($this->updatedTime);
}
public bool $edited {
get => $this->updatedTime !== null;
}
public ?CarbonImmutable $deletedAt {
get => $this->deletedTime === null ? null : CarbonImmutable::createFromTimestampUTC($this->deletedTime);
}
public bool $deleted {
get => $this->deletedTime !== null;
}
}