Files
michaelschiemer/src/Framework/Http/Session/SessionId.php

53 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Framework\Http\Session;
use InvalidArgumentException;
final readonly class SessionId
{
private function __construct(private string $value)
{
$this->validate($value);
}
public static function fromString(string $value): self
{
return new self($value);
}
public function toString(): string
{
return $this->value;
}
public function __toString(): string
{
return $this->toString();
}
public function equals(SessionId $other): bool
{
return $this->value === $other->value;
}
private function validate(string $value): void
{
if (empty($value)) {
throw new InvalidArgumentException('SessionId darf nicht leer sein');
}
// Weitere Validierungen je nach Anforderungen
// z.B. Mindestlänge, erlaubte Zeichen, Format
if (strlen($value) < 32) {
throw new InvalidArgumentException('SessionId muss mindestens 32 Zeichen lang sein');
}
if (!ctype_alnum($value)) {
throw new InvalidArgumentException('SessionId darf nur alphanumerische Zeichen enthalten');
}
}
}