Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHP 8.2] Redesign Arr::randomElement(), use Random\Randomizer::pickArrayKeys() #151

Closed
aedart opened this issue Feb 19, 2023 · 0 comments
Closed
Labels
enhancement New feature or request good first issue A good place to start contribution,...
Milestone

Comments

@aedart
Copy link
Owner

aedart commented Feb 19, 2023

Description

The Arr::randomElement() differs slightly from Laravel's Arr::random() in that it can be seeded, using a specified seeding mode. However, from PHP v8.2, the Random\Randomizer::pickArrayKeys is available, and should be used instead.

Yet, the seeding process must be slightly changed. In order to seed the Randomizer, a different Random\Engine. So, perhaps a different set of methods should be designed to replace the Arr::randomElement().

Possible solution

The following could perhaps be better suitable method(s):

use Random\Engine;

// Single random value from array - replacement for Arr::randomElement()
public static randomValue(array $arr, Enging|null $random = null): mixed;

// Keys are not preserved - intended difference from Laravel's Arr::random()
public static randomValues(array $arr, int $amount, Enging|null $random = null): array;

// Single random array key
public static randomKey(array $arr, Enging|null $random = null): string|int;

// Uses Random\Randomizer::pickArrayKeys()
public static randomKeys(array $arr, int $amount, Enging|null $random = null): array

See also

@aedart aedart added enhancement New feature or request good first issue A good place to start contribution,... labels Feb 19, 2023
@aedart aedart added this to the v8.x milestone Feb 19, 2023
aedart added a commit that referenced this issue Mar 14, 2024
aedart added a commit that referenced this issue Mar 14, 2024
aedart added a commit that referenced this issue Mar 14, 2024
Name makes more sense now.

#150, #151
aedart added a commit that referenced this issue Mar 15, 2024
Now each type of randomizer has its own responsibilities and less changes of naming conflicts (e.g. shuffleBytes() vs. shuffleArray() have both been renamed to shuffle()).

#150, #151
@aedart aedart closed this as completed Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue A good place to start contribution,...
Projects
None yet
Development

No branches or pull requests

1 participant