-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
171 additions
and
3 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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BenTools\ETL\Recipe; | ||
|
||
use BenTools\ETL\EtlExecutor; | ||
use BenTools\ETL\EventDispatcher\Event\BeforeLoadEvent; | ||
use BenTools\ETL\EventDispatcher\Event\ExtractEvent; | ||
use Closure; | ||
use InvalidArgumentException; | ||
|
||
use function in_array; | ||
use function sprintf; | ||
|
||
final class FilterRecipe extends Recipe | ||
{ | ||
private const EVENTS_CLASSES = [ExtractEvent::class, BeforeLoadEvent::class]; | ||
|
||
public function __construct( | ||
private readonly Closure $filter, | ||
private readonly string $eventClass = ExtractEvent::class, | ||
private readonly int $priority = 0, | ||
private readonly FilterRecipeMode $mode = FilterRecipeMode::INCLUDE, | ||
) { | ||
if (!in_array($this->eventClass, self::EVENTS_CLASSES)) { | ||
throw new InvalidArgumentException(sprintf('Can only filter on ExtractEvent / LoadEvent, not %s', $this->eventClass)); | ||
} | ||
} | ||
|
||
public function decorate(EtlExecutor $executor): EtlExecutor | ||
{ | ||
return match ($this->eventClass) { | ||
ExtractEvent::class => $executor->onExtract($this(...), $this->priority), | ||
BeforeLoadEvent::class => $executor->onBeforeLoad($this(...), $this->priority), | ||
default => $executor, | ||
}; | ||
} | ||
|
||
public function __invoke(ExtractEvent|BeforeLoadEvent $event): void | ||
{ | ||
$matchFilter = !($this->filter)($event->item, $event->state); | ||
if (FilterRecipeMode::EXCLUDE === $this->mode) { | ||
$matchFilter = !$matchFilter; | ||
} | ||
|
||
if ($matchFilter) { | ||
$event->state->skip(); | ||
} | ||
} | ||
} |
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BenTools\ETL\Recipe; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
enum FilterRecipeMode | ||
{ | ||
case INCLUDE; | ||
case EXCLUDE; | ||
} |
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,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BenTools\ETL\Tests\Unit\Recipe; | ||
|
||
use BenTools\ETL\EventDispatcher\Event\BeforeLoadEvent; | ||
use BenTools\ETL\EventDispatcher\Event\ExtractEvent; | ||
use BenTools\ETL\EventDispatcher\Event\LoadEvent; | ||
use BenTools\ETL\Recipe\FilterRecipe; | ||
use InvalidArgumentException; | ||
|
||
use function BenTools\ETL\skipWhen; | ||
use function BenTools\ETL\withRecipe; | ||
use function expect; | ||
use function sprintf; | ||
use function str_contains; | ||
use function strtoupper; | ||
|
||
it('filters items (on an exclude-list basis)', function (?string $eventClass, array $expectedResult) { | ||
// Given | ||
$skipItems = ['apple', 'BANANA']; | ||
$executor = withRecipe( | ||
skipWhen( | ||
fn ($item) => !in_array($item, $skipItems, true), | ||
$eventClass, | ||
), | ||
) | ||
->transformWith(fn ($item) => strtoupper($item)); | ||
|
||
// When | ||
$report = $executor->process(['banana', 'apple', 'strawberry', 'BANANA', 'APPLE', 'STRAWBERRY']); | ||
|
||
// Then | ||
expect($report->output)->toBe($expectedResult); | ||
})->with(function () { | ||
yield [null, ['APPLE', 'BANANA']]; | ||
yield [ExtractEvent::class, ['APPLE', 'BANANA']]; | ||
yield [BeforeLoadEvent::class, ['BANANA', 'BANANA']]; | ||
}); | ||
|
||
it('filters items (on an allow-list basis)', function (?string $eventClass, array $expectedResult) { | ||
// Given | ||
$executor = withRecipe( | ||
new FilterRecipe( | ||
fn (string $item) => str_contains($item, 'b') || str_contains($item, 'B'), | ||
), | ||
) | ||
->transformWith(fn ($item) => strtoupper($item)); | ||
|
||
// When | ||
$report = $executor->process(['banana', 'apple', 'strawberry', 'BANANA', 'APPLE', 'STRAWBERRY']); | ||
|
||
// Then | ||
expect($report->output)->toBe($expectedResult); | ||
})->with(function () { | ||
yield [null, ['BANANA', 'STRAWBERRY', 'BANANA', 'STRAWBERRY']]; | ||
yield [ExtractEvent::class, ['BANANA', 'STRAWBERRY', 'BANANA', 'STRAWBERRY']]; | ||
yield [BeforeLoadEvent::class, ['BANANA', 'STRAWBERRY', 'BANANA', 'STRAWBERRY']]; | ||
}); | ||
|
||
it('does not accept other types of events', function () { | ||
new FilterRecipe(fn () => '', LoadEvent::class); | ||
})->throws( | ||
InvalidArgumentException::class, | ||
sprintf('Can only filter on ExtractEvent / LoadEvent, not %s', LoadEvent::class), | ||
); |