From c443c83c11a3ba93867e7fa0a2b31af026032a59 Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Thu, 14 Mar 2019 09:35:18 -0400 Subject: [PATCH] Adds even and odd flags to the Loop variable --- src/Illuminate/View/Concerns/ManagesLoops.php | 4 ++++ tests/View/ViewFactoryTest.php | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Illuminate/View/Concerns/ManagesLoops.php b/src/Illuminate/View/Concerns/ManagesLoops.php index 5f50b247efd9..edd6363ec7d3 100644 --- a/src/Illuminate/View/Concerns/ManagesLoops.php +++ b/src/Illuminate/View/Concerns/ManagesLoops.php @@ -33,6 +33,8 @@ public function addLoop($data) 'count' => $length, 'first' => true, 'last' => isset($length) ? $length == 1 : null, + 'odd' => false, + 'even' => true, 'depth' => count($this->loopsStack) + 1, 'parent' => $parent ? (object) $parent : null, ]; @@ -51,6 +53,8 @@ public function incrementLoopIndices() 'iteration' => $loop['iteration'] + 1, 'index' => $loop['iteration'], 'first' => $loop['iteration'] == 0, + 'odd' => ! $loop['odd'], + 'even' => ! $loop['even'], 'remaining' => isset($loop['count']) ? $loop['remaining'] - 1 : null, 'last' => isset($loop['count']) ? $loop['iteration'] == $loop['count'] - 1 : null, ]); diff --git a/tests/View/ViewFactoryTest.php b/tests/View/ViewFactoryTest.php index b3d7af197f7e..f6271fdf9a56 100755 --- a/tests/View/ViewFactoryTest.php +++ b/tests/View/ViewFactoryTest.php @@ -539,6 +539,8 @@ public function testAddingLoops() 'count' => 3, 'first' => true, 'last' => false, + 'odd' => false, + 'even' => true, 'depth' => 1, 'parent' => null, ]; @@ -554,6 +556,8 @@ public function testAddingLoops() 'count' => 4, 'first' => true, 'last' => false, + 'odd' => false, + 'even' => true, 'depth' => 2, 'parent' => (object) $expectedLoop, ]; @@ -597,6 +601,8 @@ public function testAddingUncountableLoop() 'count' => null, 'first' => true, 'last' => null, + 'odd' => false, + 'even' => true, 'depth' => 1, 'parent' => null, ]; @@ -612,11 +618,19 @@ public function testIncrementingLoopIndices() $factory->incrementLoopIndices(); + $this->assertEquals(1, $factory->getLoopStack()[0]['iteration']); + $this->assertEquals(0, $factory->getLoopStack()[0]['index']); + $this->assertEquals(3, $factory->getLoopStack()[0]['remaining']); + $this->assertTrue($factory->getLoopStack()[0]['odd']); + $this->assertFalse($factory->getLoopStack()[0]['even']); + $factory->incrementLoopIndices(); $this->assertEquals(2, $factory->getLoopStack()[0]['iteration']); $this->assertEquals(1, $factory->getLoopStack()[0]['index']); $this->assertEquals(2, $factory->getLoopStack()[0]['remaining']); + $this->assertFalse($factory->getLoopStack()[0]['odd']); + $this->assertTrue($factory->getLoopStack()[0]['even']); } public function testReachingEndOfLoop()