protected static function normalizeQuery(?string $query): ?string
    {
        if ($query === null) {
            return null;
        }

        if (!static::isValidQuery($query)) {
            $message = sprintf('Invalid URI query: %s', $query);
            throw new DomainException($message);
        }

        return static::encodeQuery(static::decode($query, static::UNRESERVED_SET));
    }