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

Commit

Permalink
Merge pull request #14 from tightenco/v5.2.41
Browse files Browse the repository at this point in the history
5.2.40 and 5.2.41 changes
  • Loading branch information
mattstauffer authored Jul 22, 2016
2 parents fded841 + 4ee62cf commit cb6c548
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 16 deletions.
83 changes: 81 additions & 2 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,65 @@ public function average($key = null)
return $this->avg($key);
}

/**
* Get the median of a given key.
*
* @param null $key
* @return mixed|null
*/
public function median($key = null)
{
$count = $this->count();

if ($count == 0) {
return;
}

$values = with(isset($key) ? $this->pluck($key) : $this)
->sort()->values();

$middle = (int) floor($count / 2);

if ($count % 2) {
return $values->get($middle);
}

return (new static([
$values->get($middle - 1), $values->get($middle),
]))->average();
}

/**
* Get the mode of a given key.
*
* @param null $key
* @return array
*/
public function mode($key = null)
{
$count = $this->count();

if ($count == 0) {
return;
}

$collection = isset($key) ? $this->pluck($key) : $this;

$counts = new self;

$collection->each(function ($value) use ($counts) {
$counts[$value] = isset($counts[$value]) ? $counts[$value] + 1 : 1;
});

$sorted = $counts->sort();

$highestValue = $sorted->last();

return $sorted->filter(function ($value) use ($highestValue) {
return $value == $highestValue;
})->sort()->keys()->all();
}

/**
* Collapse the collection of items into a single array.
*
Expand Down Expand Up @@ -604,6 +663,17 @@ public function forPage($page, $perPage)
return $this->slice(($page - 1) * $perPage, $perPage);
}

/**
* Pass the collection to the given callback and return the result.
*
* @param callable $callback
* @return mixed
*/
public function pipe(callable $callback)
{
return $callback($this);
}

/**
* Get and remove the last item from the collection.
*
Expand Down Expand Up @@ -766,13 +836,22 @@ public function shift()
/**
* Shuffle the items in the collection.
*
* @param int $seed
* @return static
*/
public function shuffle()
public function shuffle($seed = null)
{
$items = $this->items;

shuffle($items);
if (is_null($seed)) {
shuffle($items);
} else {
srand($seed);

usort($items, function () {
return rand(-1, 1);
});
}

return new static($items);
}
Expand Down
28 changes: 14 additions & 14 deletions src/Illuminate/Support/Traits/Macroable.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public static function hasMacro($name)
*/
public static function __callStatic($method, $parameters)
{
if (static::hasMacro($method)) {
if (static::$macros[$method] instanceof Closure) {
return call_user_func_array(Closure::bind(static::$macros[$method], null, static::class), $parameters);
} else {
return call_user_func_array(static::$macros[$method], $parameters);
}
if (! static::hasMacro($method)) {
throw new BadMethodCallException("Method {$method} does not exist.");
}

throw new BadMethodCallException("Method {$method} does not exist.");
if (static::$macros[$method] instanceof Closure) {
return call_user_func_array(Closure::bind(static::$macros[$method], null, static::class), $parameters);
}

return call_user_func_array(static::$macros[$method], $parameters);
}

/**
Expand All @@ -70,14 +70,14 @@ public static function __callStatic($method, $parameters)
*/
public function __call($method, $parameters)
{
if (static::hasMacro($method)) {
if (static::$macros[$method] instanceof Closure) {
return call_user_func_array(static::$macros[$method]->bindTo($this, static::class), $parameters);
} else {
return call_user_func_array(static::$macros[$method], $parameters);
}
if (! static::hasMacro($method)) {
throw new BadMethodCallException("Method {$method} does not exist.");
}

if (static::$macros[$method] instanceof Closure) {
return call_user_func_array(static::$macros[$method]->bindTo($this, static::class), $parameters);
}

throw new BadMethodCallException("Method {$method} does not exist.");
return call_user_func_array(static::$macros[$method], $parameters);
}
}
13 changes: 13 additions & 0 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,16 @@ function data_get($target, $key, $default = null)
return $target;
}
}

if (! function_exists('with')) {
/**
* Return the given object. Useful for chaining.
*
* @param mixed $object
* @return mixed
*/
function with($object)
{
return $object;
}
}
81 changes: 81 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,87 @@ public function testRandomThrowsAnExceptionUsingAmountBiggerThanCollectionSize()
$data = new Collection([1, 2, 3]);
$data->random(4);
}

public function testPipe()
{
$collection = new Collection([1, 2, 3]);

$this->assertEquals(6, $collection->pipe(function ($collection) {
return $collection->sum();
}));
}

public function testMedianValueWithArrayCollection()
{
$collection = new Collection([1, 2, 2, 4]);

$this->assertEquals(2, $collection->median());
}

public function testMedianValueByKey()
{
$collection = new Collection([
(object) ['foo' => 1],
(object) ['foo' => 2],
(object) ['foo' => 2],
(object) ['foo' => 4],
]);
$this->assertEquals(2, $collection->median('foo'));
}

public function testEvenMedianCollection()
{
$collection = new Collection([
(object) ['foo' => 0],
(object) ['foo' => 3],
]);
$this->assertEquals(1.5, $collection->median('foo'));
}

public function testMedianOutOfOrderCollection()
{
$collection = new Collection([
(object) ['foo' => 0],
(object) ['foo' => 5],
(object) ['foo' => 3],
]);
$this->assertEquals(3, $collection->median('foo'));
}

public function testMedianOnEmptyCollectionReturnsNull()
{
$collection = new Collection();
$this->assertNull($collection->median());
}

public function testModeOnNullCollection()
{
$collection = new Collection();
$this->assertNull($collection->mode());
}

public function testMode()
{
$collection = new Collection([1, 2, 3, 4, 4, 5]);
$this->assertEquals([4], $collection->mode());
}

public function testModeValueByKey()
{
$collection = new Collection([
(object) ['foo' => 1],
(object) ['foo' => 1],
(object) ['foo' => 2],
(object) ['foo' => 4],
]);
$this->assertEquals([1], $collection->mode('foo'));
}

public function testWithMultipleModeValues()
{
$collection = new Collection([1, 2, 2, 1]);
$this->assertEquals([1, 2], $collection->mode());
}
}

class TestAccessorEloquentTestStub
Expand Down

0 comments on commit cb6c548

Please sign in to comment.