Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
alirezasalehizadeh committed Apr 29, 2024
1 parent 60af193 commit af85f5f
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(
*/
public static function byte(int $length): string
{
return self::$random->getBytes($length);
return self::getInstance()->getBytes($length);
}

/**
Expand All @@ -53,10 +53,10 @@ public static function byte(int $length): string
public static function string(string $bytes, int|null $length = null): string
{
if ($length) {
return self::$random->getBytesFromString($bytes, $length);
return self::getInstance()->getBytesFromString($bytes, $length);
}

return self::$random->shuffleBytes($bytes);
return self::getInstance()->shuffleBytes($bytes);
}

/**
Expand All @@ -69,10 +69,10 @@ public static function string(string $bytes, int|null $length = null): string
public static function int(int|null $min = null, int|null $max = null): int
{
if ($min && $max) {
return self::$random->getInt($min, $max);
return self::getInstance()->getInt($min, $max);
}

return self::$random->nextInt();
return self::getInstance()->nextInt();
}

/**
Expand All @@ -86,10 +86,10 @@ public static function int(int|null $min = null, int|null $max = null): int
public static function float(float|null $min = null, float|null $max = null, IntervalBoundary $boundary = IntervalBoundary::ClosedOpen): float
{
if ($min && $max) {
return self::$random->getFloat($min, $max, $boundary);
return self::getInstance()->getFloat($min, $max, $boundary);
}

return self::$random->nextFloat();
return self::getInstance()->nextFloat();
}

/**
Expand All @@ -100,7 +100,7 @@ public static function float(float|null $min = null, float|null $max = null, Int
*/
public static function array(array $array): array
{
return self::$random->shuffleArray($array);
return self::getInstance()->shuffleArray($array);
}

/**
Expand All @@ -112,7 +112,7 @@ public static function array(array $array): array
*/
public static function pick(array $array, int $num = 1): array
{
return self::$random->pickArrayKeys(array_flip($array), $num);
return self::getInstance()->pickArrayKeys(array_flip($array), $num);
}

/**
Expand All @@ -128,4 +128,9 @@ public function __call($name, $arguments)
return $this->engine->$name(...$arguments);
}
}

private static function getInstance(): Randomizer
{
return self::$random ?? new Randomizer;
}
}

0 comments on commit af85f5f

Please sign in to comment.