diff --git a/README.md b/README.md index d993308..e80075c 100644 --- a/README.md +++ b/README.md @@ -35,9 +35,7 @@ Now let's have a look on how simple it is: use Bentools\ETL\EtlExecutor; $etl = (new EtlExecutor()) - ->transformWith(function (string $name) { - yield strtoupper($name); - }); + ->transformWith(fn (string $name) => strtoupper($name)); $singers = ['Bob Marley', 'Amy Winehouse']; $report = $etl->process($singers); @@ -128,7 +126,9 @@ $etl = (new EtlExecutor()) $etl->process('file:///tmp/cities.csv', $pdo); ``` -As you can see, you can use `EtlState.destination` to retrieve the second argument you passed yo `$etl->process()`. +As you can see: +- Your transformer can _yield_ values, in case 1 extracted item becomes several items to load +- You can use `EtlState.destination` to retrieve the second argument you passed yo `$etl->process()`. The `EtlState` object contains all elements relative to the state of your ETL workflow being running. diff --git a/src/EtlExecutor.php b/src/EtlExecutor.php index c46ca83..2c0b8a6 100644 --- a/src/EtlExecutor.php +++ b/src/EtlExecutor.php @@ -118,7 +118,8 @@ private function extract(EtlState &$state): Generator private function transform(mixed $item, EtlState $state): array { try { - $items = [...$this->transformer->transform($item, $state)]; + $transformed = $this->transformer->transform($item, $state); + $items = $transformed instanceof Generator ? [...$transformed] : [$transformed]; return $this->dispatch(new TransformEvent($state, $items))->items; } catch (SkipRequest|StopRequest $e) { diff --git a/src/Transformer/CallableTransformer.php b/src/Transformer/CallableTransformer.php index cd494f2..b4329df 100644 --- a/src/Transformer/CallableTransformer.php +++ b/src/Transformer/CallableTransformer.php @@ -6,7 +6,6 @@ use Bentools\ETL\EtlState; use Closure; -use Generator; final readonly class CallableTransformer implements TransformerInterface { @@ -15,10 +14,7 @@ public function __construct( ) { } - /** - * @return Generator - */ - public function transform(mixed $item, EtlState $state): Generator + public function transform(mixed $item, EtlState $state): mixed { return ($this->closure)($item, $state); } diff --git a/src/Transformer/NullTransformer.php b/src/Transformer/NullTransformer.php index 6b81f63..897502c 100644 --- a/src/Transformer/NullTransformer.php +++ b/src/Transformer/NullTransformer.php @@ -5,12 +5,11 @@ namespace Bentools\ETL\Transformer; use Bentools\ETL\EtlState; -use Generator; final readonly class NullTransformer implements TransformerInterface { - public function transform(mixed $item, EtlState $state): Generator + public function transform(mixed $item, EtlState $state): mixed { - yield $item; + return $item; } } diff --git a/src/Transformer/TransformerInterface.php b/src/Transformer/TransformerInterface.php index 0f09093..1905a9e 100644 --- a/src/Transformer/TransformerInterface.php +++ b/src/Transformer/TransformerInterface.php @@ -5,12 +5,8 @@ namespace Bentools\ETL\Transformer; use Bentools\ETL\EtlState; -use Generator; interface TransformerInterface { - /** - * @return Generator - */ - public function transform(mixed $item, EtlState $state): Generator; + public function transform(mixed $item, EtlState $state): mixed; } diff --git a/tests/Unit/EtlExecutorTest.php b/tests/Unit/EtlExecutorTest.php index 1e7493f..03b5740 100644 --- a/tests/Unit/EtlExecutorTest.php +++ b/tests/Unit/EtlExecutorTest.php @@ -10,13 +10,13 @@ use function expect; use function strtoupper; -it('basically works', function () { +it('basically works', function (callable $transformer) { $items = []; // Given $etl = (new EtlExecutor()) ->extractFrom(fn () => yield from ['foo', 'bar']) - ->transformWith(fn (mixed $value) => yield strtoupper($value)) + ->transformWith($transformer) ->loadInto(function (string $item) use (&$items) { $items[] = $item; }) @@ -31,4 +31,7 @@ ->and($report->nbLoadedItems)->toBe(2) ->and($report->getDuration())->toBeBetween(0, 1) ; +})->with(function () { + yield 'Return value' => fn (mixed $value) => strtoupper($value); + yield 'Generator' => fn (mixed $value) => yield strtoupper($value); }); diff --git a/tests/Unit/Transformer/NullTransformerTest.php b/tests/Unit/Transformer/NullTransformerTest.php index fbecd0c..9d470ef 100644 --- a/tests/Unit/Transformer/NullTransformerTest.php +++ b/tests/Unit/Transformer/NullTransformerTest.php @@ -8,7 +8,6 @@ use Bentools\ETL\Transformer\NullTransformer; use function expect; -use function iterator_to_array; it('yields the value as-is', function () { // Given @@ -19,5 +18,5 @@ $transformedItems = $transformer->transform('foo', $state); // Then - expect(iterator_to_array($transformedItems))->toBe(['foo']); + expect($transformedItems)->toBe('foo'); });