Skip to content

Commit 596a07d

Browse files
committed
Rework rename_transliterate to use Symfony String
1 parent 6c89e49 commit 596a07d

6 files changed

Lines changed: 83 additions & 32 deletions

File tree

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,12 +360,6 @@ function rename_replace(string $search, string $replace) : Transformer\Rename\Re
360360
return new Transformer\Rename\RenameReplaceEntryStrategy($search, $replace);
361361
}
362362

363-
#[DocumentationDSL(module: Module::CORE, type: DSLType::TRANSFORMER)]
364-
function rename_transliterate() : Transformer\Rename\RenameTransliterateEntryStrategy
365-
{
366-
return new Transformer\Rename\RenameTransliterateEntryStrategy();
367-
}
368-
369363
#[DocumentationDSL(module: Module::CORE, type: DSLType::ENTRY)]
370364
function bool_entry(string $name, ?bool $value, ?Schema\Metadata $metadata = null) : Entry\BooleanEntry
371365
{

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,42 @@
44

55
namespace Flow\ETL\Transformer\Rename;
66

7+
use function Symfony\Component\String\u;
78
use Flow\ETL\{FlowContext, Row, Row\Entry};
9+
use Symfony\Component\String\Slugger\AsciiSlugger;
810

9-
final readonly class RenameCaseEntryStrategy implements RenameEntryStrategy
11+
final class RenameCaseEntryStrategy implements RenameEntryStrategy
1012
{
13+
private ?AsciiSlugger $slugger = null;
14+
1115
public function __construct(
12-
private Style $style,
16+
private readonly Style $style,
1317
) {
1418
}
1519

1620
public function rename(Row $row, Entry $entry, FlowContext $context) : Row
1721
{
1822
return match ($this->style) {
23+
Style::ASCII => $row->rename($entry->name(), u($entry->name())->ascii()->toString()),
24+
Style::CAMEL => $row->rename($entry->name(), u($entry->name())->camel()->toString()),
1925
Style::LOWER => $row->rename($entry->name(), \mb_strtolower($entry->name())),
26+
Style::SLUG => $row->rename($entry->name(), $this->slug($entry->name())),
27+
Style::TITLE => $row->rename($entry->name(), u($entry->name())->title()->toString()),
2028
Style::UPPER => $row->rename($entry->name(), \mb_strtoupper($entry->name())),
2129
Style::UCFIRST => $row->rename($entry->name(), $this->ucFirst($entry->name())),
2230
Style::UCWORDS => $row->rename($entry->name(), $this->ucWords($entry->name())),
2331
};
2432
}
2533

34+
private function slug(string $string) : string
35+
{
36+
if (null === $this->slugger) {
37+
$this->slugger = new AsciiSlugger();
38+
}
39+
40+
return $this->slugger->slug($string)->toString();
41+
}
42+
2643
private function ucFirst(string $string) : string
2744
{
2845
// Available from PHP 8.4+

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

Lines changed: 0 additions & 16 deletions
This file was deleted.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
enum Style : string
88
{
9+
case ASCII = 'ASCII';
10+
case CAMEL = 'CAMEL';
911
case LOWER = 'LOWER';
12+
case SLUG = 'SLUG';
13+
case TITLE = 'TITLE';
1014
case UCFIRST = 'UCFIRST';
1115
case UCWORDS = 'UCWORDS';
1216
case UPPER = 'UPPER';

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

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Flow\ETL\Tests\Integration\DataFrame;
66

77
use function Flow\ETL\DSL\{bool_entry, df, from_rows, int_entry, json_entry, ref, str_entry};
8-
use function Flow\ETL\DSL\{rename_replace, rename_style, rename_transliterate, row, rows};
8+
use function Flow\ETL\DSL\{rename_replace, rename_style, row, rows};
99
use Flow\ETL\{Function\StyleConverter\StringStyles,
1010
Transformer\Rename\Style};
1111
use Flow\ETL\Tests\FlowIntegrationTestCase;
@@ -83,6 +83,60 @@ public function test_rename_all_lower_case_i18n() : void
8383
);
8484
}
8585

86+
public function test_rename_all_to_ascii() : void
87+
{
88+
$rows = rows(row(int_entry('ÓSMY', 8)), row(int_entry('DZIEWIĄTY', 9)));
89+
90+
$ds = df()
91+
->read(from_rows($rows))
92+
->renameEach(rename_style(Style::ASCII))
93+
->renameEach(rename_style(Style::LOWER))
94+
->getEachAsArray();
95+
96+
self::assertEquals(
97+
[
98+
['osmy' => 8],
99+
['dziewiaty' => 9],
100+
],
101+
\iterator_to_array($ds)
102+
);
103+
}
104+
105+
public function test_rename_all_to_camel() : void
106+
{
107+
$rows = rows(row(int_entry('ósmy i dziewiąty', 89)));
108+
109+
$ds = df()
110+
->read(from_rows($rows))
111+
->renameEach(rename_style(Style::CAMEL))
112+
->getEachAsArray();
113+
114+
self::assertEquals(
115+
[
116+
['Ósmy i dziewiąty' => 89],
117+
],
118+
\iterator_to_array($ds)
119+
);
120+
}
121+
122+
public function test_rename_all_to_slug() : void
123+
{
124+
$rows = rows(row(int_entry('ÓSMY I DZIEWIĄTY', 89)));
125+
126+
$ds = df()
127+
->read(from_rows($rows))
128+
->renameEach(rename_style(Style::SLUG))
129+
->renameEach(rename_style(Style::LOWER))
130+
->getEachAsArray();
131+
132+
self::assertEquals(
133+
[
134+
['osmy-i-dziewiaty' => 89],
135+
],
136+
\iterator_to_array($ds)
137+
);
138+
}
139+
86140
public function test_rename_all_to_snake_case() : void
87141
{
88142
$rows = rows(row(int_entry('id', 1), str_entry('UserName', 'name'), bool_entry('isActive', true)), row(int_entry('id', 2), str_entry('UserName', 'name'), bool_entry('isActive', false)));
@@ -102,20 +156,18 @@ public function test_rename_all_to_snake_case() : void
102156
);
103157
}
104158

105-
public function test_rename_all_transliterate() : void
159+
public function test_rename_all_to_title() : void
106160
{
107-
$rows = rows(row(int_entry('ÓSMY', 8)), row(int_entry('DZIEWIĄTY', 9)));
161+
$rows = rows(row(int_entry('ósmy i dziewiąty', 89)));
108162

109163
$ds = df()
110164
->read(from_rows($rows))
111-
->renameEach(rename_transliterate())
112-
->renameEach(rename_style(Style::LOWER))
165+
->renameEach(rename_style(Style::TITLE))
113166
->getEachAsArray();
114167

115168
self::assertEquals(
116169
[
117-
['osmy' => 8],
118-
['dziewiaty' => 9],
170+
['Ósmy i dziewiąty' => 89],
119171
],
120172
\iterator_to_array($ds)
121173
);

web/landing/resources/dsl.json

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

0 commit comments

Comments
 (0)