Add Str::append() and Str::prepend()

This commit is contained in:
Giuseppe Criscione 2020-11-24 00:12:52 +01:00
parent 97f571ae60
commit ddefc5db9b

View File

@ -109,12 +109,28 @@ class Str
return preg_replace(['/^-|-$|[^a-z0-9-]/', '/-+/'], ['', '-'], strtr(strtolower($string), self::SLUG_TRANSLATE_MAP));
}
/**
* Append a suffix to a given string if missing
*/
public static function append(string $string, string $suffix): string
{
return static::endsWith($string, $suffix) ? $string : $string . $suffix;
}
/**
* Prepend a prefix to a given string if missing
*/
public static function prepend(string $string, string $prefix): string
{
return static::startsWith($string, $prefix) ? $string : $prefix . $string;
}
/**
* Wrap a string with another
*/
public static function wrap(string $string, string $wrap): string
{
return (static::startsWith($string, $wrap) ? '' : $wrap) . $string . (static::endsWith($string, $wrap) ? '' : $wrap);
return static::append(static::prepend($string, $wrap), $wrap);
}
/**