flash.moe/src/SSHKeys/SSHKeyInfo.php
2024-12-16 19:15:50 +00:00

53 lines
1.6 KiB
PHP

<?php
namespace Makai\SSHKeys;
use Stringable;
use Carbon\CarbonImmutable;
use Index\Db\DbResult;
class SSHKeyInfo implements Stringable {
public function __construct(
public private(set) string $id,
public private(set) int $level,
public private(set) string $algo,
public private(set) string $body,
public private(set) string $comment,
public private(set) int $createdTime,
public private(set) ?int $deprecatedTime,
) {}
public static function fromResult(DbResult $result): SSHKeyInfo {
return new SSHKeyInfo(
id: $result->getString(0),
level: $result->getInteger(1),
algo: $result->getString(2),
body: $result->getString(3),
comment: $result->getString(4),
createdTime: $result->getInteger(5),
deprecatedTime: $result->getIntegerOrNull(6),
);
}
public CarbonImmutable $createdAt {
get => CarbonImmutable::createFromTimestampUTC($this->createdTime);
}
public bool $deprecated {
get => $this->deprecatedTime !== null;
}
public ?CarbonImmutable $deprecatedAt {
get => $this->deprecatedTime === null ? null : CarbonImmutable::createFromTimestampUTC($this->deprecatedTime);
}
public function toString(bool $includeComment): string {
$line = sprintf('ssh-%s %s', $this->algo, $this->body);
if($includeComment)
$line .= sprintf(' %s (%s)', (string)$this->comment, $this->createdAt->format('M Y'));
return $line;
}
public function __toString(): string {
return $this->toString(true);
}
}