Steuerkram/src/View.php
2026-03-25 00:19:33 +01:00

63 lines
1.6 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 Steuerkram;
class View
{
private string $viewDir;
private array $flash = [];
public function __construct()
{
$this->viewDir = dirname(__DIR__) . '/src/Views';
// Session-based flash messages
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
if (isset($_SESSION['flash'])) {
$this->flash = $_SESSION['flash'];
unset($_SESSION['flash']);
}
}
public function render(string $template, array $data = []): void
{
$data['flash'] = $this->flash;
$data['currentPath'] = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
extract($data, EXTR_SKIP);
include $this->viewDir . '/' . $template;
}
public static function redirect(string $path, string $message = '', string $type = 'success'): void
{
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
if ($message !== '') {
$_SESSION['flash'][] = ['type' => $type, 'message' => $message];
}
header('Location: ' . $path);
exit;
}
public static function h(mixed $value): string
{
return htmlspecialchars((string)$value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}
public static function formatMoney(mixed $value): string
{
return number_format((float)$value, 2, ',', '.') . ' €';
}
public static function formatDate(?string $date): string
{
if (!$date) return '–';
$d = \DateTime::createFromFormat('Y-m-d', $date);
return $d ? $d->format('d.m.Y') : $date;
}
}