Composer Update

This commit is contained in:
danpros 2024-05-18 14:08:18 +07:00
commit 6163b18bf5
404 changed files with 35954 additions and 1177 deletions

View file

@ -1,300 +1,591 @@
<?php
/**
* A PHP port of URLify.js from the Django project
* (https://github.com/django/django/blob/master/django/contrib/admin/static/admin/js/urlify.js).
* Handles symbols from Latin languages, Greek, Turkish, Bulgarian, Russian,
* Ukrainian, Czech, Polish, Romanian, Latvian, Lithuanian, Vietnamese, Arabic,
* Serbian, Azerbaijani, Kazakh and Slovak. Symbols it cannot transliterate
* it will simply omit.
* A fast PHP slug generator and transliteration library, started as a PHP port of URLify.js
* from the Django project + fallback via "Portable ASCII".
*
* Usage:
* - https://github.com/django/django/blob/master/django/contrib/admin/static/admin/js/urlify.js
* - https://github.com/voku/portable-ascii
*
* echo URLify::filter (' J\'étudie le français ');
* // "jetudie-le-francais"
*
* echo URLify::filter ('Lo siento, no hablo español.');
* // "lo-siento-no-hablo-espanol"
* Handles symbols from latin languages, Arabic, Azerbaijani, Bulgarian, Burmese, Croatian, Czech, Danish, Esperanto,
* Estonian, Finnish, French, Switzerland (French), Austrian (French), Georgian, German, Switzerland (German),
* Austrian (German), Greek, Hindi, Kazakh, Latvian, Lithuanian, Norwegian, Persian, Polish, Romanian, Russian, Swedish,
* Serbian, Slovak, Turkish, Ukrainian and Vietnamese ... and many other via "ASCII::to_transliterate()".
*/
class URLify
{
public static $maps = array (
'de' => array ( /* German */
'Ä' => 'Ae', 'Ö' => 'Oe', 'Ü' => 'Ue', 'ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue', 'ß' => 'ss',
'ẞ' => 'SS'
),
'latin' => array (
'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A','Ă' => 'A', 'Æ' => 'AE', 'Ç' =>
'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I',
'Ï' => 'I', 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' =>
'O', 'Ő' => 'O', 'Ø' => 'O', 'Œ' => 'OE' ,'Ș' => 'S','Ț' => 'T', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ű' => 'U',
'Ý' => 'Y', 'Þ' => 'TH', 'ß' => 'ss', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' =>
'a', 'å' => 'a', 'ă' => 'a', 'æ' => 'ae', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e',
'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' =>
'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ő' => 'o', 'ø' => 'o', 'œ' => 'oe', 'ș' => 's', 'ț' => 't', 'ù' => 'u', 'ú' => 'u',
'û' => 'u', 'ü' => 'u', 'ű' => 'u', 'ý' => 'y', 'þ' => 'th', 'ÿ' => 'y'
),
'latin_symbols' => array (
'©' => '(c)'
),
'el' => array ( /* Greek */
'α' => 'a', 'β' => 'b', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'ζ' => 'z', 'η' => 'h', 'θ' => '8',
'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => '3', 'ο' => 'o', 'π' => 'p',
'ρ' => 'r', 'σ' => 's', 'τ' => 't', 'υ' => 'y', 'φ' => 'f', 'χ' => 'x', 'ψ' => 'ps', 'ω' => 'w',
'ά' => 'a', 'έ' => 'e', 'ί' => 'i', 'ό' => 'o', 'ύ' => 'y', 'ή' => 'h', 'ώ' => 'w', 'ς' => 's',
'ϊ' => 'i', 'ΰ' => 'y', 'ϋ' => 'y', 'ΐ' => 'i',
'Α' => 'A', 'Β' => 'B', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Ζ' => 'Z', 'Η' => 'H', 'Θ' => '8',
'Ι' => 'I', 'Κ' => 'K', 'Λ' => 'L', 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => '3', 'Ο' => 'O', 'Π' => 'P',
'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'Y', 'Φ' => 'F', 'Χ' => 'X', 'Ψ' => 'PS', 'Ω' => 'W',
'Ά' => 'A', 'Έ' => 'E', 'Ί' => 'I', 'Ό' => 'O', 'Ύ' => 'Y', 'Ή' => 'H', 'Ώ' => 'W', 'Ϊ' => 'I',
'Ϋ' => 'Y'
),
'tr' => array ( /* Turkish */
'ş' => 's', 'Ş' => 'S', 'ı' => 'i', 'İ' => 'I', 'ç' => 'c', 'Ç' => 'C', 'ü' => 'u', 'Ü' => 'U',
'ö' => 'o', 'Ö' => 'O', 'ğ' => 'g', 'Ğ' => 'G'
),
'bg' => array( /* Bulgarian */
'Щ' => 'Sht', 'Ш' => 'Sh', 'Ч' => 'Ch', 'Ц' => 'C', 'Ю' => 'Yu', 'Я' => 'Ya',
'Ж' => 'J', 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
'Е' => 'E', 'З' => 'Z', 'И' => 'I', 'Й' => 'Y', 'К' => 'K', 'Л' => 'L',
'М' => 'M', 'Н' => 'N', 'О' => 'O', 'П' => 'P', 'Р' => 'R', 'С' => 'S',
'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ь' => '', 'Ъ' => 'A',
'щ' => 'sht', 'ш' => 'sh', 'ч' => 'ch', 'ц' => 'c', 'ю' => 'yu', 'я' => 'ya',
'ж' => 'j', 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
'е' => 'e', 'з' => 'z', 'и' => 'i', 'й' => 'y', 'к' => 'k', 'л' => 'l',
'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's',
'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ь' => '', 'ъ' => 'a'
),
'ru' => array ( /* Russian */
'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo', 'ж' => 'zh',
'з' => 'z', 'и' => 'i', 'й' => 'i', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o',
'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c',
'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sh', 'ъ' => '', 'ы' => 'y', 'ь' => '', 'э' => 'e', 'ю' => 'yu',
'я' => 'ya',
'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'Yo', 'Ж' => 'Zh',
'З' => 'Z', 'И' => 'I', 'Й' => 'I', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O',
'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C',
'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sh', 'Ъ' => '', 'Ы' => 'Y', 'Ь' => '', 'Э' => 'E', 'Ю' => 'Yu',
'Я' => 'Ya',
'№' => ''
),
'uk' => array ( /* Ukrainian */
'Є' => 'Ye', 'І' => 'I', 'Ї' => 'Yi', 'Ґ' => 'G', 'є' => 'ye', 'і' => 'i', 'ї' => 'yi', 'ґ' => 'g'
),
'kk' => array ( /* Kazakh */
'Ә' => 'A', 'Ғ' => 'G', 'Қ' => 'Q', 'Ң' => 'N', 'Ө' => 'O', 'Ұ' => 'U', 'Ү' => 'U', 'Һ' => 'H',
'ә' => 'a', 'ғ' => 'g', 'қ' => 'q', 'ң' => 'n', 'ө' => 'o', 'ұ' => 'u', 'ү' => 'u', 'һ' => 'h',
),
'cs' => array ( /* Czech */
'č' => 'c', 'ď' => 'd', 'ě' => 'e', 'ň' => 'n', 'ř' => 'r', 'š' => 's', 'ť' => 't', 'ů' => 'u',
'ž' => 'z', 'Č' => 'C', 'Ď' => 'D', 'Ě' => 'E', 'Ň' => 'N', 'Ř' => 'R', 'Š' => 'S', 'Ť' => 'T',
'Ů' => 'U', 'Ž' => 'Z'
),
'pl' => array ( /* Polish */
'ą' => 'a', 'ć' => 'c', 'ę' => 'e', 'ł' => 'l', 'ń' => 'n', 'ó' => 'o', 'ś' => 's', 'ź' => 'z',
'ż' => 'z', 'Ą' => 'A', 'Ć' => 'C', 'Ę' => 'e', 'Ł' => 'L', 'Ń' => 'N', 'Ó' => 'O', 'Ś' => 'S',
'Ź' => 'Z', 'Ż' => 'Z'
),
'ro' => array ( /* Romanian */
'ă' => 'a', 'â' => 'a', 'î' => 'i', 'ș' => 's', 'ț' => 't', 'Ţ' => 'T', 'ţ' => 't'
),
'lv' => array ( /* Latvian */
'ā' => 'a', 'č' => 'c', 'ē' => 'e', 'ģ' => 'g', 'ī' => 'i', 'ķ' => 'k', 'ļ' => 'l', 'ņ' => 'n',
'š' => 's', 'ū' => 'u', 'ž' => 'z', 'Ā' => 'A', 'Č' => 'C', 'Ē' => 'E', 'Ģ' => 'G', 'Ī' => 'i',
'Ķ' => 'k', 'Ļ' => 'L', 'Ņ' => 'N', 'Š' => 'S', 'Ū' => 'u', 'Ž' => 'Z'
),
'lt' => array ( /* Lithuanian */
'ą' => 'a', 'č' => 'c', 'ę' => 'e', 'ė' => 'e', 'į' => 'i', 'š' => 's', 'ų' => 'u', 'ū' => 'u', 'ž' => 'z',
'Ą' => 'A', 'Č' => 'C', 'Ę' => 'E', 'Ė' => 'E', 'Į' => 'I', 'Š' => 'S', 'Ų' => 'U', 'Ū' => 'U', 'Ž' => 'Z'
),
'vi' => array ( /* Vietnamese */
'Á' => 'A', 'À' => 'A', 'Ả' => 'A', 'Ã' => 'A', 'Ạ' => 'A', 'Ă' => 'A', 'Ắ' => 'A', 'Ằ' => 'A', 'Ẳ' => 'A', 'Ẵ' => 'A', 'Ặ' => 'A', 'Â' => 'A', 'Ấ' => 'A', 'Ầ' => 'A', 'Ẩ' => 'A', 'Ẫ' => 'A', 'Ậ' => 'A',
'á' => 'a', 'à' => 'a', 'ả' => 'a', 'ã' => 'a', 'ạ' => 'a', 'ă' => 'a', 'ắ' => 'a', 'ằ' => 'a', 'ẳ' => 'a', 'ẵ' => 'a', 'ặ' => 'a', 'â' => 'a', 'ấ' => 'a', 'ầ' => 'a', 'ẩ' => 'a', 'ẫ' => 'a', 'ậ' => 'a',
'É' => 'E', 'È' => 'E', 'Ẻ' => 'E', 'Ẽ' => 'E', 'Ẹ' => 'E', 'Ê' => 'E', 'Ế' => 'E', 'Ề' => 'E', 'Ể' => 'E', 'Ễ' => 'E', 'Ệ' => 'E',
'é' => 'e', 'è' => 'e', 'ẻ' => 'e', 'ẽ' => 'e', 'ẹ' => 'e', 'ê' => 'e', 'ế' => 'e', 'ề' => 'e', 'ể' => 'e', 'ễ' => 'e', 'ệ' => 'e',
'Í' => 'I', 'Ì' => 'I', 'Ỉ' => 'I', 'Ĩ' => 'I', 'Ị' => 'I', 'í' => 'i', 'ì' => 'i', 'ỉ' => 'i', 'ĩ' => 'i', 'ị' => 'i',
'Ó' => 'O', 'Ò' => 'O', 'Ỏ' => 'O', 'Õ' => 'O', 'Ọ' => 'O', 'Ô' => 'O', 'Ố' => 'O', 'Ồ' => 'O', 'Ổ' => 'O', 'Ỗ' => 'O', 'Ộ' => 'O', 'Ơ' => 'O', 'Ớ' => 'O', 'Ờ' => 'O', 'Ở' => 'O', 'Ỡ' => 'O', 'Ợ' => 'O',
'ó' => 'o', 'ò' => 'o', 'ỏ' => 'o', 'õ' => 'o', 'ọ' => 'o', 'ô' => 'o', 'ố' => 'o', 'ồ' => 'o', 'ổ' => 'o', 'ỗ' => 'o', 'ộ' => 'o', 'ơ' => 'o', 'ớ' => 'o', 'ờ' => 'o', 'ở' => 'o', 'ỡ' => 'o', 'ợ' => 'o',
'Ú' => 'U', 'Ù' => 'U', 'Ủ' => 'U', 'Ũ' => 'U', 'Ụ' => 'U', 'Ư' => 'U', 'Ứ' => 'U', 'Ừ' => 'U', 'Ử' => 'U', 'Ữ' => 'U', 'Ự' => 'U',
'ú' => 'u', 'ù' => 'u', 'ủ' => 'u', 'ũ' => 'u', 'ụ' => 'u', 'ư' => 'u', 'ứ' => 'u', 'ừ' => 'u', 'ử' => 'u', 'ữ' => 'u', 'ự' => 'u',
'Ý' => 'Y', 'Ỳ' => 'Y', 'Ỷ' => 'Y', 'Ỹ' => 'Y', 'Ỵ' => 'Y', 'ý' => 'y', 'ỳ' => 'y', 'ỷ' => 'y', 'ỹ' => 'y', 'ỵ' => 'y',
'Đ' => 'D', 'đ' => 'd'
),
'ar' => array ( /* Arabic */
'أ' => 'a', 'ب' => 'b', 'ت' => 't', 'ث' => 'th', 'ج' => 'g', 'ح' => 'h', 'خ' => 'kh', 'د' => 'd',
'ذ' => 'th', 'ر' => 'r', 'ز' => 'z', 'س' => 's', 'ش' => 'sh', 'ص' => 's', 'ض' => 'd', 'ط' => 't',
'ظ' => 'th', 'ع' => 'aa', 'غ' => 'gh', 'ف' => 'f', 'ق' => 'k', 'ك' => 'k', 'ل' => 'l', 'م' => 'm',
'ن' => 'n', 'ه' => 'h', 'و' => 'o', 'ي' => 'y',
'ا' => 'a', 'إ' => 'a', 'آ' => 'a', 'ؤ' => 'o', 'ئ' => 'y', 'ء' => 'aa',
'٠' => '0', '١' => '1', '٢' => '2', '٣' => '3', '٤' => '4', '٥' => '5', '٦' => '6', '٧' => '7', '٨' => '8', '٩' => '9',
),
'fa' => array ( /* Persian */
'گ' => 'g', 'ژ' => 'j', 'پ' => 'p', 'چ' => 'ch', 'ی' => 'y', 'ک' => 'k',
'۰' => '0', '۱' => '1', '۲' => '2', '۳' => '3', '۴' => '4', '۵' => '5', '۶' => '6', '۷' => '7', '۸' => '8', '۹' => '9',
),
'sr' => array ( /* Serbian */
'ђ' => 'dj', 'ј' => 'j', 'љ' => 'lj', 'њ' => 'nj', 'ћ' => 'c', 'џ' => 'dz', 'đ' => 'dj',
'Ђ' => 'Dj', 'Ј' => 'j', 'Љ' => 'Lj', 'Њ' => 'Nj', 'Ћ' => 'C', 'Џ' => 'Dz', 'Đ' => 'Dj'
),
'az' => array ( /* Azerbaijani */
'ç' => 'c', 'ə' => 'e', 'ğ' => 'g', 'ı' => 'i', 'ö' => 'o', 'ş' => 's', 'ü' => 'u',
'Ç' => 'C', 'Ə' => 'E', 'Ğ' => 'G', 'İ' => 'I', 'Ö' => 'O', 'Ş' => 'S', 'Ü' => 'U'
),
'sk' => array ( /* Slovak */
'ĺ' => 'l', 'ľ' => 'l', 'ŕ' => 'r'
)
);
/**
* The language-mapping array.
*
* ISO 639-1 codes: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
*
* @var array[]
*/
public static $maps = [];
/**
* List of words to remove from URLs.
*/
public static $remove_list = array (
'a', 'an', 'as', 'at', 'before', 'but', 'by', 'for', 'from',
'is', 'in', 'into', 'like', 'of', 'off', 'on', 'onto', 'per',
'since', 'than', 'the', 'this', 'that', 'to', 'up', 'via',
'with'
);
/**
* List of words to remove from URLs.
*
* @var array[]
*/
public static $remove_list = [];
/**
* The character map.
*/
private static $map = array ();
/**
* An array of strings that will convert into the separator-char - used by "URLify::filter()".
*
* @var string[]
*/
private static $arrayToSeparator = [];
/**
* The character list as a string.
*/
private static $chars = '';
/**
* The character list as a regular expression.
*/
private static $regex = '';
/**
* The current language
*/
private static $language = '';
/**
* Initializes the character map.
* @param string $language
*/
private static function init ($language = "")
/**
* Add new strings the will be replaced with the separator.
*
* @param array $array <p>An array of things that should replaced by the separator.</p>
* @param bool $merge <p>Keep the previous (default) array-to-separator array.</p>
*
* @return void
*
* @psalm-param string[] $array
*/
public static function add_array_to_separator(array $array, bool $merge = true)
{
if (count (self::$map) > 0 && (($language == "") || ($language == self::$language))) {
return;
}
if ($merge === true) {
self::$arrayToSeparator = \array_unique(
\array_merge(
self::$arrayToSeparator,
$array
)
);
} else {
self::$arrayToSeparator = $array;
}
}
/* Is a specific map associated with $language ? */
if (isset(self::$maps[$language]) && is_array(self::$maps[$language])) {
/* Move this map to end. This means it will have priority over others */
$m = self::$maps[$language];
unset(self::$maps[$language]);
self::$maps[$language] = $m;
}
/* Reset static vars */
self::$language = $language;
self::$map = array();
self::$chars = '';
foreach (self::$maps as $map) {
foreach ($map as $orig => $conv) {
self::$map[$orig] = $conv;
self::$chars .= $orig;
}
}
self::$regex = '/[' . preg_quote(self::$chars, '/') . ']/u';
}
/**
* Add new characters to the list. `$map` should be a hash.
* @param array $map
*/
public static function add_chars ($map)
/**
* Add new characters to the list. `$map` should be a hash.
*
* @param array $map
* @param string|null $language
*
* @return void
*
* @psalm-param array<string, string> $map
*/
public static function add_chars(array $map, string $language = null)
{
if (! is_array ($map)) {
throw new LogicException ('$map must be an associative array.');
}
self::$maps[] = $map;
self::$map = array ();
self::$chars = '';
}
$language_key = $language ?? \uniqid('urlify', true);
/**
* Append words to the remove list. Accepts either single words
* or an array of words.
* @param mixed $words
*/
public static function remove_words ($words)
if (isset(self::$maps[$language_key])) {
self::$maps[$language_key] = \array_merge($map, self::$maps[$language_key]);
} else {
self::$maps[$language_key] = $map;
}
}
/**
* @return void
*/
public static function reset_chars()
{
$words = is_array ($words) ? $words : array ($words);
self::$remove_list = array_unique (array_merge (self::$remove_list, $words));
}
self::$maps = [];
}
/**
* Transliterates characters to their ASCII equivalents.
/**
* Transliterates characters to their ASCII equivalents.
* $language specifies a priority for a specific language.
* The latter is useful if languages have different rules for the same character.
* @param string $text
*
* @param string $string <p>The input string.</p>
* @param string $language <p>Your primary language.</p>
* @param string $unknown <p>Character use if character unknown. (default is ?).</p>
*
* @return string
*/
public static function downcode(
string $string,
string $language = 'en',
string $unknown = ''
): string {
$string = self::expandString($string, $language);
foreach (self::$maps as $mapsInner) {
foreach ($mapsInner as $orig => $replace) {
$string = \str_replace($orig, $replace, $string);
}
}
$string = \voku\helper\ASCII::to_ascii(
$string,
$language,
false,
true
);
return \voku\helper\ASCII::to_transliterate(
$string,
$unknown,
false
);
}
/**
* Convert a String to URL slug. Wraps <strong>filter()</strong> with a simpler
* set of defaults for typical usage in generating blog post slugs.
*
* @param string $string <p>The text you want to convert.</p>
* @param int $maxLength <p>Max. length of the output string, set to "0" (zero) to
* disable it</p>
* @param string $separator <p>Define a new separator for the words.</p>
* @param string $language <p>The language you want to convert to.</p>
*/
public static function slug(
string $string,
int $maxLength = 200,
string $separator = '-',
string $language = 'en'
): string {
return self::filter ($string, $maxLength, $language, false, false, true, $separator);
}
/**
* Convert a String to URL.
*
* e.g.: "Petty<br>theft" to "Petty-theft"
*
* @param string $string <p>The text you want to convert.</p>
* @param int $maxLength <p>Max. length of the output string, set to "0" (zero) to
* disable it</p>
* @param string $language <p>The language you want to convert to.</p>
* @param bool $fileName <p>
* Keep the "." from the extension e.g.: "imaäe.jpg" =>
* "image.jpg"
* </p>
* @param bool $removeWords <p>
* Remove some "words" from the string.<br />
* Info: Set extra words via <strong>remove_words()</strong>.
* </p>
* @param bool $strToLower <p>Use <strong>strtolower()</strong> at the end.</p>
* @param bool|string $separator <p>Define a new separator for the words.</p>
*
* @return string
*/
public static function filter(
string $string,
int $maxLength = 200,
string $language = 'en',
bool $fileName = false,
bool $removeWords = false,
bool $strToLower = true,
$separator = '-'
): string {
if ($string === '') {
return '';
}
// fallback
if ($language === '') {
$language = 'en';
}
// separator-fallback
if ($separator === false) {
$separator = '_';
}
if ($separator === true || $separator === '') {
$separator = '-';
}
// escaped separator
$separatorEscaped = \preg_quote($separator, '/');
// use defaults, if there are no values
if (self::$arrayToSeparator === []) {
self::reset_array_to_separator();
}
// remove apostrophes which are not used as quotes around a string
if (\strpos($string, "'") !== false) {
$stringTmp = \preg_replace("/(\w)'(\w)/u", '${1}${2}', $string);
if ($stringTmp !== null) {
$string = (string) $stringTmp;
}
}
// replace with $separator
$string = (string) \preg_replace(
self::$arrayToSeparator,
$separator,
$string
);
// remove all other html-tags
if (
\strpos($string, '<') !== false
||
\strpos($string, '>') !== false
) {
$string = \strip_tags($string);
}
// use special language replacer
$string = self::downcode($string, $language);
// replace with $separator, again
$string = (string) \preg_replace(
self::$arrayToSeparator,
$separator,
$string
);
// remove all these words from the string before urlifying
$removeWordsSearch = '//';
if ($removeWords === true) {
$removeList = self::get_remove_list($language);
if ($removeList !== []) {
$removeWordsSearch = '/\b(?:' . \implode('|', $removeList) . ')\b/ui';
}
}
// keep the "." from e.g.: a file-extension?
if ($fileName) {
$removePatternAddOn = '.';
} else {
$removePatternAddOn = '';
}
$string = (string) \preg_replace(
[
// 1) remove un-needed chars
'/[^' . $separatorEscaped . $removePatternAddOn . '\-a-zA-Z0-9\s]/u',
// 2) convert spaces to $separator
'/[\s]+/u',
// 3) remove some extras words
$removeWordsSearch,
// 4) remove double $separator's
'/[' . ($separatorEscaped ?: ' ') . ']+/u',
// 5) remove $separator at the end
'/[' . ($separatorEscaped ?: ' ') . ']+$/u',
],
[
'',
$separator,
'',
$separator,
'',
],
$string
);
// "substr" only if "$length" is set
if (
$maxLength
&&
$maxLength > 0
&&
\strlen($string) > $maxLength
) {
$string = (string) \substr(\trim($string, $separator), 0, $maxLength);
}
// convert to lowercase
if ($strToLower === true) {
$string = \strtolower($string);
}
// trim "$separator" from beginning and end of the string
return \trim($string, $separator);
}
/**
* Append words to the remove list. Accepts either single words or an array of words.
*
* @param string|string[] $words
* @param string $language
* @param bool $merge <p>Keep the previous (default) remove-words array.</p>
*
* @return void
*/
public static function remove_words($words, string $language = 'en', bool $merge = true)
{
if (\is_array($words) === false) {
$words = [$words];
}
foreach ($words as $removeWordKey => $removeWord) {
$words[$removeWordKey] = \preg_quote($removeWord, '/');
}
if ($merge === true) {
self::$remove_list[$language] = \array_unique(
\array_merge(
self::get_remove_list($language),
$words
)
);
} else {
self::$remove_list[$language] = $words;
}
}
/**
* Reset the internal "self::$arrayToSeparator" to the default values.
*
* @return void
*/
public static function reset_array_to_separator()
{
self::$arrayToSeparator = [
'/&quot;|&amp;|&lt;|&gt;|&ndash;|&mdash;/i', // ", &, <, >, , —
'/⁻|-|—|_|"|`|´|\'/',
"#/\r\n|\r|\n|<br.*/?>#isU",
];
}
/**
* reset the word-remove-array
*
* @param string $language
*
* @return void
*/
public static function reset_remove_list(string $language = 'en')
{
if ($language === '') {
return;
}
$language_orig = $language;
$language = self::get_language_for_reset_remove_list($language);
if ($language === '') {
return;
}
$stopWords = new \voku\helper\StopWords();
try {
self::$remove_list[$language_orig] = $stopWords->getStopWordsFromLanguage($language);
} catch (\voku\helper\StopWordsLanguageNotExists $e) {
self::$remove_list[$language_orig] = [];
}
}
/**
* Alias of `URLify::downcode()`.
*
* @param string $string
* @param string $language
*
* @return string
*/
public static function downcode ($text, $language = "")
*/
public static function transliterate(string $string, string $language = 'en'): string
{
self::init ($language);
return self::downcode($string, $language);
}
if (preg_match_all (self::$regex, $text, $matches)) {
for ($i = 0; $i < count ($matches[0]); $i++) {
$char = $matches[0][$i];
if (isset (self::$map[$char])) {
$text = str_replace ($char, self::$map[$char], $text);
}
}
}
return $text;
}
/**
* Expands the given string replacing some special parts for words.
* e.g. "lorem@ipsum.com" is replaced by "lorem at ipsum dot com".
*
* Most of these transformations have been inspired by the pelle/slugger
* project, distributed under the Eclipse Public License.
* Copyright 2012 Pelle Braendgaard
*
* @param string $string The string to expand
* @param string $language
*
* @return string The result of expanding the string
*/
protected static function expandString(string $string, string $language = 'en'): string
{
$string = self::expandCurrencies($string, $language);
/**
* Filters a string, e.g., "Petty theft" to "petty-theft"
* @param string $text The text to return filtered
* @param int $length The length (after filtering) of the string to be returned
* @param string $language The transliteration language, passed down to downcode()
* @param bool $file_name Whether there should be and additional filter considering this is a filename
* @param bool $use_remove_list Whether you want to remove specific elements previously set in self::$remove_list
* @param bool $lower_case Whether you want the filter to maintain casing or lowercase everything (default)
* @param bool $treat_underscore_as_space Treat underscore as space, so it will replaced with "-"
return self::expandSymbols($string, $language);
}
/**
* @param string $language
*
* @return string
*/
public static function filter ($text, $length = 60, $language = "", $file_name = false, $use_remove_list = true, $lower_case = true, $treat_underscore_as_space = true)
*/
private static function get_language_for_reset_remove_list(string $language)
{
$text = self::downcode ($text,$language);
if ($language === '') {
return '';
}
if ($use_remove_list) {
// remove all these words from the string before urlifying
$text = preg_replace ('/\b(' . join ('|', self::$remove_list) . ')\b/i', '', $text);
}
if (
\strpos($language, '_') === false
&&
\strpos($language, '-') === false
) {
$language = \strtolower($language);
} else {
$regex = '/(?<first>[a-z]{2}).*/i';
$language = \strtolower((string) \preg_replace($regex, '$1', $language));
}
// if downcode doesn't hit, the char will be stripped here
$remove_pattern = ($file_name) ? '/[^_\-.\-a-zA-Z0-9\s]/u' : '/[^\s_\-a-zA-Z0-9]/u';
$text = preg_replace ($remove_pattern, '', $text); // remove unneeded chars
if ($treat_underscore_as_space) {
$text = str_replace ('_', ' ', $text); // treat underscores as spaces
}
$text = preg_replace ('/^\s+|\s+$/u', '', $text); // trim leading/trailing spaces
$text = preg_replace ('/[-\s]+/u', '-', $text); // convert spaces to hyphens
if ($lower_case) {
$text = strtolower ($text); // convert to lowercase
}
return $language;
}
return trim (substr ($text, 0, $length), '-'); // trim to first $length chars
}
/**
* Alias of `URLify::downcode()`.
*/
public static function transliterate ($text)
/**
* Expands the numeric currencies in euros, dollars, pounds
* and yens that the given string may include.
*
* @param string $string
* @param string $language
*
* @return string
*/
private static function expandCurrencies(string $string, string $language = 'en')
{
return self::downcode ($text);
}
if (
\strpos($string, '€') === false
&&
\strpos($string, '$') === false
&&
\strpos($string, '£') === false
&&
\strpos($string, '¥') === false
) {
return $string;
}
if ($language === 'de') {
return (string) \preg_replace(
[
'/(?:\s|^)(\d+)(?: )*€(?:\s|$)/',
'/(?:\s|^)\$(?: )*(\d+)(?:\s|$)/',
'/(?:\s|^)£(?: )*(\d+)(?:\s|$)/',
'/(?:\s|^)¥(?: )*(\d+)(?:\s|$)/',
'/(?:\s|^)(\d+)[.|,](\d+)(?: )*€(?:\s|$)/',
'/(?:\s|^)\$(?: )*(\d+)[.|,](\d+)(?:\s|$)/',
'/(?:\s|^)£(?: )*(\d+)[.|,](\d+)(?:\s|$)/',
],
[
' \1 Euro ',
' \1 Dollar ',
' \1 Pound ',
' \1 Yen ',
' \1 Euro \2 Cent ',
' \1 Dollar \2 Cent ',
' \1 Pound \2 Pence ',
],
$string
);
}
return (string) \preg_replace(
[
'/(?:\s|^)1(?: )*€(?:\s|$)/',
'/(?:\s|^)(\d+)(?: )*€(?:\s|$)/',
'/(?:\s|^)\$(?: )*1(?:\s|$)/',
'/(?:\s|^)\$(?: )*(\d+)(?:\s|$)/',
'/(?:\s|^)£(?: )*1(?:\s|$)/',
'/(?:\s|^)£(?: )*(\d+)(?:\s|$)/',
'/(?:\s|^)¥(?: )*(\d+)(?:\s|$)/',
'/(?:\s|^)1[.|,](\d+)(?: )*€(?:\s|$)/',
'/(?:\s|^)(\d+)[.|,](\d+)(?: )*€(?:\s|$)/',
'/(?:\s|^)1[.|,](\d+)(?: )*$(?:\s|$)/',
'/(?:\s|^)\$(?: )*(\d+)[.|,](\d+)(?:\s|$)/',
'/(?:\s|^)1[.|,](\d+)(?: )*£(?:\s|$)/',
'/(?:\s|^)£(?: )*(\d+)[.|,](\d+)(?:\s|$)/',
],
[
' 1 Euro ',
' \1 Euros ',
' 1 Dollar ',
' \1 Dollars ',
' 1 Pound ',
' \1 Pounds ',
' \1 Yen ',
' 1 Euros \1 Cents ',
' \1 Euros \2 Cents ',
' 1 Dollars \1 Cents ',
' \1 Dollars \2 Cents ',
' 1 Pounds \1 Pence ',
' \1 Pounds \2 Pence ',
],
$string
);
}
/**
* Expands the special symbols that the given string may include, such as '@', '.', '#' and '%'.
*
* @param string $string
* @param string $language
*
* @return string
*/
private static function expandSymbols(string $string, string $language = 'en')
{
if (
\strpos($string, '©') === false
&&
\strpos($string, '®') === false
&&
\strpos($string, '@') === false
&&
\strpos($string, '&') === false
&&
\strpos($string, '%') === false
&&
\strpos($string, '=') === false
) {
return $string;
}
$maps = \voku\helper\ASCII::charsArray(true);
return (string) \preg_replace(
[
'/\s*©\s*/',
'/\s*®\s*/',
'/\s*@\s*/',
'/\s*&\s*/',
'/\s*%\s*/',
'/(\s*=\s*)/',
],
[
$maps['latin_symbols']['©'],
$maps['latin_symbols']['®'],
$maps['latin_symbols']['@'],
$maps[$language]['&'] ?? '&',
$maps[$language]['%'] ?? '%',
$maps[$language]['='] ?? '=',
],
$string
);
}
/**
* return the "self::$remove_list[$language]" array
*
* @param string $language
*
* @return array<mixed>
*/
private static function get_remove_list(string $language = 'en')
{
// check for language
if ($language === '') {
return [];
}
// set remove-array
if (!isset(self::$remove_list[$language])) {
self::reset_remove_list($language);
}
// check for array
if (
!isset(self::$remove_list[$language])
||
empty(self::$remove_list[$language])
) {
return [];
}
return self::$remove_list[$language];
}
}