Merge pull request #1256 from mazesec/add-slugify-method

Add slugify Method to Str Class and toBeSlug Assertion to Expectation Class
This commit is contained in:
nuno maduro
2025-07-22 23:08:15 +01:00
committed by GitHub
3 changed files with 52 additions and 2 deletions

View File

@ -58,7 +58,7 @@ final class Str
{
$code = str_replace('_', '__', $code);
$code = self::PREFIX.str_replace(' ', '_', $code);
$code = self::PREFIX . str_replace(' ', '_', $code);
// sticks to PHP8.2 function naming rules https://www.php.net/manual/en/functions.user-defined.php
return (string) preg_replace('/[^a-zA-Z0-9_\x80-\xff]/', '_', $code);
@ -117,4 +117,13 @@ final class Str
{
return (bool) filter_var($value, FILTER_VALIDATE_URL);
}
/**
* Converts the given `$target` to a URL-friendly "slug".
*/
public static function slugify(string $target): string
{
$target = preg_replace('/[^a-zA-Z0-9]+/', '-', $target);
return strtolower(trim($target, '-'));
}
}