diff options
| author | ilotterytea <iltsu@alright.party> | 2025-05-16 01:04:52 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-05-16 01:15:12 +0500 |
| commit | 0fac4eabc661e4425cf21d461c0e18b542fa8003 (patch) | |
| tree | e4737cd1a1e311a47d59fbff569ff696e33cd46e /src/user.php | |
| parent | 98e7d4bcb8fdf99d97f95be2fb0cc18a0543808b (diff) | |
feat: some code refactoring
Diffstat (limited to 'src/user.php')
| -rw-r--r-- | src/user.php | 106 |
1 files changed, 86 insertions, 20 deletions
diff --git a/src/user.php b/src/user.php index e9fec0b..d22eeb4 100644 --- a/src/user.php +++ b/src/user.php @@ -1,36 +1,102 @@ <?php -class User +class Badge { - private string $id; - private string $username; - private int $joined_at; - private int $last_active_at; + public string $id; - function __construct($row) + public static function from_array(array $arr, string $prefix = ""): Badge|null { - $this->id = $row["id"]; - $this->username = $row["username"]; - $this->joined_at = strtotime($row["joined_at"]); - $this->last_active_at = strtotime($row["last_active_at"]); - } + if (!empty($prefix)) { + $prefix .= "_"; + } + if (!isset($arr["{$prefix}badge_id"])) { + return null; + } - function id() - { - return $this->id; + $b = new Badge(); + $b->id = $arr["{$prefix}badge_id"]; + + return $b; } +} + +class Role +{ + public string $name; + public Badge|null $badge; - function username() + public static function from_array(array $arr): Role|null { - return $this->username; + if (!isset($arr["role_name"])) { + return null; + } + + $r = new Role(); + + $r->name = $arr["role_name"]; + $r->badge = Badge::from_array($arr, "role"); + + return $r; } +} + +class User +{ + public string $id; + public string $username; + public int $joined_at; + public int $last_active_at; - function joined_at() + public Badge|null $custom_badge; + + public Role|null $role; + + public bool $private_profile; + + public static function from_array(array $arr): User { - return $this->joined_at; + $u = new User(); + + $u->id = $arr["id"]; + $u->username = $arr["username"]; + $u->joined_at = strtotime($arr["joined_at"] ?? "0"); + $u->last_active_at = strtotime($arr["last_active_at"] ?? "0"); + + $u->private_profile = $row["private_profile"] ?? false; + + $u->custom_badge = Badge::from_array($arr, "custom"); + + $u->role = Role::from_array($arr); + + return $u; } - function last_active_at() + public static function get_user_by_id(PDO &$db, string $user_id): User|null { - return $this->last_active_at; + $stmt = $db->prepare("SELECT + u.id, + u.username, + u.joined_at, + u.last_active_at, + + up.private_profile, + r.name AS role_name, + r.badge_id AS role_badge_id, + ub.badge_id AS custom_badge_id + FROM users u + INNER JOIN user_preferences up ON up.id = u.id + LEFT JOIN role_assigns ra ON ra.user_id = u.id + LEFT JOIN roles r ON r.id = ra.role_id + LEFT JOIN user_badges ub ON ub.user_id = u.id + WHERE u.id = ? + "); + $stmt->execute([$user_id]); + + $u = null; + + if ($uploader_row = $stmt->fetch()) { + $u = User::from_array($uploader_row); + } + + return $u; } }
\ No newline at end of file |
