0.1.0
This commit is contained in:
54
src/Mapper/User.php
Normal file
54
src/Mapper/User.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
namespace Incoviba\Mapper;
|
||||
|
||||
use Incoviba\Model\Auth\User as UserModel;
|
||||
use Incoviba\Model\Model;
|
||||
|
||||
class User extends Mapper
|
||||
{
|
||||
protected string $table = 'users';
|
||||
|
||||
protected function load(array|bool $row, bool $lazy = false): UserModel|bool
|
||||
{
|
||||
if (!$row) return false;
|
||||
$model = new UserModel();
|
||||
$model->id = $row['id'];
|
||||
$model->name = $row['name'];
|
||||
$model->setPassword($row['password']);
|
||||
$model->enabled = $row['enabled'] != 0;
|
||||
return $model;
|
||||
}
|
||||
public function save(Model $model): bool
|
||||
{
|
||||
if (isset($model->id)) {
|
||||
$qb = $this->connection->createQueryBuilder()
|
||||
->insert($this->table)
|
||||
->values([
|
||||
'name' => $model->name,
|
||||
'password' => $model->getPassword(),
|
||||
'enabled' => $model->enabled ? 1 : 0
|
||||
]);
|
||||
return $this->connection->executeStatement($qb) > 0;
|
||||
}
|
||||
$qb = $this->connection->createQueryBuilder()
|
||||
->update($this->table)
|
||||
->set('name', $model->name)
|
||||
->set('password', $model->getPassword())
|
||||
->set('enabled', $model->enabled ? 1 : 0)
|
||||
->where('id = ?');
|
||||
return $this->connection->executeStatement($qb, [$model->id]) > 0;
|
||||
}
|
||||
|
||||
public function fetchByName(string $name): UserModel|bool
|
||||
{
|
||||
$qb = $this->connection->createQueryBuilder()
|
||||
->select('*')
|
||||
->from($this->table)
|
||||
->where('name = ?');
|
||||
return $this->load(
|
||||
$this->connection
|
||||
->executeQuery($qb, [$name])
|
||||
->fetchAssociative()
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user