Skip to content

Commit

Permalink
Test LazyCollection is lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber committed Aug 20, 2019
1 parent 4b185f5 commit e5cef0a
Show file tree
Hide file tree
Showing 2 changed files with 1,461 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/Illuminate/Support/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,12 @@ public function only($keys)
foreach ($original as $key => $value) {
if (array_key_exists($key, $keys)) {
yield $key => $value;

unset($keys[$key]);

if (empty($keys)) {
break;
}
}
}
}
Expand Down Expand Up @@ -944,22 +950,19 @@ public function replace($items)

return new static(function () use ($original, $items) {
$items = $this->getArrayableItems($items);
$usedItems = [];

foreach ($original as $key => $value) {
if (array_key_exists($key, $items)) {
yield $key => $items[$key];

$usedItems[$key] = true;
unset($items[$key]);
} else {
yield $key => $value;
}
}

foreach ($items as $key => $value) {
if (! array_key_exists($key, $usedItems)) {
yield $key => $value;
}
yield $key => $value;
}
});
}
Expand Down Expand Up @@ -1104,13 +1107,25 @@ public function chunk($size)
$iterator = $original->getIterator();

while ($iterator->valid()) {
$values = [];
$chunk = [];

while (true) {
$chunk[$iterator->key()] = $iterator->current();

for ($i = 0; $iterator->valid() && $i < $size; $i++, $iterator->next()) {
$values[$iterator->key()] = $iterator->current();
if (count($chunk) < $size) {
$iterator->next();

if (! $iterator->valid()) {
break;
}
} else {
break;
}
}

yield new static($values);
yield new static($chunk);

$iterator->next();
}
});
}
Expand Down
Loading

0 comments on commit e5cef0a

Please sign in to comment.