Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
Make traversable wrapping only apply when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
robations committed Mar 1, 2016
1 parent 89a0424 commit 9003bde
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/Fusonic/Linq/Linq.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,13 @@ public function skip($count)
return new Linq([]);
}
}
if ($innerIterator instanceof \Iterator === false) {
// IteratorIterator wraps $innerIterator because it is Traversable but not an Iterator.
// (see https://bugs.php.net/bug.php?id=52280)
$innerIterator = new \IteratorIterator($innerIterator);
}

// IteratorIterator wraps $innerIterator because it might be Traversable but not an Iterator.
return new Linq(new \LimitIterator(new \IteratorIterator($innerIterator), $count, -1));
return new Linq(new \LimitIterator($innerIterator, $count, -1));
}

/**
Expand All @@ -135,9 +139,14 @@ public function take($count)
if ($count == 0) {
return new Linq([]);
}
$innerIterator = $this->iterator;
if ($innerIterator instanceof \Iterator === false) {
// IteratorIterator wraps $this->iterator because it is Traversable but not an Iterator.
// (see https://bugs.php.net/bug.php?id=52280)
$innerIterator = new \IteratorIterator($innerIterator);
}

// IteratorIterator wraps $this->iterator because it might be Traversable but not an Iterator.
return new Linq(new \LimitIterator(new \IteratorIterator($this->iterator), 0, $count));
return new Linq(new \LimitIterator($innerIterator, 0, $count));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Fusonic/Linq/Test/SkipTakeTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

require_once("TestBase.php");
require_once __DIR__ . '/TestIteratorAggregate.php';
require_once(__DIR__ . "/TestIteratorAggregate.php");

use Fusonic\Linq\Linq;

Expand Down

0 comments on commit 9003bde

Please sign in to comment.