battles/classes/Battles/UserProfile.php
Ivor Barhansky 0398425205 Code smell.
2022-12-17 03:03:56 +02:00

164 lines
3.5 KiB
PHP

<?php
# Date: 16.02.2022 (21:22)
namespace Battles;
use Battles\Database\Db;
class UserProfile
{
private int $id;
private string $pass;
private string $email;
private string $realname;
private string $borndate;
private string $info;
private string $ip;
private const INFO_CHAR_LIMIT = 1500;
private string $status = '';
/**
* @param int $id
* @param string $pass
* @param string $email
* @param string $realname
* @param string $borndate
* @param string $info
* @param string $ip
*/
public function __construct(
int $id,
string $pass,
string $email,
string $realname,
string $borndate,
string $info,
string $ip
)
{
$this->id = $id;
$this->pass = $pass;
$this->email = $email;
$this->realname = $realname;
$this->borndate = $borndate;
$this->info = $info;
$this->ip = $ip;
}
public function changePassword($oldPassword, $newPassword)
{
if (password_verify($oldPassword, $this->pass)) {
$this->pass = password_hash($newPassword, PASSWORD_DEFAULT);
} else {
$this->status .= 'Неверный пароль!';
}
}
/**
* @return string
*/
public function getEmail(): string
{
return $this->email;
}
/**
* @param string $email
*/
public function setEmail(string $email): void
{
$this->email = $email;
}
/**
* @return string
*/
public function getRealname(): string
{
return $this->realname;
}
/**
* @param string $realname
*/
public function setRealname(string $realname): void
{
$this->realname = htmlspecialchars($realname);
}
/**
* @return string
*/
public function getBorndate(): string
{
return $this->borndate;
}
/**
* @param string $borndate
*/
public function setBorndate(string $borndate): void
{
$this->borndate = $borndate;
}
/**
* @return string
*/
public function getInfo(): string
{
return $this->info;
}
/**
* @param string $info
*/
public function setInfo(string $info): void
{
if (strlen($info) > self::INFO_CHAR_LIMIT) {
$this->status .= 'Максимальная длинна поля Хобби: ' . self::INFO_CHAR_LIMIT . ' символов!';
} else {
$info = htmlspecialchars($info);
$info = str_replace("\\n", '<br />', $info);
$info = str_replace("\\r", '', $info);
$info = str_replace('&lt;br&nbsp;/&gt;', '<br />', $info);
$this->info = $info;
}
}
/**
* @return string
*/
public function getIp(): string
{
return $this->ip;
}
/** Сохраняет в базу актуальные имя, пароль, email, дату рождения, текст инфы.
*
*/
public function save(): string
{
if ($this->status) {
return $this->status;
}
$query = 'update users set pass = ?, email = ?, realname = ?, borndate = ?, info = ? where id = ?';
$vals = [
//set
$this->pass,
$this->email,
$this->realname,
$this->borndate,
$this->info,
// where
$this->id
];
Db::getInstance()->execute($query, $vals);
return 'Успешно!';
}
}