Skip to content

Fix string length conflict with requireAll#9

Merged
valorin merged 1 commit into
valorin:mainfrom
faissaloux:fix-length-conflict-requireAll
Jan 1, 2024
Merged

Fix string length conflict with requireAll#9
valorin merged 1 commit into
valorin:mainfrom
faissaloux:fix-length-conflict-requireAll

Conversation

@faissaloux
Copy link
Copy Markdown
Contributor

Fixes #7

To fix the conflict between the length and requireAll, I make sure that the length is enough to require all the types of input. If not it throws an error.

Examples

use Valorin\Random\Random;

$generator = Random::useLower(range('a', 'f'))
    ->useUpper(range('G', 'L'))
    ->useNumbers(range(2, 6))
    ->useSymbols(['!', '@', '#', '$', '%', '^', '&', '*', '(', ')']);

$string = $generator->string(
    $length = 2,
    $lower = true,
    $upper = false,
    $numbers = true,
    $symbols = true,
    $requireAll = true
);

var_dump($string);

Fatal error: Uncaught InvalidArgumentException: Length not enough to requireAll!

In this case it will throw an error because length = 2 is not enough to require all the three (lower, numbers and symbols)


But if we deactivate numbers so we can have only 2, This will work and require both lower and symbols.

use Valorin\Random\Random;

$generator = Random::useLower(range('a', 'f'))
    ->useUpper(range('G', 'L'))
    ->useNumbers(range(2, 6))
    ->useSymbols(['!', '@', '#', '$', '%', '^', '&', '*', '(', ')']);

$string = $generator->string(
    $length = 2,
    $lower = true,
    $upper = false,
    $numbers = false,
    $symbols = true,
    $requireAll = true
);

var_dump($string);

string(2) "%b"

@valorin
Copy link
Copy Markdown
Owner

valorin commented Jan 1, 2024

Ah nice, thanks!

I did consider this issue when I wrote the code, but forgot to do anything about it.

@valorin valorin merged commit a6fa38f into valorin:main Jan 1, 2024
@faissaloux faissaloux deleted the fix-length-conflict-requireAll branch January 1, 2024 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

string length conflict with requireAll

3 participants