rest and post

This commit is contained in:
Kavalar 2025-01-09 16:27:34 +03:00
parent 1a54003030
commit dd231b0c07
5 changed files with 90 additions and 13 deletions

View File

@ -17,13 +17,32 @@ class RestController
return [];
}
protected function filters(): array
{
return [];
}
#[NoReturn] public function actionIndex(): void
{
$request = new Request();
$get = $request->get();
$page = $request->get('page') ?? 1;
$perPage = $request->get('per_page') ?? 10;
$query = $this->model->query();
if ($this->filters()) {
foreach ($this->filters() as $filter){
if (key_exists($filter, $get)){
if (is_numeric($get[$filter])){
$query->where($filter, $get[$filter]);
}
elseif (is_string($get[$filter])){
$query->where($filter,'like', '%' . $get[$filter] . '%');
}
}
}
}
if ($page > 1) {
$query->skip(($page - 1) * $perPage)->take($perPage);
} else {
@ -117,5 +136,4 @@ class RestController
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace kernel\helpers;
class ImageGD
{
public \GdImage $img;
public function __construct(string $resource = '', int $width = 200, int $height = 200)
{
if ($resource){
$this->img = imagecreatefrompng($resource);
}
else {
$this->img = imagecreatetruecolor($width, $height);
}
imagesavealpha($this->img, true);
}
public function addText(string $font_size, int $degree, int $x, int $y, string $color, string $font, string $text): void
{
$rgbArr = $this->hexToRgb($color);
$color = imagecolorallocate($this->img, $rgbArr[0], $rgbArr[1], $rgbArr[2]);
imagettftext($this->img, $font_size, $degree, $x, $y, $color, $font, $text);
}
public function addImg(\GdImage $gdImage, int $location_x, int $location_y, int $offset_src_x, int $offset_src_y, int $src_width, int $src_height, int $no_transparent): void
{
imagecopymerge($this->img, $gdImage, $location_x, $location_y, $offset_src_x, $offset_src_y, $src_width, $src_height, $no_transparent);
}
public function getImg()
{
return $this->img;
}
public function save(string $path): void
{
imagepng($this->img, $path);
imagedestroy($this->img);
}
protected function hexToRgb(string $hex)
{
return sscanf($hex, "#%02x%02x%02x");
}
}

View File

@ -1,6 +1,6 @@
{
"name": "Kernel",
"version": "0.1.1",
"version": "0.1.3",
"author": "ITGuild",
"slug": "kernel",
"type": "kernel",

View File

@ -13,21 +13,28 @@ use kernel\services\TokenService;
class SecureService
{
public static function createSecretCode(User $user): void
public static function createSecretCode(User $user): SecretCode
{
$secretCode = new SecretCode();
$secretCode->user_id = $user->id;
$secretCode->code = mt_rand(100000, 999999);
$secretCode->code_expires_at = date("Y-m-d H:i:s", strtotime("+5 minutes"));;
$secretCode->save();
return $secretCode;
}
public static function updateSecretCode(User $user): void
public static function updateSecretCode(User $user): SecretCode
{
$secretCode = SecretCode::where('user_id', $user->id)->first();
if(!$secretCode){
return self::createSecretCode($user);
}
$secretCode->code = mt_rand(100000, 999999);
$secretCode->code_expires_at = date("Y-m-d H:i:s", strtotime("+5 minutes"));;
$secretCode->save();
return $secretCode;
}
public static function getCodeByUserId(int $user_id)

View File

@ -11,6 +11,10 @@ class UserService
public function create(FormModel $form_model): false|User
{
$model = User::where("username", $form_model->getItem('username'))->first();
if ($model){
return $model;
}
$model = new User();
$model->username = $form_model->getItem('username');
$model->email = $form_model->getItem('email');