<?php
namespace Refinery29\Core;
final class Util
{
const R29STATIC_DOMAIN = 's%d.r29static.com';
/**
* @param string $text
* @param string $spacer
*
* @return array|mixed|string
*/
public static function tagify($text, $spacer = '-')
{
//Handle if array
if (is_array($text)) {
return array_map("self::tagify", $text);
}
$text = self::decodeHtml($text);
$text = self::normalizeStr($text);
$text = str_replace('&', 'and', $text);
return str_replace(' ', $spacer, trim(preg_replace('#([^a-z0-9/]+)#', ' ', strtolower($text))));
}
/**
* @param string $text
*
* @return string
*/
public static function decodeHtml($text)
{
return html_entity_decode($text, ENT_QUOTES, 'UTF-8');
}
/**
* @param string $text
*
* @return string
*/
public static function normalizeStr($text)
{
$map = [
'Š' => 'S', 'š' => 's', 'Ð' => 'Dj','Ž' => 'Z', 'ž' => 'z', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A',
'Å' => 'A', 'Æ' => 'A', 'Ç' => 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I',
'Ï' => 'I', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U',
'Û' => 'U', 'Ü' => 'U', 'Ý' => 'Y', 'Þ' => 'B', 'ß' => 'Ss','à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a',
'å' => 'a', 'æ' => 'a', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i',
'ï' => 'i', 'ð' => 'o', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o', 'ù' => 'u',
'ú' => 'u', 'û' => 'u', 'ý' => 'y', 'ý' => 'y', 'þ' => 'b', 'ÿ' => 'y', 'ƒ' => 'f',
];
return strtr($text, $map);
}
/**
* @param $path
* @param mixed|string $app
* @param string $scheme
*
* @return string
*/
public static function r29StaticAlias($path, $app = APP_NAME, $scheme = null)
{
// there is no bin app, bin is passed for images served by the cms
if ($app === 'bin') {
$path = preg_replace('#^/static/#', '/', $path);
} else {
$path = sprintf('%s/%s/%s', $app, rev(), $path);
}
$domain = sprintf(Util::R29STATIC_DOMAIN, Util::hashmod($path, 3) + 1);
$url = sprintf('//%s/%s', $domain, $path);
if (! empty($scheme) && strpos($url, 'http') !== 0) {
$url = sprintf('%s:%s', $scheme, $url);
}
return $url;
}
/**
* @param $str
* @param $max
*
* @return int
*/
public static function hashmod($str, $max)
{
return crc32($str) % $max;
}
}
var_dump(Refinery29\Core\Util::tagify(['sethomg john', 'yolo oko']));
preferences:
58.58 ms | 402 KiB | 5 Q