summaryrefslogtreecommitdiff
path: root/src/application/mystic/forum/utils/StringUtils.php
blob: fd3891544bb4b46c35160b3b5403d242773c45b5 (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
<?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 {
        return mb_strimwidth($str, 0, $maxLength, $ellipsis);
    }
}