super-powers/src/9ef0eb24-aae4-4f5a-99af-d724db44808f/code.power

75 lines
1.8 KiB
Plaintext
Raw Normal View History

2023-04-15 14:49:41 +00:00
/**
* The field builder switch
*
* @since 3.0.9
*/
protected static $builder = false;
/**
* Making field names safe
*
* @input string The string you would like to make safe
* @input boolean The switch to return an ALL UPPER CASE string
* @input string The string to use in white space
*
* @returns string on success
*
* @since 3.0.9
*/
public static function safe($string, $allcap = false, $spacer = '_')
{
// get global value
if (self::$builder === false)
{
self::$builder = Helper::getParams()->get('field_name_builder', 1);
}
// use the new convention
if (2 == self::$builder)
{
// 0nly continue if we have a string
if (StringHelper::check($string))
{
// check that the first character is not a number
if (is_numeric(substr((string)$string, 0, 1)))
{
$string = StringHelper::numbers($string);
}
// remove all other strange characters
$string = trim((string) $string);
$string = preg_replace('/'.$spacer.'+/', ' ', $string);
$string = preg_replace('/\s+/', ' ', $string);
// Transliterate string
$string = StringHelper::transliterate($string);
// remove all and keep only characters and numbers
$string = preg_replace("/[^A-Za-z0-9 ]/", '', (string) $string);
// replace white space with underscore (SAFEST OPTION)
$string = preg_replace('/\s+/', (string) $spacer, $string);
// return all caps
if ($allcap)
{
return strtoupper($string);
}
// default is to return lower
return strtolower($string);
}
// not a string
return '';
}
// return all caps
if ($allcap)
{
return StringHelper::safe($string, 'U');
}
// use the default (original behavior/convention)
return StringHelper::safe($string);
}