diff --git a/src/core/etl/src/Flow/ETL/Row/Entry/JsonEntry.php b/src/core/etl/src/Flow/ETL/Row/Entry/JsonEntry.php index fa90a6e92..2adb0dfd3 100644 --- a/src/core/etl/src/Flow/ETL/Row/Entry/JsonEntry.php +++ b/src/core/etl/src/Flow/ETL/Row/Entry/JsonEntry.php @@ -67,11 +67,31 @@ public static function object(string $name, array $value) : self return $entry; } + public function __serialize() : array + { + return [ + 'name' => $this->name, + /** @phpstan-ignore-next-line */ + 'value' => \base64_encode(\gzcompress($this->value())), + 'object' => $this->object, + 'type' => $this->type, + ]; + } + public function __toString() : string { return $this->toString(); } + public function __unserialize(array $data) : void + { + $this->name = $data['name']; + /** @phpstan-ignore-next-line */ + $this->value = (array) \json_decode(\gzuncompress(\base64_decode($data['value'], true)), true, flags: \JSON_THROW_ON_ERROR); + $this->object = $data['object']; + $this->type = $data['type']; + } + public function definition() : Definition { return Definition::json($this->name, $this->type->nullable()); diff --git a/src/core/etl/src/Flow/ETL/Row/Entry/StringEntry.php b/src/core/etl/src/Flow/ETL/Row/Entry/StringEntry.php index 611a39d13..f8c043b6d 100644 --- a/src/core/etl/src/Flow/ETL/Row/Entry/StringEntry.php +++ b/src/core/etl/src/Flow/ETL/Row/Entry/StringEntry.php @@ -49,11 +49,29 @@ public static function uppercase(string $name, string $value) : self return new self($name, \mb_strtoupper($value)); } + public function __serialize() : array + { + return [ + 'name' => $this->name, + /** @phpstan-ignore-next-line */ + 'value' => \base64_encode(\gzcompress($this->value)), + 'type' => $this->type, + ]; + } + public function __toString() : string { return $this->toString(); } + public function __unserialize(array $data) : void + { + $this->name = $data['name']; + /** @phpstan-ignore-next-line */ + $this->value = \gzuncompress(\base64_decode($data['value'], true)); + $this->type = $data['type']; + } + public function definition() : Definition { return Definition::string($this->name, $this->type->nullable()); diff --git a/src/core/etl/src/Flow/ETL/Row/Entry/XMLEntry.php b/src/core/etl/src/Flow/ETL/Row/Entry/XMLEntry.php index 9304a20dc..509f8889c 100644 --- a/src/core/etl/src/Flow/ETL/Row/Entry/XMLEntry.php +++ b/src/core/etl/src/Flow/ETL/Row/Entry/XMLEntry.php @@ -38,12 +38,38 @@ public function __construct(private readonly string $name, \DOMDocument|string $ $this->type = type_object($this->value::class); } + public function __serialize() : array + { + return [ + 'name' => $this->name, + /** @phpstan-ignore-next-line */ + 'value' => \base64_encode(\gzcompress($this->value->saveXML())), + 'type' => $this->type, + ]; + } + public function __toString() : string { /** @phpstan-ignore-next-line */ return $this->value->saveXML(); } + public function __unserialize(array $data) : void + { + $this->name = $data['name']; + $this->type = $data['type']; + /** @phpstan-ignore-next-line */ + $xmlString = \gzuncompress(\base64_decode($data['value'], true)); + $doc = new \DOMDocument(); + + /** @phpstan-ignore-next-line */ + if (!@$doc->loadXML($xmlString)) { + throw new InvalidArgumentException(\sprintf('Given string "%s" is not valid XML', $xmlString)); + } + + $this->value = $doc; + } + public function definition() : Definition { return Definition::xml($this->ref(), $this->type->nullable()); diff --git a/src/core/etl/src/Flow/ETL/Row/Entry/XMLNodeEntry.php b/src/core/etl/src/Flow/ETL/Row/Entry/XMLNodeEntry.php index b5209d1c0..7571f3ab3 100644 --- a/src/core/etl/src/Flow/ETL/Row/Entry/XMLNodeEntry.php +++ b/src/core/etl/src/Flow/ETL/Row/Entry/XMLNodeEntry.php @@ -23,6 +23,16 @@ public function __construct(private readonly string $name, private readonly \DOM $this->type = type_object($this->value::class); } + public function __serialize() : array + { + return [ + 'name' => $this->name, + /* @phpstan-ignore-next-line */ + 'value' => \base64_encode(\gzcompress($this->toString())), + 'type' => $this->type, + ]; + } + public function __toString() : string { /** @@ -33,6 +43,21 @@ public function __toString() : string return $this->value->ownerDocument->saveXML($this->value); } + public function __unserialize(array $data) : void + { + $this->name = $data['name']; + $this->type = $data['type']; + + /* @phpstan-ignore-next-line */ + $nodeString = \gzuncompress(\base64_decode($data['value'], true)); + $domDocument = new \DOMDocument(); + /* @phpstan-ignore-next-line */ + @$domDocument->loadXML($nodeString); + + /* @phpstan-ignore-next-line */ + $this->value = (new \DOMDocument())->importNode($domDocument->documentElement, true); + } + public function definition() : Definition { return Definition::xml_node($this->ref(), $this->type->nullable()); diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Entry/JsonEntryTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Entry/JsonEntryTest.php index dfbd1089c..1a3b59608 100644 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Entry/JsonEntryTest.php +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Entry/JsonEntryTest.php @@ -177,4 +177,24 @@ public function test_returns_json_as_value() : void $this->assertEquals(\json_encode($items), $entry->value()); } + + public function test_serialization() : void + { + $entry = new JsonEntry('name', ['foo' => 1, 'bar' => ['foo' => 'foo', 'bar' => 'bar'], 'baz']); + + $serialized = \serialize($entry); + $unserialized = \unserialize($serialized); + + $this->assertTrue($entry->isEqual($unserialized)); + } + + public function test_serialization_of_json_objects() : void + { + $entry = JsonEntry::object('entry-name', ['id' => 1, 'name' => 'one']); + + $serialized = \serialize($entry); + $unserialized = \unserialize($serialized); + + $this->assertTrue($entry->isEqual($unserialized)); + } } diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Entry/StringEntryTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Entry/StringEntryTest.php index 78b73be30..649b1cd9a 100644 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Entry/StringEntryTest.php +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Entry/StringEntryTest.php @@ -68,10 +68,13 @@ public function test_renames_entry() : void public function test_serialization() : void { - $string = new StringEntry('name', 'some string'); + $string = new StringEntry('name', <<<'TXT' +This is some very long +multi-line string, including different values like: ąćżźą + +TXT); $serialized = \serialize($string); - /** @var StringEntry $unserialized */ $unserialized = \unserialize($serialized); $this->assertTrue($string->isEqual($unserialized)); diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Entry/XMLEntryTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Entry/XMLEntryTest.php index 2cf93c49e..ec1392f1c 100644 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Entry/XMLEntryTest.php +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Entry/XMLEntryTest.php @@ -133,4 +133,27 @@ public function test_is_equal(bool $equals, XMLEntry $entry, XMLEntry $nextEntry { $this->assertSame($equals, $entry->isEqual($nextEntry)); } + + public function test_serialization() : void + { + $entry = new XMLEntry('xml', <<<'XML' + + + + 1 + Foo + + + 2 + Bar + + + +XML); + + $serialized = \serialize($entry); + $unserialized = \unserialize($serialized); + + $this->assertTrue($entry->isEqual($unserialized)); + } } diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Entry/XMLNodeEntryTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Entry/XMLNodeEntryTest.php new file mode 100644 index 000000000..9d36677b1 --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Entry/XMLNodeEntryTest.php @@ -0,0 +1,21 @@ +createElement('testElement', 'This is a test')); + + $serialized = \serialize($entry); + $unserialized = \unserialize($serialized); + + $this->assertTrue($entry->isEqual($unserialized)); + } +}