format($format); } catch (\Exception $e) { return ''; } } /** * Formatar data e hora para exibição */ public static function datetime(string $datetime, string $format = 'd/m/Y H:i'): string { if (empty($datetime)) { return ''; } try { $dateObj = new \DateTime($datetime); return $dateObj->format($format); } catch (\Exception $e) { return ''; } } /** * Formatar número para exibição */ public static function number(float|int $number, int $decimals = 2): string { return number_format($number, $decimals, ',', '.'); } /** * Formatar moeda para exibição */ public static function currency(float|int $amount, string $currency = '€'): string { return $currency . ' ' . self::number($amount, 2); } /** * Truncar texto com ellipsis */ public static function truncate(string $text, int $length = 100, string $ellipsis = '...'): string { if (mb_strlen($text) <= $length) { return $text; } return mb_substr($text, 0, $length) . $ellipsis; } /** * Limpar string para uso em IDs/classes CSS */ public static function cssId(string $value): string { return preg_replace('/[^a-zA-Z0-9\-_]/', '-', strtolower($value)); } /** * Escapar para uso em atributos data-* */ public static function dataAttr(string $value): string { return self::html($value); } /** * Verificar se valor é vazio e retornar fallback */ public static function fallback(string $value, string $fallback = ''): string { return empty(trim($value)) ? $fallback : $value; } /** * Formatar bytes para exibição humana */ public static function bytes(int $bytes): string { $units = ['B', 'KB', 'MB', 'GB', 'TB']; for ($i = 0; $bytes > 1024 && $i < count($units) - 1; $i++) { $bytes /= 1024; } return round($bytes, 2) . ' ' . $units[$i]; } /** * Formatar percentagem */ public static function percentage(float $value, int $decimals = 1): string { return number_format($value, $decimals, ',', '.') . '%'; } /** * Formatar tempo decorrido */ public static function timeAgo(string $datetime): string { if (empty($datetime)) { return ''; } try { $date = new \DateTime($datetime); $now = new \DateTime(); $diff = $now->diff($date); if ($diff->y > 0) { return $diff->y . ' ano' . ($diff->y > 1 ? 's' : ''); } elseif ($diff->m > 0) { return $diff->m . ' mês' . ($diff->m > 1 ? 'es' : ''); } elseif ($diff->d > 0) { return $diff->d . ' dia' . ($diff->d > 1 ? 's' : ''); } elseif ($diff->h > 0) { return $diff->h . ' hora' . ($diff->h > 1 ? 's' : ''); } elseif ($diff->i > 0) { return $diff->i . ' minuto' . ($diff->i > 1 ? 's' : ''); } else { return 'agora'; } } catch (\Exception $e) { return ''; } } }