55 lines
1.7 KiB
PHP
55 lines
1.7 KiB
PHP
<?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()
|
|
);
|
|
}
|
|
}
|