From 1ef0770dde4cc7e6b6e7451ad6dd2744eede543b Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Mon, 23 Oct 2023 14:21:47 +0200 Subject: [PATCH] Rework `NativeEntryFactory` to be stateless (#635) --- .../etl/src/Flow/ETL/Row/EntryFactory.php | 2 +- .../ETL/Row/Factory/NativeEntryFactory.php | 19 ++--- .../Row/Factory/NativeEntryFactoryTest.php | 70 +++++++++---------- 3 files changed, 41 insertions(+), 50 deletions(-) diff --git a/src/core/etl/src/Flow/ETL/Row/EntryFactory.php b/src/core/etl/src/Flow/ETL/Row/EntryFactory.php index 86a4af2bc..3a0986744 100644 --- a/src/core/etl/src/Flow/ETL/Row/EntryFactory.php +++ b/src/core/etl/src/Flow/ETL/Row/EntryFactory.php @@ -11,5 +11,5 @@ */ interface EntryFactory extends Serializable { - public function create(string $entryName, mixed $value) : Entry; + public function create(string $entryName, mixed $value, ?Schema $schema = null) : Entry; } diff --git a/src/core/etl/src/Flow/ETL/Row/Factory/NativeEntryFactory.php b/src/core/etl/src/Flow/ETL/Row/Factory/NativeEntryFactory.php index f4ea14af4..4b60a607d 100644 --- a/src/core/etl/src/Flow/ETL/Row/Factory/NativeEntryFactory.php +++ b/src/core/etl/src/Flow/ETL/Row/Factory/NativeEntryFactory.php @@ -13,36 +13,27 @@ use Flow\ETL\Row\Schema; /** - * @implements EntryFactory + * @implements EntryFactory */ final class NativeEntryFactory implements EntryFactory { - public function __construct(private readonly ?Schema $schema = null) - { - } - public function __serialize() : array { - return [ - 'schema' => $this->schema, - ]; + return []; } public function __unserialize(array $data) : void { - $this->schema = $data['schema']; } /** - * @param mixed $value - * * @throws InvalidArgumentException * @throws \JsonException */ - public function create(string $entryName, mixed $value) : Entry + public function create(string $entryName, mixed $value, ?Schema $schema = null) : Entry { - if ($this->schema !== null) { - return $this->fromDefinition($this->schema->getDefinition($entryName), $value); + if ($schema !== null) { + return $this->fromDefinition($schema->getDefinition($entryName), $value); } if (\is_string($value)) { diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Factory/NativeEntryFactoryTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Factory/NativeEntryFactoryTest.php index 6943f5cad..4b6d75c1c 100644 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Factory/NativeEntryFactoryTest.php +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Factory/NativeEntryFactoryTest.php @@ -27,8 +27,8 @@ public function test_array_with_schema() : void { $this->assertEquals( Entry::array('e', [1, 2, 3]), - (new NativeEntryFactory(new Schema(Schema\Definition::array('e')))) - ->create('e', [1, 2, 3]) + (new NativeEntryFactory()) + ->create('e', [1, 2, 3], new Schema(Schema\Definition::array('e'))) ); } @@ -44,7 +44,7 @@ public function test_boolean_with_schema() : void { $this->assertEquals( Entry::boolean('e', false), - (new NativeEntryFactory(new Schema(Schema\Definition::boolean('e'))))->create('e', false) + (new NativeEntryFactory())->create('e', false, new Schema(Schema\Definition::boolean('e'))) ); } @@ -53,8 +53,8 @@ public function test_conversion_to_different_type_with_schema() : void $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage("Can't convert value into entry \"e\""); - (new NativeEntryFactory(new Schema(Schema\Definition::string('e')))) - ->create('e', 1); + (new NativeEntryFactory()) + ->create('e', 1, new Schema(Schema\Definition::string('e'))); } public function test_datetime() : void @@ -69,8 +69,8 @@ public function test_datetime_string_with_schema() : void { $this->assertEquals( Entry::datetime_string('e', '2022-01-01 00:00:00 UTC'), - (new NativeEntryFactory(new Schema(Schema\Definition::dateTime('e')))) - ->create('e', '2022-01-01 00:00:00 UTC') + (new NativeEntryFactory()) + ->create('e', '2022-01-01 00:00:00 UTC', new Schema(Schema\Definition::dateTime('e'))) ); } @@ -78,8 +78,8 @@ public function test_datetime_with_schema() : void { $this->assertEquals( Entry::datetime('e', $datetime = new \DateTimeImmutable('now')), - (new NativeEntryFactory(new Schema(Schema\Definition::dateTime('e')))) - ->create('e', $datetime) + (new NativeEntryFactory()) + ->create('e', $datetime, new Schema(Schema\Definition::dateTime('e'))) ); } @@ -88,16 +88,16 @@ public function test_enum_invalid_value_with_schema() : void $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage("Value \"not_valid\" can't be converted to " . BackedIntEnum::class . ' enum'); - (new NativeEntryFactory(new Schema(Schema\Definition::enum('e', BackedIntEnum::class)))) - ->create('e', 'not_valid'); + (new NativeEntryFactory()) + ->create('e', 'not_valid', new Schema(Schema\Definition::enum('e', BackedIntEnum::class))); } public function test_enum_with_schema() : void { $this->assertEquals( Entry::enum('e', BackedIntEnum::one), - (new NativeEntryFactory(new Schema(Schema\Definition::enum('e', BackedIntEnum::class)))) - ->create('e', 'one') + (new NativeEntryFactory()) + ->create('e', 'one', new Schema(Schema\Definition::enum('e', BackedIntEnum::class))) ); } @@ -113,7 +113,7 @@ public function test_float_with_schema() : void { $this->assertEquals( Entry::float('e', 1.1), - (new NativeEntryFactory(new Schema(Schema\Definition::float('e'))))->create('e', 1.1) + (new NativeEntryFactory())->create('e', 1.1, new Schema(Schema\Definition::float('e'))) ); } @@ -137,7 +137,7 @@ public function test_integer_with_schema() : void { $this->assertEquals( Entry::integer('e', 1), - (new NativeEntryFactory(new Schema(Schema\Definition::integer('e'))))->create('e', 1) + (new NativeEntryFactory())->create('e', 1, new Schema(Schema\Definition::integer('e'))) ); } @@ -177,7 +177,7 @@ public function test_json_array_with_schema() : void { $this->assertEquals( Entry::json('e', [['id' => 1]]), - (new NativeEntryFactory(new Schema(Schema\Definition::json('e'))))->create('e', [['id' => 1]]) + (new NativeEntryFactory())->create('e', [['id' => 1]], new Schema(Schema\Definition::json('e'))) ); } @@ -185,7 +185,7 @@ public function test_json_object_array_with_schema() : void { $this->assertEquals( Entry::json_object('e', ['id' => 1]), - (new NativeEntryFactory(new Schema(Schema\Definition::json('e'))))->create('e', ['id' => 1]) + (new NativeEntryFactory())->create('e', ['id' => 1], new Schema(Schema\Definition::json('e'))) ); } @@ -193,7 +193,7 @@ public function test_json_string_with_schema() : void { $this->assertEquals( Entry::json_string('e', '{"id": 1}'), - (new NativeEntryFactory(new Schema(Schema\Definition::json('e'))))->create('e', '{"id": 1}') + (new NativeEntryFactory())->create('e', '{"id": 1}', new Schema(Schema\Definition::json('e'))) ); } @@ -201,7 +201,7 @@ public function test_list_int_with_schema() : void { $this->assertEquals( Entry::list_of_int('e', [1, 2, 3]), - (new NativeEntryFactory(new Schema(Schema\Definition::list('e', ScalarType::integer))))->create('e', [1, 2, 3]) + (new NativeEntryFactory())->create('e', [1, 2, 3], new Schema(Schema\Definition::list('e', ScalarType::integer))) ); } @@ -210,15 +210,15 @@ public function test_list_int_with_schema_but_string_list() : void $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Field "e" conversion exception. Expected list of integer got different types.'); - (new NativeEntryFactory(new Schema(Schema\Definition::list('e', ScalarType::integer))))->create('e', ['1', '2', '3']); + (new NativeEntryFactory())->create('e', ['1', '2', '3'], new Schema(Schema\Definition::list('e', ScalarType::integer))); } public function test_list_of_datetime_with_schema() : void { $this->assertEquals( Entry::list_of_datetime('e', $list = [new \DateTimeImmutable('now'), new \DateTimeImmutable('tomorrow')]), - (new NativeEntryFactory(new Schema(Schema\Definition::list('e', ObjectType::of(\DateTimeInterface::class))))) - ->create('e', $list) + (new NativeEntryFactory()) + ->create('e', $list, new Schema(Schema\Definition::list('e', ObjectType::of(\DateTimeInterface::class)))) ); } @@ -242,8 +242,8 @@ public function test_list_of_string_datetime_with_schema() : void { $this->assertEquals( Entry::list_of_datetime('e', [new \DateTimeImmutable('2022-01-01 00:00:00 UTC'), new \DateTimeImmutable('2022-01-01 00:00:00 UTC')]), - (new NativeEntryFactory(new Schema(Schema\Definition::list('e', ObjectType::of(\DateTimeInterface::class))))) - ->create('e', ['2022-01-01 00:00:00 UTC', '2022-01-01 00:00:00 UTC']) + (new NativeEntryFactory()) + ->create('e', ['2022-01-01 00:00:00 UTC', '2022-01-01 00:00:00 UTC'], new Schema(Schema\Definition::list('e', ObjectType::of(\DateTimeInterface::class)))) ); } @@ -285,12 +285,12 @@ public function test_null_with_schema() : void { $this->assertEquals( Entry::null('e'), - (new NativeEntryFactory(new Schema(Schema\Definition::null('e'))))->create('e', null) + (new NativeEntryFactory())->create('e', null, new Schema(Schema\Definition::null('e'))) ); $this->assertEquals( Entry::null('e'), - (new NativeEntryFactory(new Schema(Schema\Definition::string('e', true))))->create('e', null) + (new NativeEntryFactory())->create('e', null, new Schema(Schema\Definition::string('e', true))) ); } @@ -306,8 +306,8 @@ public function test_object_with_schema() : void { $this->assertEquals( Entry::object('e', $object = new \ArrayObject([1, 2, 3])), - (new NativeEntryFactory(new Schema(Schema\Definition::object('e')))) - ->create('e', $object) + (new NativeEntryFactory()) + ->create('e', $object, new Schema(Schema\Definition::object('e'))) ); } @@ -323,7 +323,7 @@ public function test_string_with_schema() : void { $this->assertEquals( Entry::string('e', 'string'), - (new NativeEntryFactory(new Schema(Schema\Definition::string('e'))))->create('e', 'string') + (new NativeEntryFactory())->create('e', 'string', new Schema(Schema\Definition::string('e'))) ); } @@ -364,7 +364,7 @@ public function test_uuid_string_with_uuid_definition_provided() : void { $this->assertEquals( Entry::uuid('e', $uuid = '00000000-0000-0000-0000-000000000000'), - (new NativeEntryFactory(new Schema(Schema\Definition::uuid('e'))))->create('e', $uuid) + (new NativeEntryFactory())->create('e', $uuid, new Schema(Schema\Definition::uuid('e'))) ); } @@ -373,8 +373,8 @@ public function test_with_empty_schema() : void $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('There is no definition for "e" in the schema.'); - (new NativeEntryFactory(new Schema())) - ->create('e', '1'); + (new NativeEntryFactory()) + ->create('e', '1', new Schema()); } public function test_with_schema_for_different_entry() : void @@ -382,8 +382,8 @@ public function test_with_schema_for_different_entry() : void $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('There is no definition for "diff" in the schema.'); - (new NativeEntryFactory(new Schema(Schema\Definition::string('e')))) - ->create('diff', '1'); + (new NativeEntryFactory()) + ->create('diff', '1', new Schema(Schema\Definition::string('e'))); } public function test_xml_from_dom_document() : void @@ -408,7 +408,7 @@ public function test_xml_string_with_xml_definition_provided() : void { $this->assertEquals( Entry::xml('e', $xml = '123'), - (new NativeEntryFactory(new Schema(Schema\Definition::xml('e'))))->create('e', $xml) + (new NativeEntryFactory())->create('e', $xml, new Schema(Schema\Definition::xml('e'))) ); } }