From fbe5680b688014a64bf697da1da29515bb5c8cee Mon Sep 17 00:00:00 2001 From: Benson Lee Date: Tue, 11 Jul 2017 10:51:45 -0700 Subject: [PATCH] Laravel 5.4.28 changes (#46) --- src/Illuminate/Support/Arr.php | 33 ++++++++++++++++++ src/Illuminate/Support/Collection.php | 44 +++++++++++++++++------ tests/Support/SupportCollectionTest.php | 46 +++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 11 deletions(-) diff --git a/src/Illuminate/Support/Arr.php b/src/Illuminate/Support/Arr.php index c6bf456..e7f0ad0 100644 --- a/src/Illuminate/Support/Arr.php +++ b/src/Illuminate/Support/Arr.php @@ -3,6 +3,7 @@ namespace Illuminate\Support; use ArrayAccess; +use InvalidArgumentException; use Illuminate\Support\Traits\Macroable; class Arr @@ -438,6 +439,38 @@ public static function pull(&$array, $key, $default = null) return $value; } + /** + * Get a random value from an array. + * + * @param array $array + * @param int|null $amount + * @return mixed + * + * @throws \InvalidArgumentException + */ + public static function random($array, $amount = null) + { + 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, $amount); + + $results = []; + + foreach ((array) $keys as $key) { + $results[] = $array[$key]; + } + + return $results; + } + /** * Set an array item to a given value using "dot" notation. * diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index fe100ab..54689cb 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -32,7 +32,7 @@ class Collection implements ArrayAccess, Arrayable, Countable, IteratorAggregate * @var array */ protected static $proxies = [ - 'contains', 'each', 'every', 'filter', 'first', 'flatMap', + 'average', 'avg', 'contains', 'each', 'every', 'filter', 'first', 'flatMap', 'map', 'partition', 'reject', 'sortBy', 'sortByDesc', 'sum', ]; @@ -384,6 +384,19 @@ public function when($value, callable $callback, callable $default = null) return $this; } + /** + * Apply the callback if the value is falsy. + * + * @param bool $value + * @param callable $callback + * @param callable $default + * @return mixed + */ + public function unless($value, callable $callback, callable $default = null) + { + return $this->when(! $value, $callback, $default); + } + /** * Filter items by the given key value pair. * @@ -665,6 +678,17 @@ public function intersect($items) return new static(array_intersect($this->items, $this->getArrayableItems($items))); } + /** + * Intersect the collection with the given items by key. + * + * @param mixed $items + * @return static + */ + public function intersectKey($items) + { + return new static(array_intersect_key($this->items, $this->getArrayableItems($items))); + } + /** * Determine if the collection is empty or not. * @@ -1053,21 +1077,19 @@ 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)); } /** diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 0893487..5a40b19 100644 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -590,6 +590,18 @@ public function testIntersectCollection() $this->assertEquals(['first_word' => 'Hello'], $c->intersect(new Collection(['first_world' => 'Hello', 'last_word' => 'World']))->all()); } + public function testIntersectKeyNull() + { + $c = new Collection(['id' => 1, 'first_word' => 'Hello']); + $this->assertEquals([], $c->intersectKey(null)->all()); + } + + public function testIntersectKeyCollection() + { + $c = new Collection(['id' => 1, 'first_word' => 'Hello']); + $this->assertEquals(['first_word' => 'Hello'], $c->intersectKey(new Collection(['first_word' => 'Hello', 'last_word' => 'World']))->all()); + } + public function testUnique() { $c = new Collection(['Hello', 'World', 'World']); @@ -1672,9 +1684,11 @@ public function testGettingAvgItemsFromCollection() return $item->foo; })); $this->assertEquals(15, $c->avg('foo')); + $this->assertEquals(15, $c->avg->foo); $c = new Collection([['foo' => 10], ['foo' => 20]]); $this->assertEquals(15, $c->avg('foo')); + $this->assertEquals(15, $c->avg->foo); $c = new Collection([1, 2, 3, 4, 5]); $this->assertEquals(3, $c->avg()); @@ -2107,6 +2121,38 @@ public function testWhenDefault() $this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray()); } + + public function testUnless() + { + $collection = new Collection(['michael', 'tom']); + + $collection->unless(false, function ($collection) { + return $collection->push('caleb'); + }); + + $this->assertSame(['michael', 'tom', 'caleb'], $collection->toArray()); + + $collection = new Collection(['michael', 'tom']); + + $collection->unless(true, function ($collection) { + return $collection->push('caleb'); + }); + + $this->assertSame(['michael', 'tom'], $collection->toArray()); + } + + public function testUnlessDefault() + { + $collection = new Collection(['michael', 'tom']); + + $collection->unless(true, function ($collection) { + return $collection->push('caleb'); + }, function ($collection) { + return $collection->push('taylor'); + }); + + $this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray()); + } } class TestSupportCollectionHigherOrderItem