Skip to content

Instantly share code, notes, and snippets.

@mlebkowski
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlebkowski/5755216ab974023dd32c to your computer and use it in GitHub Desktop.
Save mlebkowski/5755216ab974023dd32c to your computer and use it in GitHub Desktop.
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