Last active
April 29, 2017 21:06
-
-
Save skollro/ac5c7eacc92c66c5c48f3179bb1be10a to your computer and use it in GitHub Desktop.
A join collection macro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Collection::macro('join', function ($items, callable $callback) { | |
return $this->flatMap(function ($value) use ($items, $callback) { | |
return $items->filter(function ($item) use ($value, $callback) { | |
return $callback($value, $item); | |
})->map(function ($item) use ($value) { | |
return new Collection([$value, $item]); | |
}); | |
}); | |
}); | |
$first = collect([0, 1, 2]); | |
$second = collect([0, 0, 1]); | |
$result = $first->join($second, function ($a, $b) { | |
return $a == $b; | |
}); | |
dd($result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment