szurubooru/src/Helpers/TextHelper.php

193 lines
4.3 KiB
PHP
Raw Normal View History

2013-10-05 12:55:03 +02:00
<?php
class TextHelper
{
public static function isValidEmail($email)
{
$emailRegex = '/^[^@]+@[^@]+\.[^@]+$/';
return preg_match($emailRegex, $email);
}
public static function replaceTokens($text, array $tokens)
{
foreach ($tokens as $key => $value)
{
$token = '{' . $key . '}';
$text = str_replace($token, $value, $text);
}
return $text;
}
2013-10-06 13:21:16 +02:00
2013-10-07 23:17:33 +02:00
public static function kebabCaseToCamelCase($string)
{
$string = preg_split('/-/', $string);
$string = array_map('trim', $string);
$string = array_map('ucfirst', $string);
$string = join('', $string);
return $string;
}
public static function camelCaseToKebabCase($string)
{
$string = preg_replace_callback('/[A-Z]/', function($x)
{
return '-' . strtolower($x[0]);
}, $string);
$string = trim($string, '-');
return $string;
}
public static function camelCaseToHumanCase($string, $ucfirst = false)
{
$string = preg_replace_callback('/[A-Z]/', function($x)
{
return ' ' . strtolower($x[0]);
}, $string);
$string = trim($string);
if ($ucfirst)
$string = ucfirst($string);
return $string;
}
2013-10-06 13:21:16 +02:00
public static function resolveConstant($constantName, $className = null)
{
2013-10-07 23:17:33 +02:00
$constantName = self::kebabCaseToCamelCase($constantName);
2013-10-06 13:21:16 +02:00
//convert from kebab-case to CamelCase
if ($className !== null)
{
$constantName = $className . '::' . $constantName;
}
if (!defined($constantName))
{
throw new Exception('Undefined constant: ' . $constantName);
}
return constant($constantName);
}
2013-10-12 10:46:15 +02:00
2013-10-25 09:40:33 +02:00
private static function stripUnits($string, $base, $suffixes)
{
$suffix = substr($string, -1, 1);
$index = array_search($suffix, $suffixes);
if ($index === false)
return $string;
$number = intval($string);
for ($i = 0; $i < $index; $i ++)
$number *= $base;
return $number;
}
2013-10-12 10:46:15 +02:00
private static function useUnits($number, $base, $suffixes)
{
$suffix = array_shift($suffixes);
if ($number < $base)
{
return sprintf('%d%s', $number, $suffix);
}
do
{
$suffix = array_shift($suffixes);
$number /= (float) $base;
}
while ($number >= $base and !empty($suffixes));
return sprintf('%.01f%s', $number, $suffix);
}
public static function useBytesUnits($number)
{
return self::useUnits($number, 1024, ['B', 'K', 'M', 'G']);
}
public static function useDecimalUnits($number)
{
return self::useUnits($number, 1000, ['', 'K', 'M']);
}
2013-10-13 22:58:02 +02:00
2013-10-25 09:40:33 +02:00
public static function stripBytesUnits($string)
{
return self::stripUnits($string, 1024, ['B', 'K', 'M', 'G']);
}
public static function stripDecimalUnits($string)
{
return self::stripUnits($string, 1000, ['', 'K', 'M']);
}
2013-10-13 22:58:02 +02:00
public static function removeUnsafeKeys(&$input, $regex)
{
if (is_array($input))
{
foreach ($input as $key => $val)
{
if (preg_match($regex, $key))
unset($input[$key]);
else
self::removeUnsafeKeys($input[$key], $regex);
}
}
elseif (is_object($input))
{
foreach ($input as $key => $val)
{
if (preg_match($regex, $key))
unset($input->$key);
else
self::removeUnsafeKeys($input->$key, $regex);
}
}
}
public static function jsonEncode($obj, $illegalKeysRegex = '')
{
if (is_array($obj))
{
foreach ($obj as $key => $val)
{
if ($val instanceof RedBean_OODBBean)
{
$obj[$key] = R::exportAll($val);
}
}
}
elseif (is_object($obj))
{
foreach ($obj as $key => $val)
{
if ($val instanceof RedBean_OODBBean)
{
$obj->$key = R::exportAll($val);
}
}
}
if (!empty($illegalKeysRegex))
self::removeUnsafeKeys($obj, $illegalKeysRegex);
2013-10-13 23:02:57 +02:00
return json_encode($obj);
2013-10-13 22:58:02 +02:00
}
2013-10-19 15:19:47 +02:00
public static function parseMarkdown($text, $inline = false)
{
2013-10-19 15:19:47 +02:00
$output = CustomMarkdown::defaultTransform($text);
if ($inline)
$output = preg_replace('{</?p>}', '', $output);
return $output;
}
2013-10-22 11:40:10 +02:00
public static function encrypt($text)
{
$salt = \Chibi\Registry::getConfig()->main->salt;
$alg = MCRYPT_RIJNDAEL_256;
$mode = MCRYPT_MODE_ECB;
$iv = mcrypt_create_iv(mcrypt_get_iv_size($alg, $mode), MCRYPT_RAND);
return trim(base64_encode(mcrypt_encrypt($alg, $salt, $text, $mode, $iv)));
}
public static function decrypt($text)
{
$salt = \Chibi\Registry::getConfig()->main->salt;
$alg = MCRYPT_RIJNDAEL_256;
$mode = MCRYPT_MODE_ECB;
$iv = mcrypt_create_iv(mcrypt_get_iv_size($alg, $mode), MCRYPT_RAND);
return trim(mcrypt_decrypt($alg, $salt, base64_decode($text), $mode, $iv));
}
2013-10-05 12:55:03 +02:00
}