Skip to content

Instantly share code, notes, and snippets.

@devhammed
Created October 25, 2024 13:14
Show Gist options
  • Save devhammed/f138599a4355f9188e23246481541b83 to your computer and use it in GitHub Desktop.
Save devhammed/f138599a4355f9188e23246481541b83 to your computer and use it in GitHub Desktop.
My favourite Laravel helper functions
<?php
if ( ! function_exists('vite')) {
/**
* Get the Vite instance or Vite asset URL for the given file.
*/
function vite(?string $file = null, ?string $buildDirectory = null): Illuminate\Foundation\Vite|string
{
$vite = app(Illuminate\Foundation\Vite::class);
if (is_null($file)) {
return $vite;
}
return $vite->asset($file, $buildDirectory);
}
}
if ( ! function_exists('rgb_color')) {
/**
* Get the RGB color value for the given color.
*/
function rgb_color(string $color): string
{
$colors = Filament\Support\Facades\FilamentColor::getColors();
$shade = data_get($colors, $color, '0,0,0');
return 'rgb('.$shade.')';
}
}
if ( ! function_exists('hex_color')) {
/**
* Get the HEX color value for the given color.
*/
function hex_color(string $color): string
{
return Spatie\Color\Rgb::fromString(rgb_color($color))->toHex();
}
}
if ( ! function_exists('h')) {
/**
* Render a Blade template string.
*/
function h(string $html, array $props = []): Illuminate\Contracts\Support\Htmlable
{
return new Illuminate\Support\HtmlString(
app('blade.compiler')->render($html, $props),
);
}
}
if ( ! function_exists('t')) {
/**
* Translate the given key using less verbose syntax.
*
* @example t('Hello, {0}', 'World')
*/
function t(string $key, mixed ...$replace): string
{
try {
if (app()->isLocal() && ! blank($key)) {
cache()->lock('updating-translations')->get(function () use ($key) {
$path = lang_path(config('app.fallback_locale').'.json');
$translations = json_decode(file_get_contents($path), true);
$translations[$key] ??= null;
file_put_contents($path, json_encode($translations, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
});
}
$translation = app('translator')->get($key, $replace);
foreach ($replace as $placeholder => $value) {
$translation = str_replace('{'.$placeholder.'}', $value, $translation);
}
return str($translation)->inlineMarkdown()->trim()->toString();
} catch (Throwable) {
return str($key)->inlineMarkdown()->trim()->toString();
}
}
}
if ( ! function_exists('generate_pin_code')) {
/**
* Generate a verification pin code.
*/
function generate_pin_code(string $key, ?float $ttl = null, ?int $length = null): array
{
$pinTtl = $ttl ?? config('auth.verification_pin_ttl');
$pinLength = $length ?? config('auth.verification_pin_length');
$pinCachePrefix = config('auth.verification_pin_cache_key_prefix');
$pinBase = (int) str_repeat('1', $pinLength);
$pinEnd = (int) str_repeat('9', $pinLength);
$pin = random_int($pinBase, $pinEnd);
cache()->put(
sprintf('%s:%s', $pinCachePrefix, $key),
$pin,
now()->addMinutes($pinTtl),
);
return [$pin, $pinTtl];
}
}
if ( ! function_exists('verify_pin_code')) {
/**
* Verify the verification pin code.
*/
function verify_pin_code(string $key, string|int $pin): bool
{
$pinCachePrefix = config('auth.verification_pin_cache_key_prefix');
$cacheKey = sprintf('%s:%s', $pinCachePrefix, $key);
$valid = intval(cache($cacheKey)) === intval($pin);
if ($valid) {
cache()->forget($cacheKey);
}
return $valid;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment