diff --git a/src/core/etl/src/Flow/ETL/Row/Schema.php b/src/core/etl/src/Flow/ETL/Row/Schema.php index 9c625ebd4..6d755e0ad 100644 --- a/src/core/etl/src/Flow/ETL/Row/Schema.php +++ b/src/core/etl/src/Flow/ETL/Row/Schema.php @@ -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; diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/SchemaTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/SchemaTest.php index 09e47acca..06fc51b01 100644 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/SchemaTest.php +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/SchemaTest.php @@ -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(