Commit 7bf7327d authored by Administrator's avatar Administrator

Update 1 files via Son of Anton

parent ae64a019
<?php
declare(strict_types=1);
use App\Core\App;
use App\Core\Database;
use App\Core\Response;
use App\Core\CSRF;
function env(string $key, $default = null)
{
return $_ENV[$key] ?? $_SERVER[$key] ?? $default;
}
function app(): App
{
return App::getInstance();
}
function db(): Database
{
return App::getInstance()->db();
}
function config(string $key = null, $default = null)
{
if ($key === null) {
return App::getInstance()->config();
}
return App::getInstance()->config($key, $default);
}
function session(string $key = null, $default = null)
{
$session = App::getInstance()->session();
if ($key === null) {
return $session;
// Find the url() function and replace it with this:
if (!function_exists('url')) {
function url(string $path = ''): string
{
$path = ltrim($path, '/');
// For assets and internal links, use relative path from web root
// This avoids all protocol/host mismatch issues
$basePath = rtrim(parse_url(config('app.url', ''), PHP_URL_PATH) ?: '', '/');
if ($path === '') {
return $basePath ?: '/';
}
return $basePath . '/' . $path;
}
return $session->get($key, $default);
}
function redirect(string $url): Response
{
return (new Response())->redirect($url);
}
function url(string $path = ''): string
{
$base = rtrim(App::getInstance()->config('app.url', ''), '/');
return $base . '/' . ltrim($path, '/');
}
function route(string $name, array $params = []): string
{
return \App\Core\Router::url($name, $params);
}
function old(string $key, $default = '')
{
$old = App::getInstance()->session()->get('_old_input', []);
return $old[$key] ?? $default;
}
function csrf_field(): string
{
return CSRF::field();
}
function e(?string $value): string
{
return htmlspecialchars($value ?? '', ENT_QUOTES, 'UTF-8');
}
function __(?string $key, ?string $default = null): string
{
return $key ?? $default ?? '';
}
function now(): string
{
return date('Y-m-d H:i:s');
}
function today(): string
{
return date('Y-m-d');
}
function money(?string $amount): string
{
if ($amount === null || $amount === '') return '0.00 ج.م';
$formatted = number_format((float) $amount, 2, '.', ',');
return $formatted . ' ج.م';
}
function percentage(?string $value): string
{
if ($value === null) return '0%';
return $value . '%';
}
function age_from_dob(string $dob): array
{
try {
$birth = new \DateTime($dob);
$now = new \DateTime();
$diff = $now->diff($birth);
return ['years' => $diff->y, 'months' => $diff->m];
} catch (\Exception $e) {
return ['years' => 0, 'months' => 0];
}
}
function arabic_date(string $date): string
{
$months = [
1 => 'يناير', 2 => 'فبراير', 3 => 'مارس', 4 => 'أبريل',
5 => 'مايو', 6 => 'يونيو', 7 => 'يوليو', 8 => 'أغسطس',
9 => 'سبتمبر', 10 => 'أكتوبر', 11 => 'نوفمبر', 12 => 'ديسمبر',
];
try {
$dt = new \DateTime($date);
return $dt->format('d') . ' ' . $months[(int) $dt->format('m')] . ' ' . $dt->format('Y');
} catch (\Exception $e) {
return $date;
}
}
function is_arabic(string $text): bool
{
return (bool) preg_match('/[\p{Arabic}]/u', $text);
}
function generate_uuid(): string
{
$data = random_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
function mask_national_id(string $nid): string
{
if (strlen($nid) !== 14) return $nid;
return substr($nid, 0, 3) . str_repeat('*', 9) . substr($nid, 12, 2);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment