Skip to content

Commit 86d5767

Browse files
committed
Rework RenameReplaceEntryStrategy to allow multiple strings
1 parent 54532d8 commit 86d5767

10 files changed

Lines changed: 47 additions & 10 deletions

File tree

examples/topics/data_frame/cache/code.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use function Flow\ETL\DSL\{config_builder, data_frame, filesystem_cache, from_cache, ref, to_stream};
66
use Flow\ETL\Adapter\Http\DynamicExtractor\NextRequestFactory;
77
use Flow\ETL\Adapter\Http\PsrHttpClientDynamicExtractor;
8+
use Flow\ETL\Transformer\Rename\RenameReplaceEntryStrategy;
89
use Http\Client\Curl\Client;
910
use Nyholm\Psr7\Factory\Psr17Factory;
1011
use Psr\Http\Message\{RequestInterface, ResponseInterface};
@@ -41,7 +42,7 @@ public function create(?ResponseInterface $previousResponse = null) : ?RequestIn
4142
->withEntry('unpacked', ref('response_body')->jsonDecode())
4243
->select('unpacked')
4344
->withEntry('unpacked', ref('unpacked')->unpack())
44-
->renameAll('unpacked.', '')
45+
->renameEach(new RenameReplaceEntryStrategy('unpacked.', ''))
4546
->drop('unpacked')
4647
->select('name', 'html_url', 'blog', 'login', 'public_repos', 'followers', 'created_at')
4748
->write(to_stream(__DIR__ . '/output.txt', truncate: false))

examples/topics/data_frame/rename_columns/code.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use function Flow\ETL\DSL\{data_frame, from_array, to_stream};
6+
use Flow\ETL\Transformer\Rename\RenameReplaceEntryStrategy;
67

78
require __DIR__ . '/vendor/autoload.php';
89

@@ -13,7 +14,7 @@
1314
['id' => 3, 'name' => 'Jane', 'joined_id' => 3, 'joined_status' => 'active'],
1415
]))
1516
->rename('id', 'user_id')
16-
->renameAll('joined_', '')
17+
->renameEach(new RenameReplaceEntryStrategy('joined.', ''))
1718
->collect()
1819
->write(to_stream(__DIR__ . '/output.txt', truncate: false))
1920
->run();

examples/topics/data_reading/http_dynamic/code.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use function Flow\ETL\DSL\{data_frame, ref, to_stream};
66
use Flow\ETL\Adapter\Http\DynamicExtractor\NextRequestFactory;
77
use Flow\ETL\Adapter\Http\PsrHttpClientDynamicExtractor;
8+
use Flow\ETL\Transformer\Rename\RenameReplaceEntryStrategy;
89
use Http\Client\Curl\Client;
910
use Nyholm\Psr7\Factory\Psr17Factory;
1011
use Psr\Http\Message;
@@ -34,7 +35,7 @@ public function create(?Message\ResponseInterface $previousResponse = null) : ?M
3435
->read($from_github_api)
3536
->withEntry('unpacked', ref('response_body')->jsonDecode())
3637
->withEntry('unpacked', ref('unpacked')->unpack())
37-
->renameAll('unpacked.', '')
38+
->renameEach(new RenameReplaceEntryStrategy('unpacked.', ''))
3839
->drop('unpacked')
3940
->select('name', 'html_url', 'blog', 'login', 'public_repos', 'followers', 'created_at')
4041
->write(to_stream(__DIR__ . '/output.txt', truncate: false))

src/core/etl/src/Flow/ETL/DSL/functions.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,12 @@ function rename_style(Style $style) : Transformer\Rename\RenameCaseEntryStrategy
354354
return new Transformer\Rename\RenameCaseEntryStrategy($style);
355355
}
356356

357+
/**
358+
* @param array<string>|string $search
359+
* @param array<string>|string $replace
360+
*/
357361
#[DocumentationDSL(module: Module::CORE, type: DSLType::TRANSFORMER)]
358-
function rename_replace(string $search, string $replace) : Transformer\Rename\RenameReplaceEntryStrategy
362+
function rename_replace(string|array $search, string|array $replace) : Transformer\Rename\RenameReplaceEntryStrategy
359363
{
360364
return new Transformer\Rename\RenameReplaceEntryStrategy($search, $replace);
361365
}

src/core/etl/src/Flow/ETL/Row/Formatter/ASCIISchemaFormatter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Flow\ETL\PHP\Type\Type;
1111
use Flow\ETL\Schema;
1212
use Flow\ETL\Schema\SchemaFormatter;
13+
use Flow\ETL\Transformer\Rename\RenameReplaceEntryStrategy;
1314

1415
final readonly class ASCIISchemaFormatter implements SchemaFormatter
1516
{
@@ -24,7 +25,7 @@ public function format(Schema $schema) : string
2425
df()
2526
->read(from_array($schema->normalize()))
2627
->withEntry('type', ref('type')->unpack())
27-
->renameAll('type.', '')
28+
->renameEach(new RenameReplaceEntryStrategy('type.', ''))
2829
->rename('ref', 'name')
2930
->collect()
3031
->select('name', 'type', 'nullable', 'metadata')

src/core/etl/src/Flow/ETL/Transformer/Rename/RenameReplaceEntryStrategy.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88

99
final readonly class RenameReplaceEntryStrategy implements RenameEntryStrategy
1010
{
11+
/**
12+
* @param array<string>|string $search
13+
* @param array<string>|string $replace
14+
*/
1115
public function __construct(
12-
private string $search,
13-
private string $replace,
16+
private string|array $search,
17+
private string|array $replace,
1418
) {
1519
}
1620

src/core/etl/tests/Flow/ETL/Tests/Integration/DataFrame/RenameTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,29 @@ public function test_rename_all_lower_case_i18n() : void
8383
);
8484
}
8585

86+
public function test_rename_all_multiple() : void
87+
{
88+
$rows = rows(
89+
row(json_entry('array', ['id' => 1, 'name' => 'name', 'isActive' => true])),
90+
row(json_entry('array', ['id' => 2, 'name' => 'name', 'isActive' => false])),
91+
);
92+
93+
$ds = df()
94+
->read(from_rows($rows))
95+
->withEntry('row', ref('array')->unpack())
96+
->renameEach(rename_replace(['row.', 'isActive'], ['', 'active']))
97+
->drop('array')
98+
->getEachAsArray();
99+
100+
self::assertEquals(
101+
[
102+
['id' => 1, 'name' => 'name', 'active' => true],
103+
['id' => 2, 'name' => 'name', 'active' => false],
104+
],
105+
\iterator_to_array($ds)
106+
);
107+
}
108+
86109
public function test_rename_all_to_ascii() : void
87110
{
88111
$rows = rows(row(int_entry('ÓSMY', 8)), row(int_entry('DZIEWIĄTY', 9)));

src/core/etl/tests/Flow/ETL/Tests/Integration/Function/ArrayUnpackTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use function Flow\ETL\DSL\{from_array, ref, to_memory};
99
use Flow\ETL\Memory\ArrayMemory;
1010
use Flow\ETL\Tests\FlowTestCase;
11+
use Flow\ETL\Transformer\Rename\RenameReplaceEntryStrategy;
1112

1213
final class ArrayUnpackTest extends FlowTestCase
1314
{
@@ -23,7 +24,7 @@ public function test_array_unpack() : void
2324
)
2425
)
2526
->withEntry('array', ref('array')->unpack())
26-
->renameAll('array.', '')
27+
->renameEach(new RenameReplaceEntryStrategy('array.', ''))
2728
->drop('array')
2829
->write(to_memory($memory = new ArrayMemory()))
2930
->run();

web/landing/resources/dsl.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

web/landing/src/Flow/Website/Service/Github.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Flow\ETL\Adapter\Http\PsrHttpClientDynamicExtractor;
99
use Flow\ETL\Cache\Implementation\PSRSimpleCache;
1010
use Flow\ETL\Memory\ArrayMemory;
11+
use Flow\ETL\Transformer\Rename\RenameReplaceEntryStrategy;
1112
use Flow\Website\Factory\Github\ContributorsRequestFactory;
1213
use Http\Client\Curl\Client;
1314
use Http\Discovery\Psr17Factory;
@@ -55,7 +56,7 @@ public function contributors() : array
5556
->select('unpacked')
5657
->withEntry('data', ref('unpacked')->expand())
5758
->withEntry('data', ref('data')->unpack())
58-
->renameAll('data.', '')
59+
->renameEach(new RenameReplaceEntryStrategy('data.', ''))
5960
->drop('unpacked', 'data')
6061
->filter(not(ref('login')->endsWith(lit('[bot]'))))
6162
->filter(not(ref('login')->equals(lit('aeon-automation'))))

0 commit comments

Comments
 (0)