Files

114 lines
2.4 KiB
PHP

<?php
function h($value): string
{
return htmlspecialchars((string)($value ?? ''), ENT_QUOTES, 'UTF-8');
}
function asset_url(string $path): string
{
$publicRoot = realpath(__DIR__ . '/../../public');
$fullPath = $publicRoot ? $publicRoot . '/' . ltrim($path, '/') : '';
$version = 'dev';
if ($fullPath !== '' && is_file($fullPath)) {
$mtime = filemtime($fullPath) ?: time();
$hash = is_readable($fullPath) ? substr(hash_file('sha256', $fullPath) ?: '', 0, 10) : '';
$version = $mtime . ($hash !== '' ? '-' . $hash : '');
}
return $path . '?v=' . rawurlencode($version);
}
function won($value): string
{
return number_format((float)$value, 0) . '원';
}
function money_plain($value, int $decimals = 0): string
{
return number_format((float)$value, $decimals);
}
function numf($value, int $decimals = 0): string
{
return number_format((float)$value, $decimals);
}
function intvalf($value): string
{
return number_format((int)$value);
}
function percentf($value, int $decimals = 1): string
{
return number_format((float)$value, $decimals) . '%';
}
function blank_to_dash($value): string
{
$value = trim((string)($value ?? ''));
return $value === '' ? '-' : $value;
}
function ymd($value): string
{
if (empty($value)) {
return '-';
}
$ts = strtotime((string)$value);
if ($ts === false) {
return h((string)$value);
}
return date('Y-m-d', $ts);
}
function ym($value): string
{
if (empty($value)) {
return '-';
}
$ts = strtotime((string)$value . '-01');
if ($ts === false) {
return h((string)$value);
}
return date('Y-m', $ts);
}
function redirect(string $url): void
{
header("Location: {$url}");
exit;
}
function set_flash_message(string $type, string $message): void
{
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
$_SESSION['flash_message'] = [
'type' => $type,
'message' => $message,
];
}
function get_flash_message(): ?array
{
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
if (empty($_SESSION['flash_message'])) {
return null;
}
$flash = $_SESSION['flash_message'];
unset($_SESSION['flash_message']);
return $flash;
}