Skip to content

Commit

Permalink
Rework NativeEntryFactory to be stateless (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd authored Oct 23, 2023
1 parent 5660a30 commit 1ef0770
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/Row/EntryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
19 changes: 5 additions & 14 deletions src/core/etl/src/Flow/ETL/Row/Factory/NativeEntryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,27 @@
use Flow\ETL\Row\Schema;

/**
* @implements EntryFactory<array{schema: ?Schema}>
* @implements EntryFactory<array>
*/
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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')))
);
}

Expand All @@ -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')))
);
}

Expand All @@ -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
Expand All @@ -69,17 +69,17 @@ 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')))
);
}

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')))
);
}

Expand All @@ -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)))
);
}

Expand All @@ -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')))
);
}

Expand All @@ -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')))
);
}

Expand Down Expand Up @@ -177,31 +177,31 @@ 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')))
);
}

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')))
);
}

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')))
);
}

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)))
);
}

Expand All @@ -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))))
);
}

Expand All @@ -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))))
);
}

Expand Down Expand Up @@ -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)))
);
}

Expand All @@ -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')))
);
}

Expand All @@ -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')))
);
}

Expand Down Expand Up @@ -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')))
);
}

Expand All @@ -373,17 +373,17 @@ 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
{
$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
Expand All @@ -408,7 +408,7 @@ public function test_xml_string_with_xml_definition_provided() : void
{
$this->assertEquals(
Entry::xml('e', $xml = '<root><foo>1</foo><bar>2</bar><baz>3</baz></root>'),
(new NativeEntryFactory(new Schema(Schema\Definition::xml('e'))))->create('e', $xml)
(new NativeEntryFactory())->create('e', $xml, new Schema(Schema\Definition::xml('e')))
);
}
}

0 comments on commit 1ef0770

Please sign in to comment.