flash.moe/src/SSHKeys/SSHKeyInfo.php

54 lines
1.6 KiB
PHP
Raw Normal View History

2022-02-06 01:36:05 +00:00
<?php
2023-10-12 22:09:04 +00:00
namespace Makai\SSHKeys;
2022-02-06 01:36:05 +00:00
2024-12-16 19:15:50 +00:00
use Stringable;
use Carbon\CarbonImmutable;
2024-10-21 20:50:01 +00:00
use Index\Db\DbResult;
2022-02-06 01:36:05 +00:00
2024-12-16 19:15:50 +00:00
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,
) {}
2022-02-06 01:36:05 +00:00
2024-12-16 19:15:50 +00:00
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),
);
2022-02-06 01:36:05 +00:00
}
2024-12-16 19:15:50 +00:00
public CarbonImmutable $createdAt {
get => CarbonImmutable::createFromTimestampUTC($this->createdTime);
2022-02-06 01:36:05 +00:00
}
2024-12-16 19:15:50 +00:00
public bool $deprecated {
get => $this->deprecatedTime !== null;
2022-02-06 01:36:05 +00:00
}
2024-12-16 19:15:50 +00:00
public ?CarbonImmutable $deprecatedAt {
get => $this->deprecatedTime === null ? null : CarbonImmutable::createFromTimestampUTC($this->deprecatedTime);
2023-10-12 19:03:00 +00:00
}
2022-02-06 01:36:05 +00:00
public function toString(bool $includeComment): string {
2024-12-16 19:15:50 +00:00
$line = sprintf('ssh-%s %s', $this->algo, $this->body);
2022-02-06 01:36:05 +00:00
if($includeComment)
2024-12-16 19:15:50 +00:00
$line .= sprintf(' %s (%s)', (string)$this->comment, $this->createdAt->format('M Y'));
2022-02-06 01:36:05 +00:00
return $line;
}
public function __toString(): string {
return $this->toString(true);
}
}