igfs/app/services/UserService.php
2024-07-26 16:26:01 +03:00

59 lines
1.3 KiB
PHP

<?php
namespace app\services;
use app\helpers\Debug;
use app\models\User;
use kernel\FormModel;
class UserService
{
public function create(FormModel $form_model): false|User
{
$model = new User();
$model->username = $form_model->getItem('username');
$model->email = $form_model->getItem('email');
$model->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT);
if ($model->save()){
return $model;
}
return false;
}
public function update(FormModel $form_model, User $user): false|User
{
$user->username = $form_model->getItem('username');
$user->email = $form_model->getItem('email');
$user->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT);
if ($user->save()){
return $user;
}
return false;
}
public static function check(int $user_id): bool
{
if (User::where(['id' => $user_id])->first())
{
return true;
}
return false;
}
public static function createUsernameArr(): array
{
foreach (User::all()->toArray() as $user) {
$userArr[$user['id']] = $user['username'];
}
if (!empty($userArr)) {
return $userArr;
}
return [];
}
}