Skip to content

[5.4] Make Arr::random($array, 1) return an array of one item #19826

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

Merged
merged 5 commits into from
Jun 29, 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
19 changes: 15 additions & 4 deletions src/Illuminate/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Support;

use ArrayAccess;
use InvalidArgumentException;
use Illuminate\Support\Traits\Macroable;

class Arr
Expand Down Expand Up @@ -442,16 +443,26 @@ public static function pull(&$array, $key, $default = null)
* Get a random value from an array.
*
* @param array $array
* @param int $num
* @param int|null $amount
* @return mixed
*
* @throws \InvalidArgumentException
*/
public static function random($array, $num = 1)
public static function random($array, $amount = null)
{
if ($num == 1) {
if (($requested = $amount ?: 1) > ($count = count($array))) {
throw new InvalidArgumentException("You requested {$requested} items, but there are only {$count} items in the array.");
}

if (is_null($amount)) {
return $array[array_rand($array)];
}

$keys = array_rand($array, $num);
$keys = array_rand($array, $amount);

if ($amount == 1) {
$keys = [$keys];
}

$results = [];

Expand Down
16 changes: 6 additions & 10 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1077,21 +1077,17 @@ public function put($key, $value)
*
* @throws \InvalidArgumentException
*/
public function random($amount = 1)
public function random($amount = null)
{
if ($amount > ($count = $this->count())) {
throw new InvalidArgumentException("You requested {$amount} items, but there are only {$count} items in the collection.");
if (($requested = $amount ?: 1) > ($count = $this->count())) {
throw new InvalidArgumentException("You requested {$requested} items, but there are only {$count} items in the collection.");
}

$keys = array_rand($this->items, $amount);

if (count(func_get_args()) == 0) {
return $this->items[$keys];
if (is_null($amount)) {
return Arr::random($this->items);
}

$keys = array_wrap($keys);

return new static(array_intersect_key($this->items, array_flip($keys)));
return new static(Arr::random($this->items, $amount));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,10 @@ function array_pull(&$array, $key, $default = null)
* Get a random value from an array.
*
* @param array $array
* @param int $num
* @param int|null $num
* @return mixed
*/
function array_random($array, $num = 1)
function array_random($array, $num = null)
{
return Arr::random($array, $num);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,12 @@ public function testRandom()

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

$randomValues = Arr::random(['foo', 'bar', 'baz'], 1);

$this->assertInternalType('array', $randomValues);
$this->assertCount(1, $randomValues);
$this->assertContains($randomValues[0], ['foo', 'bar', 'baz']);

$randomValues = Arr::random(['foo', 'bar', 'baz'], 2);

$this->assertInternalType('array', $randomValues);
Expand Down