47 lines
1.2 KiB
PHP
47 lines
1.2 KiB
PHP
![]() |
<?php
|
||
|
namespace Incoviba\Repository;
|
||
|
|
||
|
use Incoviba\Common\Define;
|
||
|
use Incoviba\Common\Ideal;
|
||
|
use Incoviba\Model;
|
||
|
|
||
|
class User extends Ideal\Repository
|
||
|
{
|
||
|
public function __construct(Define\Connection $connection)
|
||
|
{
|
||
|
parent::__construct($connection);
|
||
|
$this->setTable('users');
|
||
|
}
|
||
|
|
||
|
public function create(?array $data = null): Define\Model
|
||
|
{
|
||
|
$map = [
|
||
|
'name' => [],
|
||
|
'password' => [],
|
||
|
'enabled' => [
|
||
|
'function' => function($data) {
|
||
|
return $data['enabled'] != 0;
|
||
|
}
|
||
|
]
|
||
|
];
|
||
|
return $this->parseData(new Model\User(), $data, $map);
|
||
|
}
|
||
|
|
||
|
public function save(Define\Model $model): Define\Model
|
||
|
{
|
||
|
$model->id = $this->saveNew(['name', 'password', 'enabled'], [$model->name, $model->password, $model->enabled ? 1 : 0]);
|
||
|
return $model;
|
||
|
}
|
||
|
|
||
|
public function edit(Define\Model $model, array $new_data): Define\Model
|
||
|
{
|
||
|
return $this->update($model, ['name', 'password', 'enabled'], $new_data);
|
||
|
}
|
||
|
|
||
|
public function fetchByName(string $name): Model\User
|
||
|
{
|
||
|
$query = "SELECT * FROM `{$this->getTable()}` WHERE `name` = ?";
|
||
|
return $this->fetchOne($query, [$name]);
|
||
|
}
|
||
|
}
|