Skip to content

Commit

Permalink
Adds even and odd flags to the Loop variable
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary committed Mar 14, 2019
1 parent 5ce11d0 commit c443c83
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/View/Concerns/ManagesLoops.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
Expand All @@ -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,
]);
Expand Down
14 changes: 14 additions & 0 deletions tests/View/ViewFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,8 @@ public function testAddingLoops()
'count' => 3,
'first' => true,
'last' => false,
'odd' => false,
'even' => true,
'depth' => 1,
'parent' => null,
];
Expand All @@ -554,6 +556,8 @@ public function testAddingLoops()
'count' => 4,
'first' => true,
'last' => false,
'odd' => false,
'even' => true,
'depth' => 2,
'parent' => (object) $expectedLoop,
];
Expand Down Expand Up @@ -597,6 +601,8 @@ public function testAddingUncountableLoop()
'count' => null,
'first' => true,
'last' => null,
'odd' => false,
'even' => true,
'depth' => 1,
'parent' => null,
];
Expand All @@ -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()
Expand Down

0 comments on commit c443c83

Please sign in to comment.