From c4cd40eb1103514b1f581ad2de9cba61af3ff3a5 Mon Sep 17 00:00:00 2001 From: flashwave Date: Sun, 29 Sep 2019 03:46:48 +0200 Subject: [PATCH] // can't be bothered to come up with a proper comment --- misuzu.php | 1 + src/Database/DatabaseStatement.php | 22 +++-------- src/Users/object.php | 62 ++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 17 deletions(-) create mode 100644 src/Users/object.php diff --git a/misuzu.php b/misuzu.php index 4f14716d..fe293743 100644 --- a/misuzu.php +++ b/misuzu.php @@ -71,6 +71,7 @@ require_once 'src/Users/auth.php'; require_once 'src/Users/avatar.php'; require_once 'src/Users/background.php'; require_once 'src/Users/login_attempt.php'; +require_once 'src/Users/object.php'; require_once 'src/Users/profile.php'; require_once 'src/Users/recovery.php'; require_once 'src/Users/relations.php'; diff --git a/src/Database/DatabaseStatement.php b/src/Database/DatabaseStatement.php index d446d822..90dac735 100644 --- a/src/Database/DatabaseStatement.php +++ b/src/Database/DatabaseStatement.php @@ -8,7 +8,6 @@ class DatabaseStatement { public $pdo; public $stmt; private $isQuery; - private $hasExecuted = false; public function __construct(PDOStatement $stmt, PDO $pdo, bool $isQuery) { $this->stmt = $stmt; @@ -22,26 +21,13 @@ class DatabaseStatement { } public function execute(array $params = []): bool { - if($this->hasExecuted) - return true; - $this->hasExecuted = true; - return count($params) ? $this->stmt->execute($params) : $this->stmt->execute(); } public function executeGetId(array $params = []): int { - if($this->hasExecuted) - return true; - $this->hasExecuted = true; - return $this->execute($params) ? $this->pdo->lastInsertId() : 0; } - public function reset(): DatabaseStatement { - $this->hasExecuted = false; - return $this; - } - public function fetch($default = []) { $out = $this->isQuery || $this->execute() ? $this->stmt->fetch(PDO::FETCH_ASSOC) : false; return $out ? $out : $default; @@ -61,7 +47,7 @@ class DatabaseStatement { $out = false; if($this->isQuery || $this->execute()) { - $out = $args === null ? $this->fetchObject($className) : $this->fetchObject($className, $args); + $out = $args === null ? $this->stmt->fetchObject($className) : $this->stmt->fetchObject($className, $args); } return $out !== false ? $out : $default; @@ -70,8 +56,10 @@ class DatabaseStatement { public function fetchObjects(string $className = 'stdClass', ?array $args = null): array { $objects = []; - while(($object = $this->fetchObject($className, $args, false)) !== false) { - $objects[] = $object; + if($this->isQuery || $this->execute()) { + while(($object = $this->stmt->fetchObject($className, $args)) !== false) { + $objects[] = $object; + } } return $objects; diff --git a/src/Users/object.php b/src/Users/object.php new file mode 100644 index 00000000..1e1bf1c9 --- /dev/null +++ b/src/Users/object.php @@ -0,0 +1,62 @@ +bind('username', $username)->bind('email', $email) + ->bind('register_ip', $ipAddress)->bind('last_ip', $ipAddress) + ->bind('password', user_password_hash($password)) + ->bind('user_country', ip_country_code($ipAddress)) + ->executeGetId(); + + if($createUser < 1) + return null; + + return static::get($createUser); + } + + public static function get(int $userId): ?User { + return DB::prepare(self::USER_SELECT . 'WHERE `user_id` = :user_id') + ->bind('user_id', $userId) + ->fetchObject(User::class); + } +}