Skip to content

Commit

Permalink
Added Base64 serialiezr
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech committed Apr 2, 2024
1 parent 0e82ced commit f0b0954
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/core/etl/src/Flow/ETL/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Flow\ETL\Monitoring\Memory\Unit;
use Flow\ETL\Pipeline\Optimizer;
use Flow\ETL\Row\Factory\NativeEntryFactory;
use Flow\Serializer\{NativePHPSerializer, Serializer};
use Flow\Serializer\{Base64Serializer, NativePHPSerializer, Serializer};

final class ConfigBuilder
{
Expand Down Expand Up @@ -52,7 +52,7 @@ public function build() : Config
{
$this->id ??= \uniqid('flow_php', true);
$entryFactory = new NativeEntryFactory();
$this->serializer ??= new NativePHPSerializer();
$this->serializer ??= new Base64Serializer(new NativePHPSerializer());
$cachePath = \is_string(\getenv(Config::CACHE_DIR_ENV)) && \realpath(\getenv(Config::CACHE_DIR_ENV))
? \getenv(Config::CACHE_DIR_ENV)
: \sys_get_temp_dir() . '/flow_php/cache';
Expand Down
23 changes: 23 additions & 0 deletions src/core/etl/src/Flow/Serializer/Base64Serializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Flow\Serializer;

final class Base64Serializer implements Serializer
{
public function __construct(private readonly Serializer $serializer)
{
}

public function serialize(object $serializable) : string
{
return \base64_encode($this->serializer->serialize($serializable));
}

public function unserialize(string $serialized, string $class) : object
{
/** @phpstan-ignore-next-line */
return $this->serializer->unserialize(\base64_decode($serialized, true), $class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Flow\Serializer\Tests\Unit;

use function Flow\ETL\DSL\{bool_entry, datetime_entry, float_entry, int_entry, object_entry, row, rows, str_entry, struct_element, struct_entry, struct_type, type_int, type_string};
use Flow\ETL\{Row, Rows};
use Flow\Serializer\{Base64Serializer, NativePHPSerializer};
use PHPUnit\Framework\TestCase;

final class Base64SerializerTest extends TestCase
{
public function test_serializing_rows() : void
{
$rows = rows(
...\array_map(
fn () : Row => row(
int_entry('integer', 1),
str_entry('string', 'string'),
bool_entry('boolean', true),
datetime_entry('datetime', new \DateTimeImmutable('2022-01-01 00:00:00')),
str_entry('null', null),
float_entry('float', 0.12),
object_entry('object', new \ArrayIterator([1, 2, 3])),
struct_entry(
'struct',
['integer' => 1, 'string' => 'string'],
struct_type([
struct_element('integer', type_int()),
struct_element('string', type_string()),
])
)
),
\range(0, 100)
)
);

$serializer = new Base64Serializer(new NativePHPSerializer());

$serialized = $serializer->serialize($rows);

$unserialized = $serializer->unserialize($serialized, Rows::class);

self::assertEquals(
$rows,
$unserialized
);
}
}

0 comments on commit f0b0954

Please sign in to comment.