Skip to content

Commit 5a3faf4

Browse files
Merge pull request #41 from worksolutions/fix-iterator-behavior
Fix. Iterator must have rewind method to used as state pattern.
2 parents 9444255 + 30dd666 commit 5a3faf4

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Collections library for php language",
44
"minimum-stability": "dev",
55
"license": "MIT",
6-
"version": "1.1.3",
6+
"version": "1.1.4",
77
"authors": [
88
{
99
"name": "Maxim Sokolovsky",

src/WS/Utils/Collections/CollectionFactory.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ public static function fromStrict(array $values): Collection
7676
*/
7777
public static function fromIterable(iterable $iterable): Collection
7878
{
79+
if ($iterable instanceof IteratorAggregate) {
80+
/** @noinspection PhpUnhandledExceptionInspection */
81+
$iterable = $iterable->getIterator();
82+
}
7983
if (self::isStatePatternIterator($iterable)) {
80-
if ($iterable instanceof IteratorAggregate) {
81-
/** @noinspection PhpUnhandledExceptionInspection */
82-
$iterable = $iterable->getIterator();
83-
}
8484
if (!$iterable instanceof Iterator) {
8585
throw new UnsupportedException('Only Iterator interface can be applied to IteratorCollection');
8686
}
@@ -104,13 +104,16 @@ private static function isStatePatternIterator(iterable $iterable): bool
104104
if (!is_object($iterable)) {
105105
return false;
106106
}
107+
if (!method_exists($iterable, 'rewind')) {
108+
return false;
109+
}
107110
$i = 2;
108111
$lastItem = null;
109112
foreach ($iterable as $item) {
110113
if ($i === 0) {
111114
break;
112115
}
113-
if (is_object($item) && $item === $lastItem && method_exists($item, 'rewind')) {
116+
if (is_object($item) && $item === $lastItem) {
114117
return true;
115118
}
116119
$lastItem = $item;

0 commit comments

Comments
 (0)