-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a handy CallbackWriter to write with a Closure (#120)
* Add a handy CallbackWriter to write with a Closure * Fixed missing comment on CallbackWriter
- Loading branch information
1 parent
4be935c
commit b333150
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yokai\Batch\Job\Item\Writer; | ||
|
||
use Yokai\Batch\Job\Item\ItemWriterInterface; | ||
|
||
/** | ||
* An {@see ItemWriterInterface} that write items with a {@see Closure} provided at construction. | ||
* | ||
* Provided {@see Closure} must accept items to write and must return nothing. | ||
*/ | ||
final class CallbackWriter implements ItemWriterInterface | ||
{ | ||
public function __construct( | ||
private \Closure $callback, | ||
) { | ||
} | ||
|
||
public function write(iterable $items): void | ||
{ | ||
($this->callback)($items); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yokai\Batch\Tests\Job\Item\Writer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yokai\Batch\Job\Item\Writer\CallbackWriter; | ||
|
||
class CallbackWriterTest extends TestCase | ||
{ | ||
public function testWrite(): void | ||
{ | ||
$saveditems = []; | ||
$writer = new CallbackWriter(function (array $items) use (&$saveditems) { | ||
$saveditems = [...$saveditems, ...$items]; | ||
}); | ||
$writer->write([1, 2, 3]); | ||
$writer->write([4, 5, 6]); | ||
|
||
self::assertSame([1, 2, 3, 4, 5, 6], $saveditems); | ||
} | ||
} |