PDO::PARAM_INT, 'loginName' => PDO::PARAM_STR, 'name' => PDO::PARAM_STR, 'vorname' => PDO::PARAM_STR, 'gebDatum' => PDO::PARAM_STR, 'eMail' => PDO::PARAM_STR, 'pwHash' => PDO::PARAM_STR, 'config' => PDO::PARAM_STR ]; /** Constructor * @todo Document parameter * @todo Input sanitation */ public function __construct($id, $loginName, $name, $firstName, $dateOfBirth, $eMail, $config, $pwHash) { $this->id = (int) $id; $this->loginName = $loginName; $this->name = $name; $this->firstName = $firstName; $this->dateOfBirth = $dateOfBirth != null ? DateTime::createFromFormat('Y-m-d', $dateOfBirth) : null; $this->eMail = $eMail; $this->config = $config; $this->pwHash = $pwHash; } /** Export the User data into an associative array * * @return array associative array representing the user */ public function toAssoc() { return [ 'id' => $this->id, 'loginName' => $this->loginName, 'name' => $this->name, 'vorname' => $this->firstName, 'gebDatum' => $this->dateOfBirth, 'eMail' => $this->eMail, 'config' => $this->config, 'pwHash' => $this->pwHash ]; } /** verify the users password * * @param [string] $password the password to verify * @return true if password is verified, false otherwise */ public function verifyPassword($password) { return password_verify($password, $this->pwHash); } // getter functions public function getId() { return $this->id; } public function getLoginName() { return $this->loginName; } public function getName() { return $this->name; } public function getFirstname() { return $this->firstName; } public function getConfig() { return $this->$config; } // static functions /** Create a User from an assoziative array like it is returned from db requests * * @param array $member associative array with the UserData from the dbRequest * @param $columnMappings renaming of columnNames, e.g., if the id isn't under id in the array, add 'id'=>'userId' to the mappings * @return User initialized user */ public static function fromDbArray($member, $columnMappings = []) { // if it isn't remapped, take default column name foreach (self::$dbColumns as $columnName => $columnDataType) { if (!array_key_exists($columnName, $columnMappings)) { $columnMappings[$columnName] = $columnName; } } return new User( $member[$columnMappings['id']] ?? null, $member[$columnMappings['loginName']] ?? null, $member[$columnMappings['name']] ?? null, $member[$columnMappings['vorname']] ?? null, $member[$columnMappings['gebDatum']] ?? null, array_key_exists($columnMappings['eMail'], $member) ? explode(',', $member['eMail']) : null, array_key_exists($columnMappings['config'], $member) ? json_decode($member['config']) : null, array_key_exists($columnMappings['pwHash'], $member) ? $member['pwHash'] : null ); } /** Load an User from the db via an id * * @param int $userId * @return loaded user or null (if sth. wrong) */ public static function loadFromDb($userId) { return self::loadFromDbBy('id', $userId); } /** Load an User from the db via the loginName * * @param int $login * @return loaded user or null (if sth. wrong) */ public static function loadFromDbByLoginName($login) { return self::loadFromDbBy('loginName', $login); } /** Load a user from the db by a column * * @param [string] $name name of the column * @param [mixed] $value value to look for * @param [bool] $unique if the value is unique (true->return single value) or not (false->return array) * @return loaded user or null (if sth. wrong) */ public static function loadFromDbBy($name, $value) { if (!array_key_exists($name, self::$dbColumns)) { return null; } $response = dbConnector::query( 'SELECT * FROM `wkParticipo_Users` WHERE `' . $name . '` = :' . $name, [$name => ['value' => $value, 'data_type' => self::$dbColumns[$name]]] ); if (count($response) != 1) { return null; } return User::fromDbArray($response[0]); } }