From 49770eca4e2e780d4e8cdc762e2adbcab8b924fa Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 5 Mar 2018 08:05:53 -0600 Subject: [PATCH] port change forward --- src/Illuminate/View/Concerns/ManagesLoops.php | 9 +++++++- tests/View/ViewFactoryTest.php | 22 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/View/Concerns/ManagesLoops.php b/src/Illuminate/View/Concerns/ManagesLoops.php index 5f50b247efd9..ff3332c3802e 100644 --- a/src/Illuminate/View/Concerns/ManagesLoops.php +++ b/src/Illuminate/View/Concerns/ManagesLoops.php @@ -3,6 +3,7 @@ namespace Illuminate\View\Concerns; use Countable; +use Traversable; use Illuminate\Support\Arr; trait ManagesLoops @@ -22,7 +23,13 @@ trait ManagesLoops */ public function addLoop($data) { - $length = is_array($data) || $data instanceof Countable ? count($data) : null; + $length = null; + + if (is_array($data) || $data instanceof Countable) { + $length = count($data); + } elseif ($data instanceof Traversable) { + $length = iterator_count($data); + } $parent = Arr::last($this->loopsStack); diff --git a/tests/View/ViewFactoryTest.php b/tests/View/ViewFactoryTest.php index efbccf85d3ea..b4e91ec5ab14 100755 --- a/tests/View/ViewFactoryTest.php +++ b/tests/View/ViewFactoryTest.php @@ -555,6 +555,28 @@ public function testAddingLoops() $this->assertEquals([$expectedLoop], $factory->getLoopStack()); } + public function testAddingTraversableLoop() + { + $factory = $this->getFactory(); + + $data = new \DatePeriod(\Carbon\Carbon::today(), \Carbon\CarbonInterval::day(), \Carbon\Carbon::tomorrow()->endOfDay()); + + $factory->addLoop($data); + + $expectedLoop = [ + 'iteration' => 0, + 'index' => 0, + 'remaining' => 2, + 'count' => 2, + 'first' => true, + 'last' => false, + 'depth' => 1, + 'parent' => null, + ]; + + $this->assertEquals([$expectedLoop], $factory->getLoopStack()); + } + public function testAddingUncountableLoop() { $factory = $this->getFactory();