public static function summarize(
        string $str,
        int $maxLength,
        string $ellipsis = "...",
        string $needle = ""
    ): string
    {
        $length = mb_strlen($str);
        if ($length <= $maxLength) {
            return $str;
        }

        $endPos = $maxPos = $maxLength - mb_strlen($ellipsis);
        if ($needle !== "") {
            $offset = $maxPos - $length;
            $endPos = mb_strrpos($str, $needle, $offset);
            if ($endPos === false) {
                $endPos = $maxPos;
            }
        }

        return mb_substr($str, 0, $endPos).$ellipsis;
    }