Merge pull request #44 from CyberAli1989/master

Add helper functions for standard JSON responses
master
A1Gard 2 months ago committed by GitHub
commit 6dced06316
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -14,7 +14,8 @@ use Illuminate\Support\Facades\Route;
* @param $langCode string code like fa * @param $langCode string code like fa
* @return bool * @return bool
*/ */
function langIsRTL($langCode) { function langIsRTL($langCode)
{
$rtlLanguages = [ $rtlLanguages = [
'ar', // Arabic 'ar', // Arabic
'arc', // Aramaic 'arc', // Aramaic
@ -44,6 +45,7 @@ function langIsRTL($langCode) {
return in_array(strtolower($langCode), $rtlLanguages); return in_array(strtolower($langCode), $rtlLanguages);
} }
/** /**
* @param $lang string code like fa * @param $lang string code like fa
* @return string * @return string
@ -251,8 +253,9 @@ function logAdmin($method, $cls, $id): void
]); ]);
} }
function gfx(){ function gfx()
return \App\Models\Gfx::pluck('value','key'); {
return \App\Models\Gfx::pluck('value', 'key');
} }
@ -472,6 +475,7 @@ function getAdminRoutes()
return $routes; return $routes;
} }
/** /**
* get all client routes array * get all client routes array
* @return array * @return array
@ -612,8 +616,8 @@ function validateSettingRequest($setting, $newValue)
case 'optimize': case 'optimize':
if ($newValue != 'jpg' && $newValue != 'webp') { if ($newValue != 'jpg' && $newValue != 'webp') {
return 'webp'; return 'webp';
}else{ } else {
return $newValue; return $newValue;
} }
case 'gallery_thumb': case 'gallery_thumb':
case 'post_thumb': case 'post_thumb':
@ -695,7 +699,7 @@ function nestedWithData($items, $parent_id = null)
$r .= PHP_EOL . ' </li>'; $r .= PHP_EOL . ' </li>';
} }
} }
$r .= '</ol>' . PHP_EOL; $r .= '</ol>' . PHP_EOL;
return $r; return $r;
} }
@ -784,6 +788,7 @@ function getGroupBySetting($key)
{ {
return Group::where('id', getSetting($key) ?? 1)->first(); return Group::where('id', getSetting($key) ?? 1)->first();
} }
/** /**
* get group by setting key * get group by setting key
* @param $key * @param $key
@ -817,3 +822,62 @@ function getCategoryProductBySetting($key, $limit = 10)
return Category::where('id', getSetting($key) ?? 1)->first() return Category::where('id', getSetting($key) ?? 1)->first()
->products()->where('status', 1)->limit($limit)->get(); ->products()->where('status', 1)->limit($limit)->get();
} }
/**
* @param null $data
* @param null $message
* @param null $metaTitle
* @param null $metaDescription
* @param null $metaImage
* @param null $metaSourceImage
* @param null $ogUrl
* @param null $ogType
* @param string $ogLocate
* @param null $canonical_url
* @return \Illuminate\Http\JsonResponse
*/
function success($data = null, $message = null, $meta = [], $og = [], $twitter = [], $canonical_url = null, $jsonLd = null)
{
$defaultMeta = [
'title' => null,
'description' => null,
'image' => null,
'secure_image' => null,
];
$defaultOg = [
'url' => null,
'type' => null,
'site_name' => env('APP_NAME'),
'description' => null,
'locate' => 'fa_IR'
];
$defaultTwitter = [
'card' => 'summary_large_image',
'site' => '@yourTwitterHandle',
'title' => null,
'description' => null,
'image' => null,
];
return response()->json([
"success" => true,
"message" => $message,
"data" => $data,
"meta" => array_merge($defaultMeta, $meta),
"og" => array_merge($defaultOg, $og),
"twitter" => array_merge($defaultTwitter, $twitter),
"canonical_url" => $canonical_url,
]);
}
function errors($errors, $status = 422, $message = null, $data = null)
{
return response()->json([
"success" => false,
"errors" => $errors,
"message" => $message,
"data" => $data,
], $status);
}

Loading…
Cancel
Save