MicroFrameWork/app/services/UserService.php

59 lines
1.3 KiB
PHP
Raw Normal View History

2024-07-24 14:07:45 +03:00
<?php
namespace app\services;
2024-07-25 16:15:18 +03:00
use app\helpers\Debug;
2024-07-24 14:07:45 +03:00
use app\models\User;
use kernel\FormModel;
class UserService
{
2024-07-24 17:22:59 +03:00
public function create(FormModel $form_model): false|User
2024-07-24 14:07:45 +03:00
{
$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);
2024-07-24 17:22:59 +03:00
if ($model->save()){
return $model;
}
return false;
2024-07-24 14:07:45 +03:00
}
2024-07-25 13:23:50 +03:00
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;
}
2024-07-26 12:42:44 +03:00
public static function check(int $user_id): bool
2024-07-25 16:15:18 +03:00
{
2024-07-26 12:42:44 +03:00
2024-07-26 11:57:05 +03:00
if (User::where(['id' => $user_id])->first())
2024-07-25 16:15:18 +03:00
{
return true;
}
return false;
}
2024-07-26 16:09:06 +03:00
public static function createUsernameArr(): array
{
foreach (User::all()->toArray() as $user) {
$userArr[$user['id']] = $user['username'];;
}
if (!empty($userArr)) {
return $userArr;
}
return [];
}
2024-07-24 14:07:45 +03:00
}