<?php
declare(strict_types=1);

namespace App\Framework\Http\Session;

final readonly class RandomBytesSessionIdGenerator implements SessionIdGenerator
{
    public function __construct(
        private int $length = 32
    ) {
    }

    public function generate(): SessionId
    {
        return SessionId::fromString(bin2hex(random_bytes($this->length)));
    }

    public function isValid(SessionId $id): bool
    {
        $idString = $id->toString();
        return strlen($idString) === $this->length * 2 && ctype_xdigit($idString);
    }
}
