Skip to content

Commit 2cae774

Browse files
authored
Fixed renaming functionality to work with i18n characters (#1607)
1 parent 4fd67a7 commit 2cae774

2 files changed

Lines changed: 73 additions & 4 deletions

File tree

src/core/etl/src/Flow/ETL/Transformer/RenameAllCaseTransformer.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,56 @@ public function transform(Rows $rows, FlowContext $context) : Rows
2121
return $rows->map(function (Row $row) : Row {
2222
foreach ($row->entries()->all() as $entry) {
2323
if ($this->upper) {
24-
$row = $row->rename($entry->name(), \strtoupper($entry->name()));
24+
$row = $row->rename($entry->name(), \mb_strtoupper($entry->name()));
2525
}
2626

2727
if ($this->lower) {
28-
$row = $row->rename($entry->name(), \strtolower($entry->name()));
28+
$row = $row->rename($entry->name(), \mb_strtolower($entry->name()));
2929
}
3030

3131
if ($this->ucfirst) {
32-
$row = $row->rename($entry->name(), \ucfirst($entry->name()));
32+
$row = $row->rename($entry->name(), $this->ucFirst($entry->name()));
3333
}
3434

3535
if ($this->ucwords) {
36-
$row = $row->rename($entry->name(), \ucwords($entry->name()));
36+
$row = $row->rename($entry->name(), $this->ucWords($entry->name()));
3737
}
3838
}
3939

4040
return $row;
4141
});
4242
}
43+
44+
private function ucFirst(string $string) : string
45+
{
46+
// Available from PHP 8.4+
47+
if (\function_exists('mb_ucfirst')) {
48+
return \mb_ucfirst($string);
49+
}
50+
51+
$encoding = \mb_internal_encoding();
52+
53+
return \mb_strtoupper(\mb_substr($string, 0, 1, $encoding), $encoding) . \mb_substr($string, 1, null, $encoding);
54+
}
55+
56+
private function ucWords(string $string) : string
57+
{
58+
$result = '';
59+
$previousCharacter = ' ';
60+
61+
$encoding = \mb_internal_encoding();
62+
63+
for ($i = 0, $length = \mb_strlen($string, $encoding); $i < $length; $i++) {
64+
$currentCharacter = \mb_substr($string, $i, 1, $encoding);
65+
66+
if (' ' === $previousCharacter) {
67+
$currentCharacter = \mb_strtoupper($currentCharacter, $encoding);
68+
}
69+
70+
$result .= $currentCharacter;
71+
$previousCharacter = $currentCharacter;
72+
}
73+
74+
return $result;
75+
}
4376
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ public function test_rename_all_lower_case() : void
6161
);
6262
}
6363

64+
public function test_rename_all_lower_case_i18n() : void
65+
{
66+
$rows = rows(row(int_entry('ILOŚĆ PRZEDMIOTÓW', 0)), row(int_entry('ILOŚĆ PRZEDMIOTÓW', 10)));
67+
68+
$ds = df()
69+
->read(from_rows($rows))
70+
->renameAllLowerCase()
71+
->getEachAsArray();
72+
73+
self::assertEquals(
74+
[
75+
['ilość przedmiotów' => 0],
76+
['ilość przedmiotów' => 10],
77+
],
78+
\iterator_to_array($ds)
79+
);
80+
}
81+
6482
public function test_rename_all_to_snake_case() : void
6583
{
6684
$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)));
@@ -133,4 +151,22 @@ public function test_rename_all_upper_case_word() : void
133151
\iterator_to_array($ds)
134152
);
135153
}
154+
155+
public function test_rename_all_upper_case_word_i18n() : void
156+
{
157+
$rows = rows(row(int_entry('ósmy', 8)), row(int_entry('dziewiąty', 9)));
158+
159+
$ds = df()
160+
->read(from_rows($rows))
161+
->renameAllUpperCaseWord()
162+
->getEachAsArray();
163+
164+
self::assertEquals(
165+
[
166+
['Ósmy' => 8],
167+
['Dziewiąty' => 9],
168+
],
169+
\iterator_to_array($ds)
170+
);
171+
}
136172
}

0 commit comments

Comments
 (0)