/** * @param UserDataField $field * @param User $user * @param mixed $value * @return UserData * @throws \InvalidArgumentException */ public static function create(UserDataField $field, User $user, $value) { // check if field is already set for user if ($user->getData($field->getName()) !== null) { throw new \InvalidArgumentException('User\'s data field ' . $field->getName() . ' already set.'); } $value = serialize($value); Aelix::db()->prepare('INSERT INTO `user_data` SET `userID` = :userID, `fieldID` = :fieldID, `value` = :value')->execute([':userID' => $user->getID(), ':fieldID' => $field->getID(), ':value' => $value]); $dataID = Aelix::db()->getPDO()->lastInsertId('user_data'); return new UserData($user, $field, $dataID, $value); }
/** * User constructor. */ public function __construct() { parent::__construct(['id' => self::GUEST_USER_ID, 'username' => 'guest', 'fullname' => 'Guest', 'email' => '', 'passwordHash' => '']); }
/** * @param int $userID * @return User * @throws UserDoesntExistException */ public static function getByID($userID) { if (isset(self::$userByID[$userID]) && self::$userByID[$userID] instanceof User) { return self::$userByID[$userID]; } else { $stmt = Aelix::db()->prepare('SELECT * FROM `user` WHERE `id` = :userID')->execute([':userID' => $userID]); if ($stmt->rowCount() != 1) { throw new UserDoesntExistException(); } $user = new User($stmt->fetchArray()); self::$userByID[$user->getID()] = $user; return $user; } }
/** * @return User Object GuestUser if user doesn't exist */ public function getUser() { // TODO: guest user if ($this->get('core.user_id', false) === false) { return new GuestUser(); } try { return User::getByID($this->get('core.user_id')); } catch (UserDoesntExistException $e) { $this->forget('core.user_id'); return new GuestUser(); } }