From dd231b0c0716434f0294158f77c28c134a498dcc Mon Sep 17 00:00:00 2001 From: Kavalar Date: Thu, 9 Jan 2025 16:27:34 +0300 Subject: [PATCH] rest and post --- kernel/RestController.php | 36 ++++++++++---- kernel/helpers/ImageGD.php | 48 +++++++++++++++++++ kernel/manifest.json | 2 +- .../modules/secure/services/SecureService.php | 11 ++++- kernel/modules/user/service/UserService.php | 6 ++- 5 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 kernel/helpers/ImageGD.php diff --git a/kernel/RestController.php b/kernel/RestController.php index 2d7596d..99ba279 100644 --- a/kernel/RestController.php +++ b/kernel/RestController.php @@ -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 { @@ -31,7 +50,7 @@ class RestController } $expand = $this->expand(); - $expandParams = explode( ",", $request->get('expand') ?? ""); + $expandParams = explode(",", $request->get('expand') ?? ""); $finalExpand = array_intersect($expandParams, $expand); if ($finalExpand) { $res = $query->get()->load($finalExpand)->toArray(); @@ -46,14 +65,14 @@ class RestController { $expand = $this->expand(); $request = new Request(); - $expandParams = explode( ",", $request->get('expand') ?? ""); + $expandParams = explode(",", $request->get('expand') ?? ""); $model = $this->model->where("id", $id)->first(); $finalExpand = array_intersect($expandParams, $expand); - if ($finalExpand){ + if ($finalExpand) { $model->load($finalExpand); } $res = []; - if ($model){ + if ($model) { $res = $model->toArray(); } @@ -64,7 +83,7 @@ class RestController { $model = $this->model->where("id", $id)->first(); $res = []; - if ($model){ + if ($model) { $res = $model->toArray(); } @@ -78,7 +97,7 @@ class RestController { $request = new Request(); $data = $request->post(); - foreach ($this->model->getFillable() as $item){ + foreach ($this->model->getFillable() as $item) { $this->model->{$item} = $data[$item] ?? null; } $this->model->save(); @@ -93,8 +112,8 @@ class RestController $model = $this->model->where('id', $id)->first(); - foreach ($model->getFillable() as $item){ - if (!empty($data[$item])){ + foreach ($model->getFillable() as $item) { + if (!empty($data[$item])) { $model->{$item} = $data[$item] ?? null; } } @@ -117,5 +136,4 @@ class RestController } - } \ No newline at end of file diff --git a/kernel/helpers/ImageGD.php b/kernel/helpers/ImageGD.php new file mode 100644 index 0000000..3b15b05 --- /dev/null +++ b/kernel/helpers/ImageGD.php @@ -0,0 +1,48 @@ +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"); + } + +} \ No newline at end of file diff --git a/kernel/manifest.json b/kernel/manifest.json index 6521135..bc1217e 100644 --- a/kernel/manifest.json +++ b/kernel/manifest.json @@ -1,6 +1,6 @@ { "name": "Kernel", - "version": "0.1.1", + "version": "0.1.3", "author": "ITGuild", "slug": "kernel", "type": "kernel", diff --git a/kernel/modules/secure/services/SecureService.php b/kernel/modules/secure/services/SecureService.php index b47f4d4..1699b6a 100644 --- a/kernel/modules/secure/services/SecureService.php +++ b/kernel/modules/secure/services/SecureService.php @@ -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) diff --git a/kernel/modules/user/service/UserService.php b/kernel/modules/user/service/UserService.php index 9915c2a..ebb26bc 100644 --- a/kernel/modules/user/service/UserService.php +++ b/kernel/modules/user/service/UserService.php @@ -11,7 +11,11 @@ class UserService public function create(FormModel $form_model): false|User { - $model = new 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'); $model->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT);