diff --git a/app/Helpers/helpers.php b/app/Helpers/helpers.php new file mode 100644 index 0000000..b46a2fb --- /dev/null +++ b/app/Helpers/helpers.php @@ -0,0 +1,229 @@ +diffForHumans(); + } +} + +/** + * Check if the authenticated user is an admin. + */ +if (!function_exists('is_admin')) { + function is_admin(): bool + { + return Auth::check() && Auth::user()->role === 'admin'; + } +} + +/** + * Generate a versioned asset URL to prevent browser caching. + */ +if (!function_exists('asset_versioned')) { + function asset_versioned($path): string + { + $version = config('app.asset_version', time()); + return asset($path) . '?v=' . $version; + } +} + +/** + * Get a limited string excerpt with optional length. + */ +if (!function_exists('str_excerpt')) { + function str_excerpt(string $string, int $length = 100): string + { + return Str::limit(strip_tags($string), $length); + } +} + +/** + * Format a date using a given format (default: d/m/Y). + */ +if (!function_exists('format_date')) { + function format_date($date, string $format = 'd/m/Y'): string + { + return Carbon::parse($date)->format($format); + } +} + +/** + * Send a generic notification to a user. + */ +if (!function_exists('send_notification')) { + function send_notification(\App\Models\User $user, $message, $type = 'info') + { + $user->notify(new \App\Notifications\GenericNotification($message, $type)); + } +} + +/** + * Get an environment variable as a boolean. + */ +if (!function_exists('env_bool')) { + function env_bool(string $key, bool $default = false): bool + { + return filter_var(env($key, $default), FILTER_VALIDATE_BOOLEAN); + } +} + +/** + * Check if the current route matches one or more given route names. + */ +if (!function_exists('route_is')) { + function route_is(string|array $names): bool + { + return request()->routeIs($names); + } +} + +/** + * Abort the request if the app is running in a specific environment. + */ +if (!function_exists('abort_if_env')) { + function abort_if_env(string $env, int $code = 403) + { + if (app()->environment($env)) { + abort($code, "Action not allowed in $env environment."); + } + } +} + +/** + * Return a CSS class if the current route matches one or more route names. + */ +if (!function_exists('active_class')) { + function active_class(string|array $routes, string $class = 'active'): string + { + return request()->routeIs($routes) ? $class : ''; + } +} + +/** + * Return a standard JSON response. + */ +if (!function_exists('json_response')) { + function json_response($data = [], string $message = '', int $code = 200) + { + return response()->json([ + 'message' => $message, + 'data' => $data, + ], $code); + } +} + +/** + * Generate a UUID (Universally Unique Identifier). + */ +if (!function_exists('uuid')) { + function uuid(): string + { + return (string) Str::uuid(); + } +} + +/** + * Convert bytes into a human-readable file size format. + */ +if (!function_exists('file_size_human')) { + function file_size_human(int $bytes, int $precision = 2): string + { + $units = ['B', 'KB', 'MB', 'GB', 'TB']; + $bytes = max($bytes, 0); + $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); + $pow = min($pow, count($units) - 1); + $bytes /= (1 << (10 * $pow)); + return round($bytes, $precision) . ' ' . $units[$pow]; + } +} + +/** + * Get a full URL for a file stored in Laravel's storage. + */ +if (!function_exists('storage_url')) { + function storage_url(string $path): string + { + return Storage::url($path); + } +} + +/** + * Log a debug message to the Laravel log. + */ +if (!function_exists('log_debug')) { + function log_debug(string $message, array $context = []) + { + Log::debug($message, $context); + } +} + +/** + * Check if a string is valid JSON. + */ +if (!function_exists('is_json')) { + function is_json(string $string): bool + { + json_decode($string); + return json_last_error() === JSON_ERROR_NONE; + } +} + +/** + * Remember cache for a given key and duration. + */ +if (!function_exists('cache_remember')) { + function cache_remember(string $key, int $minutes, Closure $callback) + { + return Cache::remember($key, now()->addMinutes($minutes), $callback); + } +} + +/** + * Perform a GET HTTP request and return the JSON response as array. + */ +if (!function_exists('http_get_json')) { + function http_get_json(string $url, array $headers = []): array + { + try { + $response = Http::withHeaders($headers)->get($url); + return $response->json(); + } catch (Exception $e) { + Log::error("HTTP GET failed: " . $e->getMessage()); + return []; + } + } +} + +/** + * Get the authenticated user from a specific guard. + */ +if (!function_exists('user_guard')) { + function user_guard(string $guard = 'web') + { + return Auth::guard($guard); + } +} diff --git a/composer.lock b/composer.lock index 6bc3a79..17384dd 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "fd38e7434ec17abb4278b799dc697955", + "content-hash": "531dbc4605741ffe9143e7fa7ef28e38", "packages": [ { "name": "brick/math",