Skip to content

Commit

Permalink
Fixed serialization issues related to various problems
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech committed Dec 31, 2023
1 parent bae711f commit 7afad94
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/core/etl/src/Flow/ETL/Row/Entry/JsonEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
18 changes: 18 additions & 0 deletions src/core/etl/src/Flow/ETL/Row/Entry/StringEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
26 changes: 26 additions & 0 deletions src/core/etl/src/Flow/ETL/Row/Entry/XMLEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
25 changes: 25 additions & 0 deletions src/core/etl/src/Flow/ETL/Row/Entry/XMLNodeEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand All @@ -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());
Expand Down
20 changes: 20 additions & 0 deletions src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Entry/JsonEntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
23 changes: 23 additions & 0 deletions src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Entry/XMLEntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
<xml>
<root>
<item>
<id>1</id>
<name>Foo</name>
</item>
<item>
<id>2</id>
<name>Bar</name>
</item>
</root>
</xml>
XML);

$serialized = \serialize($entry);
$unserialized = \unserialize($serialized);

$this->assertTrue($entry->isEqual($unserialized));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

namespace Flow\ETL\Tests\Unit\Row\Entry;

use Flow\ETL\Row\Entry\XMLNodeEntry;
use PHPUnit\Framework\TestCase;

final class XMLNodeEntryTest extends TestCase
{
public function test_serialization() : void
{
$domDocument = new \DOMDocument();

$entry = new XMLNodeEntry('node', $domDocument->createElement('testElement', 'This is a test'));

$serialized = \serialize($entry);
$unserialized = \unserialize($serialized);

$this->assertTrue($entry->isEqual($unserialized));
}
}

0 comments on commit 7afad94

Please sign in to comment.