|
<?php |
|
|
|
if (!function_exists('generate_random_string')) { |
|
/** |
|
* Generate a random string of a specified length. |
|
* |
|
* @param int $length |
|
* @return string |
|
*/ |
|
function generate_random_string($length = 10) |
|
{ |
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
$randomString = ''; |
|
for ($i = 0; $i < $length; $i++) { |
|
$randomString .= $characters[rand(0, strlen($characters) - 1)]; |
|
} |
|
return $randomString; |
|
} |
|
} |
|
|
|
if (!function_exists('format_date')) { |
|
/** |
|
* Format a date to a human-readable format. |
|
* |
|
* @param string $date |
|
* @param string $format |
|
* @return string |
|
*/ |
|
function format_date($date, $format = 'Y-m-d H:i:s') |
|
{ |
|
return \Carbon\Carbon::parse($date)->format($format); |
|
} |
|
} |
|
|
|
if (!function_exists('slugify')) { |
|
/** |
|
* Convert a string to a URL-friendly slug. |
|
* |
|
* @param string $text |
|
* @return string |
|
*/ |
|
function slugify($text) |
|
{ |
|
return \Illuminate\Support\Str::slug($text); |
|
} |
|
} |
|
|
|
if (!function_exists('is_production')) { |
|
/** |
|
* Check if the application is in production mode. |
|
* |
|
* @return bool |
|
*/ |
|
function is_production() |
|
{ |
|
return config('app.env') === 'production'; |
|
} |
|
} |
|
|
|
if (!function_exists('array_to_csv')) { |
|
/** |
|
* Convert an array to a CSV string. |
|
* |
|
* @param array $data |
|
* @return string |
|
*/ |
|
function array_to_csv(array $data) |
|
{ |
|
$output = fopen('php://temp', 'r+'); |
|
foreach ($data as $row) { |
|
fputcsv($output, $row); |
|
} |
|
rewind($output); |
|
$csv = stream_get_contents($output); |
|
fclose($output); |
|
return $csv; |
|
} |
|
} |
|
|
|
if (!function_exists('log_error')) { |
|
/** |
|
* Log an error with a custom message. |
|
* |
|
* @param \Throwable $exception |
|
* @param string $message |
|
* @return void |
|
*/ |
|
function log_error(\Throwable $exception, $message = 'An error occurred') |
|
{ |
|
\Log::error($message, [ |
|
'error' => $exception->getMessage(), |
|
'file' => $exception->getFile(), |
|
'line' => $exception->getLine(), |
|
]); |
|
} |
|
} |
|
|
|
if (!function_exists('humanize_date')) { |
|
/** |
|
* Format a date to a human-readable format. |
|
* |
|
* @param string $date |
|
* @param string $format |
|
* @return string |
|
*/ |
|
function humanize_date($date) |
|
{ |
|
return Carbon\Carbon::parse($date)->diffForHumans(); |
|
} |
|
} |