Skip to content

Commit 9da0fb6

Browse files
committed
Update callback handling in each and chunk methods
Refactored the each and chunk methods to break iteration if the callback returns false, improving control over message processing.
1 parent b144729 commit 9da0fb6

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/MessageQuery.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,13 @@ public function append(string $message, mixed $flags = null): int
9292
*/
9393
public function each(callable $callback, int $chunkSize = 10, int $startChunk = 1): void
9494
{
95-
$this->chunk(fn (MessageCollection $messages) => (
96-
$messages->each($callback)
97-
), $chunkSize, $startChunk);
95+
$this->chunk(function (MessageCollection $messages) use ($callback) {
96+
foreach ($messages as $key => $message) {
97+
if ($callback($message, $key) === false) {
98+
break;
99+
}
100+
}
101+
}, $chunkSize, $startChunk);
98102
}
99103

100104
/**
@@ -134,7 +138,10 @@ public function chunk(callable $callback, int $chunkSize = 10, int $startChunk =
134138
break;
135139
}
136140

137-
$callback($hydrated, $page);
141+
// If the callback returns false, break out.
142+
if ($callback($hydrated, $page) === false) {
143+
break;
144+
}
138145
}
139146

140147
// Restore the original state.

0 commit comments

Comments
 (0)