super-powers/src/a8935cbe-7701-40dc-bfd5-675.../code.power

54 lines
1.3 KiB
Plaintext

/**
* The field builder switch
*
* @since 3.0.9
*/
protected static $builder = false;
/**
* Making field type name safe
*
* @param String $string The you would like to make safe
* @param String $option The option for the component.
*
* @returns string on success
*
* @since 3.0.9
*/
public static function safe($string, $option = null)
{
// get global value
if (self::$builder === false)
{
self::$builder = Helper::getParams($option)->get('type_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, 0, 1)))
{
$string = StringHelper::numbers($string);
}
// Transliterate string
$string = StringHelper::transliterate($string);
// remove all and keep only characters and numbers and point (TODO just one point)
$string = trim(preg_replace("/[^A-Za-z0-9_\.]/", '', (string) $string));
// best is to return lower (for all string equality in compiler)
return strtolower($string);
}
// not a string
return '';
}
// use the default (original behaviour/convention)
return StringHelper::safe($string);
}