Skip to content

Commit

Permalink
Added filter method to DependencyCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
Verena Röösli authored and icanhazstring committed Oct 19, 2021
1 parent 5e8705d commit 7dbc4c3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Expand Up @@ -11,15 +11,10 @@ final class CollectFilteredDependenciesCommandHandler
{
public function collect(FilterDependencyCollectionCommand $command): DependencyCollection
{
$dependencyCollection = new DependencyCollection();
$namedExclusion = $command->getNamedExclusion();

foreach ($command->getRequiredDependencyCollection() as $dependency) {
if (!in_array($dependency->getName(), $namedExclusion)) {
$dependencyCollection->add($dependency);
}
}

return $dependencyCollection;
return $command->getRequiredDependencyCollection()->filter(static function ($dependency) use ($namedExclusion) {
return !in_array($dependency->getName(), $namedExclusion);
});
}
}
11 changes: 11 additions & 0 deletions src/Dependency/DependencyCollection.php
Expand Up @@ -65,4 +65,15 @@ public function partition(Closure $partition): array
new self($noMatches)
];
}

/**
* @param Closure $fn
* @return DependencyCollection
*/
public function filter(Closure $fn): DependencyCollection
{
return new self(
array_filter($this->items, $fn)
);
}
}

0 comments on commit 7dbc4c3

Please sign in to comment.