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

add filter, pluck, groupBy functions in EntityList #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 32 additions & 1 deletion src/EntityList.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace DevMakerLab;

use Closure;
use Countable;
use ArrayAccess;
use ArrayIterator;
Expand Down Expand Up @@ -73,7 +74,7 @@ public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->entities);
}

public function sortBy(string $property): self
{
usort($this->entities, function($a, $b) use ($property) {
Expand All @@ -95,4 +96,34 @@ public function only(...$keys): array

return $items;
}

/**
* @param mixed|Closure
* @param ?mixed $value
*/
public function filter($key, $value = null): array
{
$callable = is_callable($key)
? $key
: function (Entity $entity) use ($key, $value) {
return $entity->{$key} === $value;
};

return array_filter($this->entities, $callable);
}

public function pluck(string $key): array
{
return array_column($this->entities, $key);
}

public function groupBy(string $key): array
{
$group = [];
foreach ($this->entities as $entity) {
$group[$entity->{$key}][] = $entity;
}

return $group;
}
}
79 changes: 79 additions & 0 deletions tests/EntityListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,83 @@ public function can_sort_by_entity_property()

$this->assertEquals($expected, $people->sortBy('age'));
}

/** @test */
public function can_filter_list_using_callback()
{
$people = new People([
$adult = new Hooman(['name' => 'First', 'age' => 25]),
new Hooman(['name' => 'Second', 'age' => 12]),
]);

$callback = function (Hooman $entity) {
if ($entity->age > 21) {
return $entity->age;
}
};

$filteredPeople = $people->filter($callback);

$this->assertEquals($filteredPeople, [$adult]);
}

/** @test */
public function can_filter_using_key_value()
{
$adultAge = 21;

$people = new People([
$adult = new Hooman(['name' => 'First', 'age' => 21]),
new Hooman(['name' => 'Second', 'age' => 12]),
]);

$filteredPeople = $people->filter('age', $adultAge);

$this->assertEquals($filteredPeople, [$adult]);
}

/** @test */
public function can_pluck_all_entity_property()
{
$firstName = 'First';
$secondName = 'Second';

$people = new People([
new Hooman(['name' => $firstName, 'age' => 25]),
new Hooman(['name' => $secondName, 'age' => 12]),
]);

$peopleNames = $people->pluck('name');

$this->assertEquals($peopleNames, [$firstName, $secondName]);
}

/** @test */
public function can_group_by_entity_property()
{
$grandPaAge = 82;
$adultAge = 21;
$babyAge = 3;

$grandPaHooman = new Hooman(['name' => 'Adult', 'age' => $grandPaAge]);
$adultHooman = new Hooman(['name' => 'Adult', 'age' => $adultAge]);
$babyHooman = new Hooman(['name' => 'Baby', 'age' => $babyAge]);

$people = new People([
$grandPaHooman,
$adultHooman,
$adultHooman,
$babyHooman,
]);

$groupedPeopleByAge = $people->groupBy('age');

$family = [
$grandPaAge => [$grandPaHooman],
$adultAge => [$adultHooman, $adultHooman],
$babyAge => [$babyHooman]
];

$this->assertEquals($groupedPeopleByAge, $family);
}
}