<?php

namespace Medienreaktor\Courses\Service;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class SlugService
{

    private $upas = Array("ä" => "ae", "ü" => "ue", "ö" => "oe", "Ä" => "Ae", "Ü" => "Ue", "Ö" => "Oe", "ß" => "ss");

    /*
     *
     * @param string $title
     * @param int $uid
     * @return string slug
     */
    public function createTitleSlug(string $title, int $uid){

        $slug = $title."-".$uid;

        $text = strtr($slug, $this->upas);

        // replace non letter or digits by divider
        $text = preg_replace('~[^\pL\d]+~u', "-", $text);

        // transliterate
        $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);

        // remove unwanted characters
        $text = preg_replace('~[^-\w]+~', '', $text);

        // trim
        $text = trim($text, "-");

        // remove duplicate divider
        $text = preg_replace('~-+~', "-", $text);

        // lowercase
        $text = strtolower($text);

        if (empty($text)) {
            return 'n-a';
        }

        return $text;
    }

}