diff options
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); + } +} |