rest and post
This commit is contained in:
parent
1a54003030
commit
dd231b0c07
@ -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
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
48
kernel/helpers/ImageGD.php
Normal file
48
kernel/helpers/ImageGD.php
Normal 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");
|
||||
}
|
||||
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "Kernel",
|
||||
"version": "0.1.1",
|
||||
"version": "0.1.3",
|
||||
"author": "ITGuild",
|
||||
"slug": "kernel",
|
||||
"type": "kernel",
|
||||
|
@ -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)
|
||||
|
@ -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');
|
||||
|
Loading…
x
Reference in New Issue
Block a user