EscolaLMS/Jitsi

View on GitHub
src/Helpers/StringHelper.php

Summary

Maintainability
A
35 mins
Test Coverage
A
100%
<?php

namespace EscolaLms\Jitsi\Helpers;

use EscolaLms\Jitsi\Enum\JitsiEnum;

class StringHelper
{
    public static function convertToJitsiSlug(string $str, array $options = [], string|null $type = null, int|null $modelId = null, string|null $subModel = null): string
    {
        // Make sure string is in UTF-8 and strip invalid UTF-8 characters
        $str = mb_convert_encoding($str, 'UTF-8', mb_list_encodings());

        $defaults = [
            'delimiter' => '',
            'limit' => null,
            'lowercase' => true,
            'replacements' => [],
            'transliterate' => true,
        ];

        // Merge options
        $options = array_merge($defaults, $options);

        $charMap = JitsiEnum::CHAR_MAP;

        // Make custom replacements
        $str = preg_replace(array_keys($options['replacements']), $options['replacements'], $str);

        // Transliterate characters to ASCII
        if ($options['transliterate']) {
            $str = str_replace(array_keys($charMap), $charMap, $str);
        }

        // Replace non-alphanumeric characters with our delimiter
        $str = preg_replace('/[^\p{L}\p{Nd}]+/u', $options['delimiter'], $str);

        // Remove duplicate delimiters
        $str = preg_replace('/(' . preg_quote($options['delimiter'], '/') . '){2,}/', '$1', $str);

        // Truncate slug to max. characters
        $str = mb_substr($str, 0, ($options['limit'] ? $options['limit'] : mb_strlen($str, 'UTF-8')), 'UTF-8');

        // Remove delimiter from ends
        $str = trim($str, $options['delimiter']);
        $str = $options['lowercase'] ? mb_strtolower($str, 'UTF-8') : $str;
        $suffix = ($type ? "_{$type}" : '') . ($modelId ? "_{$modelId}" : '') . ($subModel ? "_{$subModel}" : '');
        return "{$str}{$suffix}";
    }

}