diff options
author | Daniel Weipert <git@mail.dweipert.de> | 2025-08-19 15:50:42 +0200 |
---|---|---|
committer | Daniel Weipert <git@mail.dweipert.de> | 2025-08-19 16:11:52 +0200 |
commit | d08f4c83470c25d35d24594bd73e4effdac191a0 (patch) | |
tree | 8320e4d0750776891fa5680ce5904de714128fce /src/Models/User.php | |
parent | a0ad1f5e7fac279b33ea09ca0e347cd7d02cd8ec (diff) |
database migrations and models for users, devices, tokens
Diffstat (limited to 'src/Models/User.php')
-rw-r--r-- | src/Models/User.php | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/Models/User.php b/src/Models/User.php new file mode 100644 index 0000000..5d198e7 --- /dev/null +++ b/src/Models/User.php @@ -0,0 +1,88 @@ +<?php + +namespace App\Models; + +use App\Database; +use App\Support\ConnectsToDatabase; + +class User implements ConnectsToDatabase +{ + public function __construct(private string $id) + {} + + public static function fromDatabase(array $row): self + { + return new self( + $row["id"], + ); + } + + public static function fetch(string $id = ""): ?self + { + if (empty($id)) { + throw new \InvalidArgumentException("missing user id"); + } + + return Database::getInstance()->query( + <<<SQL + select * from users where id=:id + SQL, + [ + "id" => $id, + ] + ); + } + + public static function fetchAll(): array + {} + + public static function fetchWithPassword(string $id, string $password): ?self + { + $row = Database::getInstance()->query("select * from users where id=:id and password=:password", [ + "id" => $id, + "password" => $password, + ])->fetch(); + + if (empty($row)) { + return null; + } + + return self::fromDatabase($row); + } + + public function insert(): bool + { + return Database::getInstance()->query( + <<<SQL + insert into users (id) + values (:id) + SQL, + [ + "id" => $this->id, + ] + ); + } + + public function update(): bool + {} + + public function delete(): bool + {} + + public function getId(): string + { + return $this->id; + } + + public function fetchDevice(string $id): ?Device + { + return Device::fetch($id, $this->id); + } + /** + * @return Device[] + */ + public function fetchDevices(): array + { + return Device::fetchAll($this->id); + } +} |