Skip to content

Commit

Permalink
Added Schema::keep - to keep selected schema entries (#996)
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech authored Feb 20, 2024
1 parent c13cf3f commit 7fd9cc7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/core/etl/src/Flow/ETL/Row/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,29 @@ public function gracefulRemove(string|Reference ...$entries) : self
return $this;
}

public function keep(string|Reference ...$entries) : self
{
$refs = References::init(...$entries);

$definitions = [];

foreach ($entries as $entry) {
if (!$this->findDefinition($entry)) {
throw new SchemaDefinitionNotFoundException((string) $entry);
}
}

foreach ($this->definitions as $definition) {
if ($refs->has($definition->entry())) {
$definitions[] = $definition;
}
}

$this->setDefinitions(...$definitions);

return $this;
}

public function merge(self $schema) : self
{
$newDefinitions = $this->definitions;
Expand Down
30 changes: 30 additions & 0 deletions src/core/etl/tests/Flow/ETL/Tests/Unit/Row/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,36 @@ public function test_graceful_remove_non_existing_definition() : void
);
}

public function test_keep_non_existing_entries() : void
{
$this->expectException(SchemaDefinitionNotFoundException::class);

schema(
int_schema('id'),
str_schema('name'),
str_schema('surname'),
str_schema('email'),
)->keep('not-existing');
}

public function test_keep_selected_entries() : void
{
$schema = schema(
int_schema('id'),
str_schema('name'),
str_schema('surname'),
str_schema('email'),
);

$this->assertEquals(
schema(
str_schema('name'),
str_schema('surname'),
),
$schema->keep('name', 'surname')
);
}

public function test_making_whole_schema_nullable() : void
{
$schema = new Schema(
Expand Down

0 comments on commit 7fd9cc7

Please sign in to comment.