Skip to content

Commit

Permalink
Split Randomizer into smaller interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
aedart committed Mar 14, 2024
1 parent c5c37be commit 642d0cf
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 67 deletions.
37 changes: 37 additions & 0 deletions packages/Contracts/src/Utils/Random/ArrayRandomizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Aedart\Contracts\Utils\Random;

use Throwable;

/**
* Array Randomizer
*
* @author Alin Eugen Deac <[email protected]>
* @package Aedart\Contracts\Utils\Random
*/
interface ArrayRandomizer
{
/**
* Returns random array keys
*
* @param array $arr
* @param int $amount Amount of keys to return
*
* @return array<string>|array<int>
*
* @throws Throwable
*/
public function pickKeys(array $arr, int $amount): array;

/**
* Shuffles given array
*
* @param array $arr
*
* @return array New shuffled array
*
* @throws Throwable
*/
public function shuffle(array $arr): array;
}
35 changes: 35 additions & 0 deletions packages/Contracts/src/Utils/Random/NumericRandomizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Aedart\Contracts\Utils\Random;

use Throwable;

/**
* Numeric Randomizer
*
* @author Alin Eugen Deac <[email protected]>
* @package Aedart\Contracts\Utils\Random
*/
interface NumericRandomizer
{
/**
* Returns a uniformly selected integer
*
* @param int $min
* @param int $max
*
* @return int
*
* @throws Throwable
*/
public function int(int $min, int $max): int;

/**
* Returns next positive integer
*
* @return int
*
* @throws Throwable
*/
public function nextInt(): int;
}
71 changes: 4 additions & 67 deletions packages/Contracts/src/Utils/Random/Randomizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,74 +10,11 @@
* @author Alin Eugen Deac <[email protected]>
* @package Aedart\Contracts\Utils\Random
*/
interface Randomizer
interface Randomizer extends
StringRandomizer,
NumericRandomizer,
ArrayRandomizer
{
/**
* Returns random bytes
*
* @param int $length
*
* @return string
*
* @throws Throwable
*/
public function bytes(int $length): string;

/**
* Returns a uniformly selected integer
*
* @param int $min
* @param int $max
*
* @return int
*
* @throws Throwable
*/
public function int(int $min, int $max): int;

/**
* Returns next positive integer
*
* @return int
*
* @throws Throwable
*/
public function nextInt(): int;

/**
* Returns random array keys
*
* @param array $arr
* @param int $amount Amount of keys to return
*
* @return array<string>|array<int>
*
* @throws Throwable
*/
public function pickKeys(array $arr, int $amount): array;

/**
* Shuffles given array
*
* @param array $arr
*
* @return array New shuffled array
*
* @throws Throwable
*/
public function shuffle(array $arr): array;

/**
* Shuffles given bytes
*
* @param string $bytes
*
* @return string New shuffled bytes
*
* @throws Throwable
*/
public function shuffleBytes(string $bytes): string;

/**
* Returns the underlying driver of this randomizer
*
Expand Down
36 changes: 36 additions & 0 deletions packages/Contracts/src/Utils/Random/StringRandomizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Aedart\Contracts\Utils\Random;

use Throwable;

/**
* String Randomizer
*
* @author Alin Eugen Deac <[email protected]>
* @package Aedart\Contracts\Utils\Random
*/
interface StringRandomizer
{
/**
* Returns random bytes
*
* @param int $length
*
* @return string
*
* @throws Throwable
*/
public function bytes(int $length): string;

/**
* Shuffles given bytes
*
* @param string $bytes
*
* @return string New shuffled bytes
*
* @throws Throwable
*/
public function shuffleBytes(string $bytes): string;
}

0 comments on commit 642d0cf

Please sign in to comment.