-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UniqueFactory for creating random string or int from given range (#…
…1128) * Add UniqueFactory for creating random string or int from given range * CS fixes Co-authored-by: Joseph Bielawski <[email protected]> * Rename to RandomGenerator, introduce interface * Fix missing import after move * Use functions instead of static calls for tests requiring randomness * Delete src/core/etl/tests/Flow/ETL/Tests/Integration/Cache/PSRSimpleCacheTest.php * Use functions instead of static calls for tests requiring randomness * Add scalar function for random string --------- Co-authored-by: Joseph Bielawski <[email protected]>
- Loading branch information
Showing
37 changed files
with
421 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Function; | ||
|
||
use Flow\ETL\{NativePHPRandomValueGenerator, RandomValueGenerator, Row}; | ||
|
||
class RandomString implements ScalarFunction | ||
{ | ||
private RandomValueGenerator|NativePHPRandomValueGenerator $generator; | ||
|
||
private int|ScalarFunction $length; | ||
|
||
public function __construct(ScalarFunction|int $length, RandomValueGenerator $generator = new NativePHPRandomValueGenerator()) | ||
{ | ||
$this->length = $length; | ||
$this->generator = $generator; | ||
} | ||
|
||
public function eval(Row $row) : string | ||
{ | ||
return $this->generator->string(is_int($this->length) ? $this->length : $this->length->eval($row)); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/core/etl/src/Flow/ETL/NativePHPRandomValueGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL; | ||
|
||
final class NativePHPRandomValueGenerator implements RandomValueGenerator | ||
{ | ||
public function int(int $min, int $max) : int | ||
{ | ||
return \random_int($min, $max); | ||
} | ||
|
||
public function string(int $int) : string | ||
{ | ||
$bytes = (int) \ceil($int / 2); | ||
$bytes >= 1 ?: $bytes = 1; | ||
|
||
return \substr(\bin2hex(\random_bytes($bytes)), 0, \max(0, $int)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL; | ||
|
||
interface RandomValueGenerator | ||
{ | ||
public function int(int $min, int $max) : int; | ||
|
||
public function string(int $int) : string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.