Less home code more F3
This commit is contained in:
79
app/helpers.php
Normal file
79
app/helpers.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
function app_timezone(string $value): string
|
||||
{
|
||||
$value = trim($value);
|
||||
return in_array($value, DateTimeZone::listIdentifiers(), true) ? $value : 'UTC';
|
||||
}
|
||||
|
||||
function app_now(): string
|
||||
{
|
||||
return gmdate('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
function app_slug(string $value): string
|
||||
{
|
||||
$slug = Web::instance()->slug(trim($value));
|
||||
return $slug !== '' ? $slug : 'article';
|
||||
}
|
||||
|
||||
function app_date_fr(string $value): string
|
||||
{
|
||||
static $formatter = null;
|
||||
|
||||
$value = trim($value);
|
||||
if ($value === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
try {
|
||||
$date = new DateTimeImmutable($value, new DateTimeZone('UTC'));
|
||||
$date = $date->setTimezone(new DateTimeZone(date_default_timezone_get()));
|
||||
|
||||
if (!$formatter instanceof IntlDateFormatter) {
|
||||
$formatter = new IntlDateFormatter(
|
||||
'fr_FR',
|
||||
IntlDateFormatter::LONG,
|
||||
IntlDateFormatter::SHORT,
|
||||
date_default_timezone_get(),
|
||||
IntlDateFormatter::GREGORIAN,
|
||||
"d MMMM yyyy 'à' HH:mm"
|
||||
);
|
||||
}
|
||||
|
||||
return (string) ($formatter->format($date) ?: $value);
|
||||
} catch (Throwable) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
function app_error_meta(int $code): array
|
||||
{
|
||||
return match ($code) {
|
||||
400 => ['title' => 'Requête invalide', 'message' => 'La requête envoyée est invalide.'],
|
||||
403 => ['title' => 'Accès refusé', 'message' => 'Tu n’as pas accès à cette ressource.'],
|
||||
404 => ['title' => 'Page introuvable', 'message' => 'La page demandée est introuvable.'],
|
||||
default => ['title' => 'Erreur serveur', 'message' => 'Une erreur est survenue.'],
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
function app_request_is_secure(): bool
|
||||
{
|
||||
if (!empty($_SERVER['HTTPS']) && strtolower((string) $_SERVER['HTTPS']) !== 'off') {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strtolower((string) ($_SERVER['REQUEST_SCHEME'] ?? '')) === 'https') {
|
||||
return true;
|
||||
}
|
||||
|
||||
$forwardedProto = strtolower(trim((string) ($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '')));
|
||||
if ($forwardedProto !== '') {
|
||||
return explode(',', $forwardedProto)[0] === 'https';
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user