Files
michaelschiemer/backups/docs-backup-20250731125004/validation-rules/url-validation-example.php
Michael Schiemer 55a330b223 Enable Discovery debug logging for production troubleshooting
- Add DISCOVERY_LOG_LEVEL=debug
- Add DISCOVERY_SHOW_PROGRESS=true
- Temporary changes for debugging InitializerProcessor fixes on production
2025-08-11 20:13:26 +02:00

80 lines
2.5 KiB
PHP

<?php
/**
* URL Validation Rule - Verwendungsbeispiele
*
* Die URL Validation Rule verwendet das bestehende Url Value Object
* für konsistente und umfassende URL-Validierung.
*/
declare(strict_types=1);
use App\Framework\Validation\Rules\Url;
// Basis URL-Validierung
class UserProfileRequest
{
public function __construct(
#[Url]
public readonly string $website,
#[Url(requireSecure: true)]
public readonly string $secureUrl,
#[Url(allowLocal: false)]
public readonly string $publicUrl,
#[Url(autoAddProtocol: true)]
public readonly string $flexibleUrl,
#[Url(
requireSecure: true,
allowLocal: false,
message: 'Bitte geben Sie eine gültige HTTPS-URL ein, die öffentlich erreichbar ist.'
)]
public readonly string $strictUrl,
) {}
}
// Verwendungsbeispiele:
// ✅ Gültige URLs
$validExamples = [
'website' => 'https://example.com',
'secureUrl' => 'https://secure.example.com',
'publicUrl' => 'https://public.example.com',
'flexibleUrl' => 'example.com', // Wird zu https://example.com
'strictUrl' => 'https://production.example.com',
];
// ❌ Ungültige URLs
$invalidExamples = [
'website' => 'not-a-url', // Kein gültiges URL-Format
'secureUrl' => 'http://insecure.com', // Nicht HTTPS
'publicUrl' => 'https://localhost', // Lokale URL nicht erlaubt
'flexibleUrl' => 'ftp://files.com', // FTP nicht HTTP/HTTPS
'strictUrl' => 'http://localhost', // Nicht sicher UND lokal
];
/**
* Die URL Validation Rule unterstützt verschiedene Konfigurationen:
*
* @param bool $requireSecure - Nur HTTPS URLs erlauben
* @param bool $allowLocal - Lokale URLs (localhost, 192.168.x.x) erlauben
* @param bool $autoAddProtocol - Automatisch https:// hinzufügen wenn fehlt
* @param string|null $message - Benutzerdefinierte Fehlermeldung
*
* Validierungsregeln des Url Value Objects:
* - Gültiges URL-Format (parse_url() erfolgreich)
* - Scheme und Host erforderlich
* - Unterstützte Schemes: http, https, ftp, ftps, ssh, file, data
* - Host max. 253 Zeichen
* - Keine ungültigen Zeichen im Host: Leerzeichen, <, >, ", '
* - Keine aufeinanderfolgenden Punkte im Host
* - Host darf nicht mit Punkt oder Bindestrich beginnen/enden
* - IP-Adressen werden validiert (filter_var FILTER_VALIDATE_IP)
* - Ports müssen zwischen 1-65535 liegen
* - Keine Leer-URLs (nur Schema ohne Host)
* - Keine führenden/nachgestellten Leerzeichen
* - Max. 2048 Zeichen Gesamtlänge
*/