Skip to content

Instantly share code, notes, and snippets.

@mlebkowski mlebkowski/functional.php
Last active Aug 29, 2015

Embed
What would you like to do?
Functional PHP with a little OOP aid
<?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
You can’t perform that action at this time.