From 2b2054d842ef5a40a9920853918c9a203af2fbd6 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 1 Jul 2015 07:59:34 -0500 Subject: [PATCH] Update code to work in HHVM - HHVM does not allow using Traversables in `array_walk` invocations. Changed to a foreach loop, which allowed reverting the visibility on `injectPropertyAsLink`, and made the latter function recursive. --- src/Plugin/Hal.php | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Plugin/Hal.php b/src/Plugin/Hal.php index d768219..0d379fc 100644 --- a/src/Plugin/Hal.php +++ b/src/Plugin/Hal.php @@ -606,10 +606,9 @@ public function renderEntity(Entity $halEntity, $renderEntity = true, $depth = 0 unset($entity[$key]); } if ($value instanceof LinkCollection) { - $that = $this; - array_walk_recursive($value, function ($link) use ($entityLinks, $that) { - $that->injectPropertyAsLink($link, $entityLinks); - }); + foreach ($value as $link) { + $entityLinks = $this->injectPropertyAsLink($link, $entityLinks); + } unset($entity[$key]); } } @@ -1379,15 +1378,26 @@ protected function marshalMetadataLinks(Metadata $metadata, LinkCollection $link * * Ensures that the link hasn't been previously injected. * - * This method is marked as public only so that it can be used inside a - * closure context in PHP 5.3. - * - * @param Link $link + * @param Link[]|Link $link * @param LinkCollection $links * @return LinkCollection + * @throws Exception\InvalidArgumentException if a non-link is provided. */ - public function injectPropertyAsLink(Link $link, LinkCollection $links) + protected function injectPropertyAsLink($link, LinkCollection $links) { + if (is_array($link)) { + foreach ($link as $single) { + $links = $this->injectPropertyAsLink($single, $links); + } + return $links; + } + + if (! $link instanceof Link) { + throw new Exception\InvalidArgumentException( + 'Invalid link discovered; cannot inject into representation' + ); + } + $rel = $link->getRelation(); if (! $links->has($rel)) { $links->add($link);