misuzu/src/Users/BanInfo.php

127 lines
3.3 KiB
PHP

<?php
namespace Misuzu\Users;
use Index\DateTime;
use Index\Data\IDbResult;
class BanInfo {
public function __construct(
private string $id,
private string $userId,
private ?string $modId,
private int $severity,
private string $publicReason,
private string $privateReason,
private int $created,
private ?int $expires,
) {}
public static function fromResult(IDbResult $result): BanInfo {
return new BanInfo(
id: $result->getString(0),
userId: $result->getString(1),
modId: $result->getStringOrNull(2),
severity: $result->getInteger(3),
publicReason: $result->getString(4),
privateReason: $result->getString(5),
created: $result->getInteger(6),
expires: $result->getIntegerOrNull(8),
);
}
public function getId(): string {
return $this->id;
}
public function getUserId(): string {
return $this->userId;
}
public function hasModId(): bool {
return $this->modId !== null;
}
public function getModId(): ?string {
return $this->modId;
}
public function getSeverity(): int {
return $this->severity;
}
public function hasPublicReason(): bool {
return $this->publicReason !== '';
}
public function getPublicReason(): string {
return $this->publicReason;
}
public function hasPrivateReason(): bool {
return $this->privateReason !== '';
}
public function getPrivateReason(): string {
return $this->privateReason;
}
public function getCreatedTime(): int {
return $this->created;
}
public function getCreatedAt(): DateTime {
return DateTime::fromUnixTimeSeconds($this->created);
}
public function isPermanent(): bool {
return $this->expires === null;
}
public function getExpiresTime(): ?int {
return $this->expires;
}
public function getExpiresAt(): ?DateTime {
return $this->expires === null ? null : DateTime::fromUnixTimeSeconds($this->expires);
}
public function isActive(): bool {
return $this->expires === null || $this->expires > time();
}
public function isExpired(): bool {
return $this->expires !== null && $this->expires <= time();
}
private const DURATION_DIVS = [
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second',
];
private static function getTimeString(?int $left, int $right): string {
if($left === null)
return 'permanent';
$duration = $left - $right;
foreach(self::DURATION_DIVS as $span => $name) {
$display = floor($duration / $span);
if($display > 0)
return number_format($display) . ' ' . $name . ($display == 1 ? '' : 's');
}
return 'an amount of time';
}
public function getDurationString(): string {
return self::getTimeString($this->expires, $this->created);
}
public function getRemainingString(): string {
return self::getTimeString($this->expires, time());
}
}