Skip to content

Commit c443c83

Browse files
committed
Adds even and odd flags to the Loop variable
1 parent 5ce11d0 commit c443c83

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/Illuminate/View/Concerns/ManagesLoops.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function addLoop($data)
3333
'count' => $length,
3434
'first' => true,
3535
'last' => isset($length) ? $length == 1 : null,
36+
'odd' => false,
37+
'even' => true,
3638
'depth' => count($this->loopsStack) + 1,
3739
'parent' => $parent ? (object) $parent : null,
3840
];
@@ -51,6 +53,8 @@ public function incrementLoopIndices()
5153
'iteration' => $loop['iteration'] + 1,
5254
'index' => $loop['iteration'],
5355
'first' => $loop['iteration'] == 0,
56+
'odd' => ! $loop['odd'],
57+
'even' => ! $loop['even'],
5458
'remaining' => isset($loop['count']) ? $loop['remaining'] - 1 : null,
5559
'last' => isset($loop['count']) ? $loop['iteration'] == $loop['count'] - 1 : null,
5660
]);

tests/View/ViewFactoryTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,8 @@ public function testAddingLoops()
539539
'count' => 3,
540540
'first' => true,
541541
'last' => false,
542+
'odd' => false,
543+
'even' => true,
542544
'depth' => 1,
543545
'parent' => null,
544546
];
@@ -554,6 +556,8 @@ public function testAddingLoops()
554556
'count' => 4,
555557
'first' => true,
556558
'last' => false,
559+
'odd' => false,
560+
'even' => true,
557561
'depth' => 2,
558562
'parent' => (object) $expectedLoop,
559563
];
@@ -597,6 +601,8 @@ public function testAddingUncountableLoop()
597601
'count' => null,
598602
'first' => true,
599603
'last' => null,
604+
'odd' => false,
605+
'even' => true,
600606
'depth' => 1,
601607
'parent' => null,
602608
];
@@ -612,11 +618,19 @@ public function testIncrementingLoopIndices()
612618

613619
$factory->incrementLoopIndices();
614620

621+
$this->assertEquals(1, $factory->getLoopStack()[0]['iteration']);
622+
$this->assertEquals(0, $factory->getLoopStack()[0]['index']);
623+
$this->assertEquals(3, $factory->getLoopStack()[0]['remaining']);
624+
$this->assertTrue($factory->getLoopStack()[0]['odd']);
625+
$this->assertFalse($factory->getLoopStack()[0]['even']);
626+
615627
$factory->incrementLoopIndices();
616628

617629
$this->assertEquals(2, $factory->getLoopStack()[0]['iteration']);
618630
$this->assertEquals(1, $factory->getLoopStack()[0]['index']);
619631
$this->assertEquals(2, $factory->getLoopStack()[0]['remaining']);
632+
$this->assertFalse($factory->getLoopStack()[0]['odd']);
633+
$this->assertTrue($factory->getLoopStack()[0]['even']);
620634
}
621635

622636
public function testReachingEndOfLoop()

0 commit comments

Comments
 (0)