Skip to content

Commit

Permalink
add support for intl extension
Browse files Browse the repository at this point in the history
  • Loading branch information
mindplay-dk committed Aug 11, 2016
1 parent dc2b01c commit a21db43
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"mindplay/testies": "dev-master",
"phpunit/php-code-coverage": "^2.2.4"
},
"suggest": {
"ext-intl": "provides better/faster support for slug-creation in URL helpers"
},
"autoload": {
"psr-4": {
"mindplay\\timber\\": "src/"
Expand Down
31 changes: 20 additions & 11 deletions src/UrlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,26 @@ protected function str($value)
protected function slug($value, $max_length = null) {
$string = $this->str($value);

static $latin1 = [
// https://github.com/jbroadway/urlify
'À' => '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'
];

$clean = str_replace(array_keys($latin1), array_values($latin1), $string);
$clean = mb_strtolower($clean, 'UTF-8');
if (function_exists('transliterator_transliterate')) {
// use intl extension where available:

$clean = transliterator_transliterate('Any-Latin; Latin-ASCII; Lower()', $string);
} else {
// use a less capable fallback-function with support for basic latin characters only:

static $latin1 = [
// https://github.com/jbroadway/urlify
'À' => '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'
];

$clean = str_replace(array_keys($latin1), array_values($latin1), $string);
$clean = mb_strtolower($clean, 'UTF-8');
}

$clean = preg_replace('/[^a-zA-Z0-9]+/u', '-', $clean); // reduce disallowed char ranges to dashes
$clean = preg_replace('/[^a-zA-Z0-9-]+/u', '', $clean); // remove any remaining disallowed chars
$clean = preg_replace('/(^-+|-+$)/', '', $clean); // strip leading/trailing dashes
Expand Down

0 comments on commit a21db43

Please sign in to comment.