summaryrefslogtreecommitdiff
path: root/src/application/mystic/forum/utils/StringUtils.php
blob: 94221fb94ca252282d5c3ec229c6dc59327b3b47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
declare(strict_types=1);

namespace mystic\forum\utils;

final class StringUtils {
    use StaticClass;

    public static function camelToSnake(string $camelCase): string {
        $result = '';

        for ($i = 0; $i < strlen($camelCase); $i++) {
            $char = $camelCase[$i];

            if (ctype_upper($char)) {
                $result .= '_' . strtolower($char);
            } else {
                $result .= $char;
            }
        }

        return ltrim($result, '_');
    }

    public static function truncate(string $str, int $maxLength, string $ellipsis = "…"): string {
        if ($maxLength < 0)
            return $str;
        return mb_strimwidth($str, 0, $maxLength, $ellipsis);
    }
}