Last active
August 29, 2015 14:25
-
-
Save mlebkowski/5755216ab974023dd32c to your computer and use it in GitHub Desktop.
Functional PHP with a little OOP aid
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 | |
// Node[] -> (map) -> NodeTranslation[] -> (map) -> Category[] -> slice | |
// Purely „functional” PHP: | |
$parents = array_slice(array_map(function (NodeTranslation $nt) | |
{ | |
return new Category($nt->getTitle(), $nt->getNode()->getId(), $nt->getSlug()); | |
}, array_map(function (Node $node) | |
{ | |
return $node->getNodeTranslations()->first(); | |
}, $node->getParents())), 1); | |
// using iterators instead: | |
$parents = (new ArrayCollection($node->getParents()))->map(function (Node $node) | |
{ | |
return $node->getNodeTranslations()->first(); | |
})->map(function (NodeTranslation $nt) | |
{ | |
return new Category($nt->getTitle(), $nt->getNode()->getId(), $nt->getSlug()); | |
})->slice(1); | |
// how it should be done: | |
$parents = $node->getParents() | |
->map(function (Node $node) -> $node->getNodeTranslations()->first()) | |
->map(function (NodeTranslation $nt) -> new Category($nt->getTitle(), $nt->getNode()->getId(), $nt->getSlug())) | |
->slice(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment