Skip to content

Commit

Permalink
Objects pfilter
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Feb 5, 2020
1 parent 3415f6d commit 0b9b161
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
47 changes: 46 additions & 1 deletion src/Objects.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ public static function psort(array $list, $property)

public static function mfilter(array $list, $method, $negate = false)
{
if(!is_string($method))
if(!is_string($method) || empty($method))
{
throw new InvalidArgumentException('Argument method is not a string.');
}
Expand All @@ -626,6 +626,51 @@ public static function mfilter(array $list, $method, $negate = false)
return $result;
}

/**
* Filter a list of objects by matching the property against the match value (===)
*
* @param $list array List of objects to filter.
* @param $property string A property name.
* @param $match mixed A value to match the property against, or a closure
* @param $negate bool Optionally, pass true to drop objects
* which pass the filter instead of keeping them.
*
* @return array List of objects which pass the filter.
* @throws InvalidArgumentException
*/

public static function pfilter(array $list, $property, $match, $negate = false)
{
if(!is_string($property) || empty($property))
{
throw new InvalidArgumentException('Argument property is not a string.');
}

$result = [];
if(is_callable($match))
{
foreach($list as $key => $object)
{
if($match(Objects::property($object, $property)) != $negate)
{
$result[$key] = $object;
}
}
}
else
{
foreach($list as $key => $object)
{
if((Objects::property($object, $property) === $match) != $negate)
{
$result[$key] = $object;
}
}
}

return $result;
}

/**
* Perform a callback against an object, and return the object
*
Expand Down
39 changes: 39 additions & 0 deletions tests/ObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,4 +432,43 @@ public function testTreeM()
$this->assertContainsOnlyInstancesOf(Branch::class, $tree->getChildren());
$this->assertCount(1, $tree->getChildren());
}

public function testPFilterNullPropertyThrowException()
{
$caught = null;
try
{
Objects::pfilter([], null, 'abc');
}
catch(InvalidArgumentException $ex)
{
$caught = $ex;
}

$this->assertEquals(true, ($caught instanceof InvalidArgumentException));
}

public function testPFilter()
{
$a = new Pancake("apple", "toffee");
$b = new Pancake("apple", "strawberry");
$c = new Pancake("orange", "toffee");
$d = new Pancake("orange", "gravy");

$list = ['a' => $a, 'b' => $b, 'c' => $c, 'd' => $d];

$actual = Objects::pfilter($list, 'fruit', 'apple');
$this->assertEquals(['a' => $a, 'b' => $b,], $actual);

$actual = Objects::pfilter($list, 'fruit', 'apple', true);
$this->assertEquals(['c' => $c, 'd' => $d,], $actual);

$matchApple = function ($prop) { return $prop == 'apple'; };

$actual = Objects::pfilter($list, 'fruit', $matchApple);
$this->assertEquals(['a' => $a, 'b' => $b,], $actual);

$actual = Objects::pfilter($list, 'fruit', $matchApple, true);
$this->assertEquals(['c' => $c, 'd' => $d,], $actual);
}
}

0 comments on commit 0b9b161

Please sign in to comment.