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.28 changes (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
besologic authored Jul 11, 2017
1 parent 16a16f2 commit fbe5680
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 11 deletions.
33 changes: 33 additions & 0 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 @@ -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.
*
Expand Down
44 changes: 33 additions & 11 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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));
}

/**
Expand Down
46 changes: 46 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fbe5680

Please sign in to comment.