mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 19:14:04 +00:00
[4.x] Improve id generators (#1300)
* add RandomIntGenerator * remove string assertions * make int ranges configurable * update test to use min & max
This commit is contained in:
parent
7ce7629864
commit
25360f6b6a
7 changed files with 43 additions and 4 deletions
|
|
@ -11,5 +11,5 @@ interface UniqueIdentifierGenerator
|
|||
/**
|
||||
* Generate a unique identifier for a model.
|
||||
*/
|
||||
public static function generate(Model $model): string;
|
||||
public static function generate(Model $model): string|int;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ class RandomHexGenerator implements UniqueIdentifierGenerator
|
|||
{
|
||||
public static int $bytes = 6;
|
||||
|
||||
public static function generate(Model $model): string
|
||||
public static function generate(Model $model): string|int
|
||||
{
|
||||
return bin2hex(random_bytes(static::$bytes));
|
||||
}
|
||||
|
|
|
|||
22
src/UniqueIdentifierGenerators/RandomIntGenerator.php
Normal file
22
src/UniqueIdentifierGenerators/RandomIntGenerator.php
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\UniqueIdentifierGenerators;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
|
||||
|
||||
/**
|
||||
* Generates a cryptographically secure random integer for the tenant key.
|
||||
*/
|
||||
class RandomIntGenerator implements UniqueIdentifierGenerator
|
||||
{
|
||||
public static int $min = 0;
|
||||
public static int $max = PHP_INT_MAX;
|
||||
|
||||
public static function generate(Model $model): string|int
|
||||
{
|
||||
return random_int(static::$min, static::$max);
|
||||
}
|
||||
}
|
||||
|
|
@ -18,7 +18,7 @@ class RandomStringGenerator implements UniqueIdentifierGenerator
|
|||
{
|
||||
public static int $length = 8;
|
||||
|
||||
public static function generate(Model $model): string
|
||||
public static function generate(Model $model): string|int
|
||||
{
|
||||
return Str::random(static::$length);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
|
|||
*/
|
||||
class UUIDGenerator implements UniqueIdentifierGenerator
|
||||
{
|
||||
public static function generate(Model $model): string
|
||||
public static function generate(Model $model): string|int
|
||||
{
|
||||
return Uuid::uuid4()->toString();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue