From 9d15d65f08ae4d2564cbd4b5e8ca3cb268416364 Mon Sep 17 00:00:00 2001 From: Valentin Leguy Date: Mon, 18 Oct 2021 09:56:37 +0000 Subject: [PATCH] add filter, pluck, groupBy functions in EntityList --- src/EntityList.php | 33 ++++++++++++++++- tests/EntityListTest.php | 79 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) diff --git a/src/EntityList.php b/src/EntityList.php index 3fd43c8..3e34384 100644 --- a/src/EntityList.php +++ b/src/EntityList.php @@ -2,6 +2,7 @@ namespace DevMakerLab; +use Closure; use Countable; use ArrayAccess; use ArrayIterator; @@ -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) { @@ -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; + } } diff --git a/tests/EntityListTest.php b/tests/EntityListTest.php index aa4e234..765fcbb 100644 --- a/tests/EntityListTest.php +++ b/tests/EntityListTest.php @@ -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); + } }