public static camel2id ( string $name, string $separator = '-', boolean | string $strict = false ) : string | ||
$name | string | the string to be converted |
$separator | string | the character used to concatenate the words in the ID |
$strict | boolean | string | whether to insert a separator between two consecutive uppercase chars, defaults to false |
return | string | the resulting ID |
public static function camel2id($name, $separator = '-', $strict = false)
{
$regex = $strict ? '/[A-Z]/' : '/(?<![A-Z])[A-Z]/';
if ($separator === '_') {
return trim(strtolower(preg_replace($regex, '_\\0', $name)), '_');
} else {
return trim(strtolower(str_replace('_', $separator, preg_replace($regex, $separator . '\\0', $name))), $separator);
}
}
/** * Retrieves i18n category for given key * --- * Finds i18n category based on config rule * or default category or model classname category * --- * @return string namespace */ public function getI18nCategory($key = false) { if ($key) { $rules = ArrayHelper::getValue(self::$generator->messages, $key); foreach ($rules as $regex => $ns) { $pattern = static::sanitazeNsRegex($regex); if (preg_match('%' . $pattern . '%', self::basename($this->getClass(self::RK_MODEL_CM)), $matches)) { return static::sanitazeI18n($ns, $matches); } } } if (self::$generator->messageCategory) { return self::$generator->messageCategory; } $basename = self::basename($this->getClass(self::RK_MODEL_CM)); if (static::$generator->isDbView()) { $prefix = ucfirst(str_replace('_', '', static::$generator->dbViewPrefix)); $basename = str_replace($prefix, '', $basename); } return \yii\helpers\BaseInflector::camel2id($basename, '/'); }