2020-05-08 22:53:21 +00:00
|
|
|
<?php
|
|
|
|
namespace EEPROM;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use JsonSerializable;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct() {
|
|
|
|
if($this === self::$active)
|
|
|
|
self::$active = null;
|
|
|
|
}
|
|
|
|
public function setActive(): self {
|
|
|
|
self::$active = $this;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getId(): int {
|
|
|
|
return $this->user_id ?? 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSizeMultiplier(): int {
|
|
|
|
return $this->user_size_multiplier ?? 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCreated(): int {
|
|
|
|
return $this->user_created ?? 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRestricted(): int {
|
|
|
|
return $this->user_restricted ?? 0;
|
|
|
|
}
|
|
|
|
public function isRestricted(): bool {
|
|
|
|
return $this->getRestricted() > 0;
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:02:49 +00:00
|
|
|
public function jsonSerialize(): mixed {
|
2020-05-08 22:53:21 +00:00
|
|
|
return [
|
|
|
|
'id' => $this->getId(),
|
|
|
|
'size_multi' => $this->getSizeMultiplier(),
|
|
|
|
'created' => date('c', $this->getCreated()),
|
|
|
|
'restricted' => $this->getRestricted() < 1 ? null : date('c', $this->getRestricted()),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function byId(int $userId): self {
|
|
|
|
if($userId < 1)
|
|
|
|
throw new UserNotFoundException;
|
|
|
|
|
|
|
|
$createUser = DB::prepare('INSERT IGNORE INTO `prm_users` (`user_id`) VALUES (:id)');
|
|
|
|
$createUser->bindValue('id', $userId);
|
|
|
|
$createUser->execute();
|
|
|
|
|
|
|
|
$getUser = DB::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` = :user
|
|
|
|
');
|
|
|
|
$getUser->bindValue('user', $userId);
|
|
|
|
$getUser->execute();
|
|
|
|
$user = $getUser->fetchObject(self::class);
|
|
|
|
|
|
|
|
if($user === false)
|
|
|
|
throw new UserNotFoundException;
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function all(int $limit = 0, int $after = 0): array {
|
|
|
|
$query = '
|
|
|
|
SELECT `user_id`, `user_size_multiplier`,
|
|
|
|
UNIX_TIMESTAMP(`user_created`) AS `user_created`,
|
|
|
|
UNIX_TIMESTAMP(`user_restricted`) AS `user_restricted`
|
|
|
|
FROM `prm_users`
|
|
|
|
';
|
|
|
|
|
|
|
|
if($after > 0)
|
|
|
|
$query .= sprintf(' WHERE `user_id` > %d', $after);
|
|
|
|
|
|
|
|
$query .= ' ORDER BY `user_id`';
|
|
|
|
|
|
|
|
if($limit > 0)
|
|
|
|
$query .= sprintf(' LIMIT %d', $limit);
|
|
|
|
|
|
|
|
$getUsers = DB::prepare($query);
|
|
|
|
$getUsers->execute();
|
|
|
|
$out = [];
|
|
|
|
|
|
|
|
while($user = $getUsers->fetchObject(self::class))
|
|
|
|
$out[] = $user;
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
}
|