Skip to content

Commit

Permalink
port change forward
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Mar 5, 2018
1 parent d217a20 commit 49770ec
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Illuminate/View/Concerns/ManagesLoops.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\View\Concerns;

use Countable;
use Traversable;
use Illuminate\Support\Arr;

trait ManagesLoops
Expand All @@ -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);

Expand Down
22 changes: 22 additions & 0 deletions tests/View/ViewFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 49770ec

Please sign in to comment.