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

[5.4] array_random helper #19741

Merged
merged 3 commits into from
Jun 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Connectors/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ protected function getWriteConfig(array $config)
protected function getReadWriteConfig(array $config, $type)
{
return isset($config[$type][0])
? $config[$type][array_rand($config[$type])]
? Arr::random($config[$type])
: $config[$type];
}

Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,17 @@ public static function pull(&$array, $key, $default = null)
return $value;
}

/**
* Get a random value from an array.
*
* @param array $array
* @return mixed
*/
public static function random($array)
{
return $array[array_rand($array)];
}

/**
Copy link
Contributor

@tillkruss tillkruss Jun 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably support the optional $num argument as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, the $num argument should be supported.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, just as a proposal: in addition to the regular "raffle tickets" mode, a "dice roll" mode might be added, allowing items to be picked several times. (see fzaninotto/Faker#1060 to better understand what I mean)

* Set an array item to a given value using "dot" notation.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,19 @@ function array_pull(&$array, $key, $default = null)
}
}

if (! function_exists('array_random')) {
/**
* Get a random value from an array.
*
* @param array $array
* @return mixed
*/
function array_random($array)
{
return Arr::random($array);
}
}

if (! function_exists('array_set')) {
/**
* Set an array item to a given value using "dot" notation.
Expand Down
8 changes: 8 additions & 0 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,14 @@ public function testPull()
$this->assertEquals(['emails' => ['joe@example.com' => 'Joe', 'jane@localhost' => 'Jane']], $array);
}

public function testRandom()
{
$randomValue = Arr::random(['foo', 'bar', 'baz']);

$this->assertInternalType('string', $randomValue);
$this->assertContains($randomValue, ['foo', 'bar', 'baz']);
}

public function testSet()
{
$array = ['products' => ['desk' => ['price' => 100]]];
Expand Down