Skip to content

Commit e95dc82

Browse files
[10.x] Add percentage method to Collections (#48034)
* Create percentage Collection method * Update percentage to return null for empty collections and add more test cases * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 1eed738 commit e95dc82

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/Illuminate/Collections/Traits/EnumeratesValues.php

+19
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,25 @@ public function partition($key, $operator = null, $value = null)
481481
return new static([new static($passed), new static($failed)]);
482482
}
483483

484+
/**
485+
* Calculate the percentage of items that pass a given truth test.
486+
*
487+
* @param (callable(TValue, TKey): bool) $callback
488+
* @param int $precision
489+
* @return float|null
490+
*/
491+
public function percentage(callable $callback, int $precision = 2)
492+
{
493+
if ($this->isEmpty()) {
494+
return null;
495+
}
496+
497+
return round(
498+
$this->filter($callback)->count() / $this->count() * 100,
499+
$precision
500+
);
501+
}
502+
484503
/**
485504
* Get the sum of the given values.
486505
*

tests/Support/SupportCollectionTest.php

+41
Original file line numberDiff line numberDiff line change
@@ -5634,6 +5634,47 @@ public function testEnsureForInheritance($collection)
56345634
$data->ensure(\Throwable::class);
56355635
}
56365636

5637+
/**
5638+
* @dataProvider collectionClassProvider
5639+
*/
5640+
public function testPercentageWithFlatCollection($collection)
5641+
{
5642+
$collection = new $collection([1, 1, 2, 2, 2, 3]);
5643+
5644+
$this->assertSame(33.33, $collection->percentage(fn ($value) => $value === 1));
5645+
$this->assertSame(50.00, $collection->percentage(fn ($value) => $value === 2));
5646+
$this->assertSame(16.67, $collection->percentage(fn ($value) => $value === 3));
5647+
$this->assertSame(0.0, $collection->percentage(fn ($value) => $value === 5));
5648+
}
5649+
5650+
/**
5651+
* @dataProvider collectionClassProvider
5652+
*/
5653+
public function testPercentageWithNestedCollection($collection)
5654+
{
5655+
$collection = new $collection([
5656+
['name' => 'Taylor', 'foo' => 'foo'],
5657+
['name' => 'Nuno', 'foo' => 'bar'],
5658+
['name' => 'Dries', 'foo' => 'bar'],
5659+
['name' => 'Jess', 'foo' => 'baz'],
5660+
]);
5661+
5662+
$this->assertSame(25.00, $collection->percentage(fn ($value) => $value['foo'] === 'foo'));
5663+
$this->assertSame(50.00, $collection->percentage(fn ($value) => $value['foo'] === 'bar'));
5664+
$this->assertSame(25.00, $collection->percentage(fn ($value) => $value['foo'] === 'baz'));
5665+
$this->assertSame(0.0, $collection->percentage(fn ($value) => $value['foo'] === 'test'));
5666+
}
5667+
5668+
/**
5669+
* @dataProvider collectionClassProvider
5670+
*/
5671+
public function testPercentageReturnsNullForEmptyCollections($collection)
5672+
{
5673+
$collection = new $collection([]);
5674+
5675+
$this->assertNull($collection->percentage(fn ($value) => $value === 1));
5676+
}
5677+
56375678
/**
56385679
* Provides each collection class, respectively.
56395680
*

0 commit comments

Comments
 (0)