$max) $value = $max; return $value; } /** * Sanitizar número decimal */ public static function decimal($value, int $decimals = 2): float { $value = filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); return round((float)$value, $decimals); } /** * Sanitizar boolean */ public static function boolean($value): bool { if (is_bool($value)) { return $value; } if (is_string($value)) { $value = strtolower(trim($value)); // TRUE values $trueValues = [ '1', 'true', 'yes', 'y', 'sim', 's', 'ok', 'on', 'active', 'enabled', 'enable', 'verdadeiro', 'v', 'positivo', 'checked' ]; return in_array($value, $trueValues, true); } if (is_numeric($value)) { return $value > 0; } return false; } /** * Sanitizar URL */ public static function url(?string $value): string { if ($value === null) return ''; $value = trim($value); $value = filter_var($value, FILTER_SANITIZE_URL); return $value ?: ''; } /** * Sanitizar telefone */ public static function phone(?string $value): string { if ($value === null) return ''; $value = trim($value); $value = preg_replace('/[^0-9+\-\(\)\s]/', '', $value); return substr($value, 0, 20); } /** * Sanitizar data */ public static function date(?string $value): string { if ($value === null || empty(trim($value))) { return date('Y-m-d'); } $value = trim($value); // Validar formato Y-m-d if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) { // Verificar se a data é válida if (checkdate((int)substr($value, 5, 2), (int)substr($value, 8, 2), (int)substr($value, 0, 4))) { return $value; } } // Tentar converter outros formatos $timestamp = strtotime($value); if ($timestamp !== false && $timestamp > 0) { $formatted = date('Y-m-d', $timestamp); // Verificar se a data resultante é válida if ($formatted !== '1970-01-01') { return $formatted; } } // Se tudo falhar, retornar data atual return date('Y-m-d'); } /** * Sanitizar array */ public static function array(array $data, ?callable $sanitizer = null): array { if ($sanitizer === null) { $sanitizer = [self::class, 'string']; } return array_map($sanitizer, $data); } /** * Sanitizar para SQL (escapar) */ public static function sql(string $value): string { return addslashes($value); } /** * Sanitizar para HTML (escapar) */ public static function html(string $value): string { return htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); } /** * Sanitizar para JavaScript (escapar) */ public static function js(string $value): string { return json_encode($value, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP); } /** * Sanitizar para atributos HTML */ public static function attribute(string $value): string { return htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); } /** * Sanitizar para CSS */ public static function css(string $value): string { return preg_replace('/[^a-zA-Z0-9\s\-_\.#]/', '', $value); } /** * Sanitizar nome de ficheiro */ public static function filename(string $value): string { $value = preg_replace('/[^a-zA-Z0-9\-_\.]/', '', $value); return substr($value, 0, 255); } /** * Sanitizar slug/URL amigável */ public static function slug(string $value): string { $value = strtolower($value); $value = preg_replace('/[^a-z0-9\-]/', '-', $value); $value = preg_replace('/-+/', '-', $value); $value = trim($value, '-'); return substr($value, 0, 100); } }