2
0
Fork 0
forked from flashii/eeprom
eeprom-nabucco/src/User.php

92 lines
2.4 KiB
PHP
Raw Normal View History

2020-05-08 22:53:21 +00:00
<?php
namespace EEPROM;
use Exception;
use JsonSerializable;
2022-07-06 16:58:40 +00:00
use Index\Data\IDbConnection;
use Index\Data\DbType;
2020-05-08 22:53:21 +00:00
class UserNotFoundException extends Exception {}
class User implements JsonSerializable {
private static $active;
public static function hasActive(): bool {
return !empty(self::$active);
}
public static function active(): self {
return self::$active;
}
2022-07-06 16:58:40 +00:00
public function __construct(
private $id = 0,
private $sizeMultiplier = 0,
private $created = 0,
private $restricted = 0,
) {}
2020-05-08 22:53:21 +00:00
public function __destruct() {
if($this === self::$active)
self::$active = null;
}
public function setActive(): self {
self::$active = $this;
return $this;
}
public function getId(): int {
2022-07-06 16:58:40 +00:00
return $this->id;
2020-05-08 22:53:21 +00:00
}
public function getSizeMultiplier(): int {
2022-07-06 16:58:40 +00:00
return $this->sizeMultiplier;
2020-05-08 22:53:21 +00:00
}
public function getCreated(): int {
2022-07-06 16:58:40 +00:00
return $this->created;
2020-05-08 22:53:21 +00:00
}
public function getRestricted(): int {
2022-07-06 16:58:40 +00:00
return $this->restricted;
2020-05-08 22:53:21 +00:00
}
public function isRestricted(): bool {
2022-07-06 16:58:40 +00:00
return $this->restricted > 0;
2020-05-08 22:53:21 +00:00
}
public function jsonSerialize(): mixed {
2020-05-08 22:53:21 +00:00
return [
2022-07-06 16:58:40 +00:00
'id' => $this->id,
'size_multi' => $this->sizeMultiplier,
'created' => date('c', $this->created),
'restricted' => $this->restricted < 1 ? null : date('c', $this->restricted),
2020-05-08 22:53:21 +00:00
];
}
2022-07-06 16:58:40 +00:00
public static function byId(IDbConnection $conn, int $userId): self {
2020-05-08 22:53:21 +00:00
if($userId < 1)
throw new UserNotFoundException;
2022-07-06 16:58:40 +00:00
$create = $conn->prepare('INSERT IGNORE INTO `prm_users` (`user_id`) VALUES (?)');
$create->addParameter(1, $userId, DbType::INTEGER);
$create->execute();
2020-05-08 22:53:21 +00:00
2022-07-06 16:58:40 +00:00
$get = $conn->prepare(
'SELECT `user_id`, `user_size_multiplier`, UNIX_TIMESTAMP(`user_created`) AS `user_created`,'
. ' UNIX_TIMESTAMP(`user_restricted`) AS `user_restricted` FROM `prm_users` WHERE `user_id` = ?'
);
$get->addParameter(1, $userId, DbType::INTEGER);
$get->execute();
$result = $get->getResult();
2020-05-08 22:53:21 +00:00
2022-07-06 16:58:40 +00:00
if(!$result->next())
throw new UserNotFoundException;
2020-05-08 22:53:21 +00:00
2022-07-06 16:58:40 +00:00
return new static(
$result->getInteger(0),
$result->getInteger(1),
$result->getInteger(2),
$result->getInteger(3),
);
2020-05-08 22:53:21 +00:00
}
}