<?php

namespace App\Helpers;

class StringHelper {

    /**
     * Urlizes strings
     *
     * @param string $s input string
     * @return string slug / handle
     */
    public static function urlize(string $s): string
    {
        $s = utf8_decode($s);
        $s = self::convertHighAscii($s);
        $s = strtolower($s);
        $s = strip_tags($s);
        $s = preg_replace('!&[^;\s]+;!', '', $s);
        $s = preg_replace('!&[^;\s]+;!', '', $s);
        $s = preg_replace('![^\w\s-]!', '', $s);
        $s = preg_replace('![\s_]+!', '-', $s);
        $s = urlencode($s);
        return $s;
    }

    /**
     * Converts high ascii
     *
     * @param string $s
     * @return string
     */
    public static function convertHighAscii(string $s): string
    {
        $HighASCII = array(
            "!\xc0!" => 'A',    # A`
            "!\xe0!" => 'a',    # a`
            "!\xc1!" => 'A',    # A'
            "!\xe1!" => 'a',    # a'
            "!\xc2!" => 'A',    # A^
            "!\xe2!" => 'a',    # a^
            "!\xc4!" => 'Ae',   # A:
            "!\xe4!" => 'ae',   # a:
            "!\xc3!" => 'A',    # A~
            "!\xe3!" => 'a',    # a~
            "!\xc8!" => 'E',    # E`
            "!\xe8!" => 'e',    # e`
            "!\xc9!" => 'E',    # E'
            "!\xe9!" => 'e',    # e'
            "!\xca!" => 'E',    # E^
            "!\xea!" => 'e',    # e^
            "!\xcb!" => 'Ee',   # E:
            "!\xeb!" => 'ee',   # e:
            "!\xcc!" => 'I',    # I`
            "!\xec!" => 'i',    # i`
            "!\xcd!" => 'I',    # I'
            "!\xed!" => 'i',    # i'
            "!\xce!" => 'I',    # I^
            "!\xee!" => 'i',    # i^
            "!\xcf!" => 'Ie',   # I:
            "!\xef!" => 'ie',   # i:
            "!\xd2!" => 'O',    # O`
            "!\xf2!" => 'o',    # o`
            "!\xd3!" => 'O',    # O'
            "!\xf3!" => 'o',    # o'
            "!\xd4!" => 'O',    # O^
            "!\xf4!" => 'o',    # o^
            "!\xd6!" => 'Oe',   # O:
            "!\xf6!" => 'oe',   # o:
            "!\xd5!" => 'O',    # O~
            "!\xf5!" => 'o',    # o~
            "!\xd8!" => 'Oe',   # O/
            "!\xf8!" => 'oe',   # o/
            "!\xd9!" => 'U',    # U`
            "!\xf9!" => 'u',    # u`
            "!\xda!" => 'U',    # U'
            "!\xfa!" => 'u',    # u'
            "!\xdb!" => 'U',    # U^
            "!\xfb!" => 'u',    # u^
            "!\xdc!" => 'Ue',   # U:
            "!\xfc!" => 'ue',   # u:
            "!\xc7!" => 'C',    # ,C
            "!\xe7!" => 'c',    # ,c
            "!\xd1!" => 'N',    # N~
            "!\xf1!" => 'n',    # n~
            "!\xdf!" => 'ss'
        );
        $find = array_keys($HighASCII);
        $replace = array_values($HighASCII);
        $s = preg_replace($find, $replace, $s);
        return $s;
    }

    /**
     * Undocumented function
     *
     * @param string $gid
     * @return string
     */
    static function getIdFromGid(string $gid): int
    {
        if (empty($gid)) {
            return null;
        }
        $parts = explode('/', $gid);
        return $parts[sizeof($parts) - 1] * 1;
    }

    static function cleanWhiteSpaces($string = '')
    {
        return trim(preg_replace('/\s+/', ' ', $string));
    }
}
