Files
bildirchin/src/Helpers.php
T

62 lines
1.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace Bildirchin;
class Helpers
{
public static function formatPrice(int|float $price): string
{
return self::toPersianDigits(number_format((float)$price, 0, '.', ',')) . ' ' . Config::CURRENCY;
}
public static function toPersianDigits(string|int|float $input): string
{
$en = ['0','1','2','3','4','5','6','7','8','9'];
$fa = ['۰','۱','۲','۳','۴','۵','۶','۷','۸','۹'];
return str_replace($en, $fa, (string)$input);
}
public static function sanitize(string $input): string
{
return htmlspecialchars(trim($input), ENT_QUOTES, 'UTF-8');
}
public static function jsonResponse(array $data, int $status = 200): void
{
http_response_code($status);
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
exit;
}
public static function flash(string $key, ?string $msg = null): ?string
{
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
if ($msg !== null) {
$_SESSION['flash'][$key] = $msg;
return null;
}
if (isset($_SESSION['flash'][$key])) {
$value = $_SESSION['flash'][$key];
unset($_SESSION['flash'][$key]);
return $value;
}
return null;
}
public static function getJalaliDate(?string $timestamp = null): string
{
$time = $timestamp ? strtotime($timestamp) : time();
$date = date('Y/m/d H:i', $time);
return self::toPersianDigits($date);
}
public static function generateOrderCode(): string
{
return 'BLD-' . strtoupper(substr(uniqid(), -6));
}
}