mirror of
https://github.com/pestphp/pest.git
synced 2026-03-13 11:17:22 +01:00
Add slugify method
This commit is contained in:
@ -1159,4 +1159,21 @@ final class Expectation
|
|||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that the value can be converted to a slug
|
||||||
|
*
|
||||||
|
* @return self<TValue>
|
||||||
|
*/
|
||||||
|
public function toBeSlug(string $message = ''): self
|
||||||
|
{
|
||||||
|
if ($message === '') {
|
||||||
|
$message = "Failed asserting that {$this->value} can be converted to a slug.";
|
||||||
|
}
|
||||||
|
|
||||||
|
$slug = Str::slugify((string) $this->value);
|
||||||
|
Assert::assertNotEmpty($slug, $message);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -116,4 +116,13 @@ final class Str
|
|||||||
{
|
{
|
||||||
return (bool) filter_var($value, FILTER_VALIDATE_URL);
|
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, '-'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user