Created
December 5, 2022 14:26
-
-
Save makkinga/a250f8dbc05ea30f7ded25c62c485119 to your computer and use it in GitHub Desktop.
Draw lots for your Christmas surprises!
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
<?php | |
/* | |
* LotDrawer::draw([ | |
* 'Frank' => '[email protected]', | |
* 'John' => '[email protected]', | |
* 'Nathan' => '[email protected]', | |
* 'Jane' => '[email protected]', | |
* ]); | |
*/ | |
class LotDrawer | |
{ | |
/** | |
* Draw lots | |
* | |
* @param array $names | |
* @return array | |
*/ | |
public static function draw(array $names): array | |
{ | |
$shuffled = array_keys($names); | |
shuffle($shuffled); | |
$result = []; | |
$index = 0; | |
foreach ($names as $name => $email) { | |
if ($shuffled[$index] === $name) { | |
return self::draw($names); | |
} | |
$result[] = [ | |
'name' => $name, | |
'email' => $email, | |
'assigned_to' => $shuffled[$index], | |
]; | |
$index++; | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment