1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 18:24:04 +00:00
tenancy/src/UniqueIdentifierGenerators/RandomHexGenerator.php
Samuel Štancl 25360f6b6a
[4.x] Improve id generators (#1300)
* add RandomIntGenerator

* remove string assertions

* make int ranges configurable

* update test to use min & max
2025-01-21 17:06:15 +01:00

25 lines
684 B
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\UniqueIdentifierGenerators;
use Illuminate\Database\Eloquent\Model;
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
/**
* Generates a cryptographically secure random hex string for the tenant key.
*
* To customize the byte length, change the static `bytes` property.
* The produced string length is 2 * byte length.
* The number of unique combinations is 2 ^ (8 * byte length).
*/
class RandomHexGenerator implements UniqueIdentifierGenerator
{
public static int $bytes = 6;
public static function generate(Model $model): string|int
{
return bin2hex(random_bytes(static::$bytes));
}
}