Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add percentage method to Collections #48034

Merged
merged 3 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1758,4 +1758,23 @@ public function offsetUnset($key): void
{
unset($this->items[$key]);
}

/**
* Calculates the percentage of items that pass a given truth test.
*
* @param (callable(TValue, TKey): bool) $callback
* @param int $precision
* @return float
*/
public function percentage(callable $callback, int $precision = 2): float
{
if ($this->isEmpty()) {
return 0;
WendellAdriel marked this conversation as resolved.
Show resolved Hide resolved
}

return round(
num: $this->filter($callback)->count() / $this->count() * 100,
precision: $precision
);
}
}
19 changes: 19 additions & 0 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,25 @@ public function count(): int
return iterator_count($this->getIterator());
}

/**
* Calculates the percentage of items that pass a given truth test.
*
* @param (callable(TValue, TKey): bool) $callback
* @param int $precision
* @return float
*/
public function percentage(callable $callback, int $precision = 2): float
{
if ($this->isEmpty()) {
return 0;
}

return round(
num: $this->filter($callback)->count() / $this->count() * 100,
precision: $precision
);
}

/**
* Make an iterator from the given source.
*
Expand Down
29 changes: 29 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5634,6 +5634,35 @@ public function testEnsureForInheritance($collection)
$data->ensure(\Throwable::class);
}

/**
* @dataProvider collectionClassProvider
*/
public function testPercentageWithFlatCollection($collection)
{
$collection = new $collection([1, 1, 2, 2, 2, 3]);

$this->assertSame(33.33, $collection->percentage(fn ($value) => $value === 1));
$this->assertSame(50.00, $collection->percentage(fn ($value) => $value === 2));
$this->assertSame(16.67, $collection->percentage(fn ($value) => $value === 3));
}

/**
* @dataProvider collectionClassProvider
*/
public function testPercentageWithNestedCollection($collection)
{
$collection = new $collection([
['name' => 'Taylor', 'foo' => 'foo'],
['name' => 'Nuno', 'foo' => 'bar'],
['name' => 'Dries', 'foo' => 'bar'],
['name' => 'Jess', 'foo' => 'baz'],
]);

$this->assertSame(25.00, $collection->percentage(fn ($value) => $value['foo'] === 'foo'));
$this->assertSame(50.00, $collection->percentage(fn ($value) => $value['foo'] === 'bar'));
$this->assertSame(25.00, $collection->percentage(fn ($value) => $value['foo'] === 'baz'));
}

/**
* Provides each collection class, respectively.
*
Expand Down