MAPG-115 add base model and User model
This commit is contained in:
parent
fe814908c7
commit
00dc78b50c
49
src/Model/BaseModel.php
Normal file
49
src/Model/BaseModel.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php namespace MapGuesser\Model;
|
||||
|
||||
abstract class BaseModel
|
||||
{
|
||||
protected static array $fields;
|
||||
|
||||
protected $id = null;
|
||||
|
||||
public static function getFields(): array
|
||||
{
|
||||
return array_merge(['id'], static::$fields);
|
||||
}
|
||||
|
||||
public function __construct(array $data)
|
||||
{
|
||||
foreach ($data as $key => $value) {
|
||||
$method = 'set' . str_replace('_', '', ucwords($key, '_'));
|
||||
|
||||
if (method_exists($this, $method)) {
|
||||
$this->$method($value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function setId($id): void
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
function toArray(): array
|
||||
{
|
||||
$array = [];
|
||||
|
||||
foreach (self::getFields() as $key) {
|
||||
$method = 'get' . str_replace('_', '', ucwords($key, '_'));
|
||||
|
||||
if (method_exists($this, $method)) {
|
||||
$array[$key] = $this->$method();
|
||||
}
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
}
|
70
src/Model/User.php
Normal file
70
src/Model/User.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?php namespace MapGuesser\Model;
|
||||
|
||||
use MapGuesser\Interfaces\Authentication\IUser;
|
||||
|
||||
class User extends BaseModel implements IUser
|
||||
{
|
||||
private static array $types = ['user', 'admin'];
|
||||
|
||||
protected static array $fields = ['email', 'password', 'type'];
|
||||
|
||||
private string $email;
|
||||
|
||||
private string $password;
|
||||
|
||||
private string $type = 'user';
|
||||
|
||||
public function setEmail(string $email): void
|
||||
{
|
||||
$this->email = $email;
|
||||
}
|
||||
|
||||
public function setPassword(string $hashedPassword): void
|
||||
{
|
||||
$this->password = $hashedPassword;
|
||||
}
|
||||
|
||||
public function setPlainPassword(string $plainPassword): void
|
||||
{
|
||||
$this->password = password_hash($plainPassword, PASSWORD_BCRYPT);
|
||||
}
|
||||
|
||||
public function setType(string $type): void
|
||||
{
|
||||
if (in_array($type, self::$types)) {
|
||||
$this->type = $type;
|
||||
}
|
||||
}
|
||||
|
||||
public function getEmail(): string
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
public function getPassword(): string
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
public function getType(): string
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
public function hasPermission(int $permission): bool
|
||||
{
|
||||
switch ($permission) {
|
||||
case IUser::PERMISSION_NORMAL:
|
||||
return true;
|
||||
break;
|
||||
case IUser::PERMISSION_ADMIN:
|
||||
return $this->type === 'admin';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public function checkPassword(string $password): bool
|
||||
{
|
||||
return password_verify($password, $this->password);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user