Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
Laravel 5.4.33 changes (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
besologic authored Aug 14, 2017
1 parent 7c232b5 commit 73aa38b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 43 deletions.
22 changes: 15 additions & 7 deletions src/Illuminate/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,27 +454,35 @@ public static function pull(&$array, $key, $default = null)
}

/**
* Get a random value from an array.
* Get one or a specified number of random values from an array.
*
* @param array $array
* @param int|null $amount
* @param int|null $number
* @return mixed
*
* @throws \InvalidArgumentException
*/
public static function random($array, $amount = null)
public static function random($array, $number = null)
{
if (($requested = $amount ?: 1) > ($count = count($array))) {
$requested = is_null($number) ? 1 : $number;

$count = count($array);

if ($requested > $count) {
throw new InvalidArgumentException(
"You requested {$requested} items, but there are only {$count} items in the array."
"You requested {$requested} items, but there are only {$count} items available."
);
}

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

$keys = array_rand($array, $amount);
if ((int) $number === 0) {
return [];
}

$keys = array_rand($array, $number);

$results = [];

Expand Down
33 changes: 11 additions & 22 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use CachingIterator;
use JsonSerializable;
use IteratorAggregate;
use InvalidArgumentException;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Arrayable;
Expand Down Expand Up @@ -59,23 +58,23 @@ public static function make($items = [])
}

/**
* Create a new collection by invoking the callback a given amount of times.
* Create a new collection by invoking the callback a given number of times.
*
* @param int $amount
* @param int $number
* @param callable $callback
* @return static
*/
public static function times($amount, callable $callback = null)
public static function times($number, callable $callback = null)
{
if ($amount < 1) {
if ($number < 1) {
return new static;
}

if (is_null($callback)) {
return new static(range(1, $amount));
return new static(range(1, $number));
}

return (new static(range(1, $amount)))->map($callback);
return (new static(range(1, $number)))->map($callback);
}

/**
Expand Down Expand Up @@ -1070,30 +1069,20 @@ public function put($key, $value)
}

/**
* Get zero or more items randomly from the collection.
* Get one or a specified number of items randomly from the collection.
*
* @param int|null $amount
* @param int|null $number
* @return mixed
*
* @throws \InvalidArgumentException
*/
public function random($amount = null)
public function random($number = null)
{
if ($amount === 0) {
return new static;
}

if (($requested = $amount ?: 1) > ($count = $this->count())) {
throw new InvalidArgumentException(
"You requested {$requested} items, but there are only {$count} items in the collection."
);
}

if (is_null($amount)) {
if (is_null($number)) {
return Arr::random($this->items);
}

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

/**
Expand Down
57 changes: 43 additions & 14 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,10 @@ public function testRandom()
{
$data = new Collection([1, 2, 3, 4, 5, 6]);

$random = $data->random();
$this->assertInternalType('integer', $random);
$this->assertContains($random, $data->all());

$random = $data->random(0);
$this->assertInstanceOf(Collection::class, $random);
$this->assertCount(0, $random);
Expand All @@ -919,9 +923,21 @@ public function testRandom()
$this->assertInstanceOf(Collection::class, $random);
$this->assertCount(1, $random);

$random = $data->random(3);
$random = $data->random(2);
$this->assertInstanceOf(Collection::class, $random);
$this->assertCount(2, $random);

$random = $data->random('0');
$this->assertInstanceOf(Collection::class, $random);
$this->assertCount(0, $random);

$random = $data->random('1');
$this->assertInstanceOf(Collection::class, $random);
$this->assertCount(1, $random);

$random = $data->random('2');
$this->assertInstanceOf(Collection::class, $random);
$this->assertCount(3, $random);
$this->assertCount(2, $random);
}

public function testRandomOnEmptyCollection()
Expand All @@ -931,23 +947,36 @@ public function testRandomOnEmptyCollection()
$random = $data->random(0);
$this->assertInstanceOf(Collection::class, $random);
$this->assertCount(0, $random);
}

public function testRandomWithoutArgument()
{
$data = new Collection([1, 2, 3, 4, 5, 6]);

$random = $data->random();
$this->assertInternalType('integer', $random);
$this->assertContains($random, $data->all());
$random = $data->random('0');
$this->assertInstanceOf(Collection::class, $random);
$this->assertCount(0, $random);
}

/**
* @expectedException InvalidArgumentException
*/
public function testRandomThrowsAnErrorWhenRequestingMoreItemsThanAreAvailable()
{
(new Collection)->random();
$data = new Collection();
$exceptions = 0;

try {
$data->random();
} catch (\InvalidArgumentException $e) {
++$exceptions;
}

try {
$data->random(1);
} catch (\InvalidArgumentException $e) {
++$exceptions;
}

try {
$data->random(2);
} catch (\InvalidArgumentException $e) {
++$exceptions;
}

$this->assertSame(3, $exceptions);
}

public function testTakeLast()
Expand Down

0 comments on commit 73aa38b

Please sign in to comment.