From 73aa38b20d932f5e8f8ccf721e4c27f4304783d6 Mon Sep 17 00:00:00 2001 From: Benson Lee Date: Mon, 14 Aug 2017 13:47:19 -0700 Subject: [PATCH] Laravel 5.4.33 changes (#50) --- src/Illuminate/Support/Arr.php | 22 +++++++--- src/Illuminate/Support/Collection.php | 33 +++++--------- tests/Support/SupportCollectionTest.php | 57 +++++++++++++++++++------ 3 files changed, 69 insertions(+), 43 deletions(-) diff --git a/src/Illuminate/Support/Arr.php b/src/Illuminate/Support/Arr.php index 72a9c0f..4b3e447 100644 --- a/src/Illuminate/Support/Arr.php +++ b/src/Illuminate/Support/Arr.php @@ -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 = []; diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index f8847cd..fa7a850 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -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; @@ -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); } /** @@ -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)); } /** diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 0edced7..00a9ac2 100644 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -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); @@ -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() @@ -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()