mirror of
https://github.com/pestphp/pest.git
synced 2026-03-07 00:07:22 +01:00
52 lines
1.3 KiB
PHP
52 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pest\Support;
|
|
|
|
/**
|
|
* @internal
|
|
*/
|
|
final class Str
|
|
{
|
|
/**
|
|
* Pool of alpha-numeric characters for generating (unsafe) random strings
|
|
* from.
|
|
*/
|
|
private const POOL = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
|
|
/**
|
|
* Create a (unsecure & non-cryptographically safe) random alpha-numeric
|
|
* string value.
|
|
*
|
|
* @param int $length the length of the resulting randomized string
|
|
*
|
|
* @see https://github.com/laravel/framework/blob/4.2/src/Illuminate/Support/Str.php#L240-L242
|
|
*/
|
|
public static function random(int $length = 16): string
|
|
{
|
|
return substr(str_shuffle(str_repeat(self::POOL, 5)), 0, $length);
|
|
}
|
|
|
|
/**
|
|
* Checks if the given `$target` starts with the given `$search`.
|
|
*/
|
|
public static function startsWith(string $target, string $search): bool
|
|
{
|
|
return substr($target, 0, strlen($search)) === $search;
|
|
}
|
|
|
|
/**
|
|
* Checks if the given `$target` ends with the given `$search`.
|
|
*/
|
|
public static function endsWith(string $target, string $search): bool
|
|
{
|
|
$length = strlen($search);
|
|
if ($length === 0) {
|
|
return true;
|
|
}
|
|
|
|
return substr($target, -$length) === $search;
|
|
}
|
|
}
|