protected function finalizeSlug($slug)
    {
        $prefix = $this->slugPrefix();
        if ($prefix) {
            $prefix = $this->generateRoutePattern((string)$prefix);
            if ($slug === $prefix) {
                throw new UnexpectedValueException('The slug is the same as the prefix.');
            }
            $slug = $prefix.preg_replace('!^'.preg_quote($prefix).'\b!', '', $slug);
        }

        $suffix = $this->slugSuffix();
        if ($suffix) {
            $suffix = $this->generateRoutePattern((string)$suffix);
            if ($slug === $suffix) {
                throw new UnexpectedValueException('The slug is the same as the suffix.');
            }
            $slug = preg_replace('!\b'.preg_quote($suffix).'$!', '', $slug).$suffix;
        }

        $slug = rtrim($slug, '/');

        return $slug;
    }