private function toRegex(string $pattern): string
    {
        while (\preg_match(self::OPTIONAL_PLACEHOLDER_REGEX, $pattern)) {
            $pattern = \preg_replace(self::OPTIONAL_PLACEHOLDER_REGEX, '($1)?', $pattern);
        }

        $pattern = \preg_replace_callback(self::REQUIRED_PLACEHOLDER_REGEX, function (array $match = []) {
            $match = \array_pop($match);
            $pos = \mb_strpos($match, self::REGEX_PREFIX);
            if (false !== $pos) {
                $parameterName = \mb_substr($match, 0, $pos);
                $parameterRegex = \mb_substr($match, $pos + 2);

                return "(?<$parameterName>$parameterRegex)";
            }

            return "(?<$match>[^/]+)";
        }, $pattern);

        return $pattern;
    }