szurubooru/src/Helpers/TextHelper.php

54 lines
1.2 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;
}
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-05 12:55:03 +02:00
}