Skip to content

Commit 798778f

Browse files
committed
Fix convert case transformer
1 parent 104f164 commit 798778f

File tree

2 files changed

+67
-36
lines changed

2 files changed

+67
-36
lines changed

src/Transformers/ConvertCase.php

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Marquine\Etl\Transformers;
44

5+
use Marquine\Etl\Row;
56
use InvalidArgumentException;
67

78
class ConvertCase extends Transformer
@@ -11,7 +12,7 @@ class ConvertCase extends Transformer
1112
*
1213
* @var array
1314
*/
14-
protected $columns;
15+
protected $columns = [];
1516

1617
/**
1718
* The mode of the conversion.
@@ -27,6 +28,13 @@ class ConvertCase extends Transformer
2728
*/
2829
protected $encoding = 'utf-8';
2930

31+
/**
32+
* The int representation of the mode.
33+
*
34+
* @var int
35+
*/
36+
protected $conversionMode;
37+
3038
/**
3139
* Properties that can be set via the options method.
3240
*
@@ -37,33 +45,34 @@ class ConvertCase extends Transformer
3745
];
3846

3947
/**
40-
* Get the transformer handler.
48+
* Initialize the step.
4149
*
42-
* @return callable
50+
* @return void
4351
*/
44-
public function transform()
52+
public function initialize()
4553
{
46-
$mode = $this->getConversionMode();
47-
48-
return function ($row) use ($mode) {
49-
if ($this->columns) {
50-
foreach ($this->columns as $column) {
51-
$row[$column] = mb_convert_case($row[$column], $mode, $this->encoding);
52-
}
53-
} else {
54-
foreach ($row as $column => $value) {
55-
$row[$column] = mb_convert_case($value, $mode, $this->encoding);
56-
}
57-
}
54+
$this->conversionMode = $this->getConversionMode();
55+
}
5856

59-
return $row;
60-
};
57+
/**
58+
* Transform the given row.
59+
*
60+
* @param \Marquine\Etl\Row $row
61+
* @return void
62+
*/
63+
public function transform(Row $row)
64+
{
65+
$row->transform($this->columns, function ($column) {
66+
return mb_convert_case($column, $this->conversionMode, $this->encoding);
67+
});
6168
}
6269

6370
/**
6471
* Get the conversion mode.
6572
*
66-
* @return string
73+
* @return int
74+
*
75+
* @throws \InvalidArgumentException
6776
*/
6877
protected function getConversionMode()
6978
{
@@ -80,6 +89,6 @@ protected function getConversionMode()
8089
return MB_CASE_TITLE;
8190
}
8291

83-
throw new InvalidArgumentException('The provided conversion mode is not supported.');
92+
throw new InvalidArgumentException("The conversion mode [{$this->mode}] is invalid.");
8493
}
8594
}

tests/Transformers/ConvertCaseTest.php

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,72 +3,94 @@
33
namespace Tests\Transformers;
44

55
use Tests\TestCase;
6+
use Marquine\Etl\Row;
67
use Marquine\Etl\Transformers\ConvertCase;
78

89
class ConvertCaseTest extends TestCase
910
{
10-
protected $data = [
11-
['id' => '1', 'name' => 'jane doe', 'email' => 'janedoe@email.com'],
12-
['id' => '2', 'name' => 'JOHN DOE', 'email' => 'JOHNDOE@EMAIL.COM'],
13-
];
11+
protected function setUp()
12+
{
13+
parent::setUp();
14+
15+
$this->data = [
16+
new Row(['id' => '1', 'name' => 'jane doe', 'email' => 'janedoe@email.com']),
17+
new Row(['id' => '2', 'name' => 'JOHN DOE', 'email' => 'JOHNDOE@EMAIL.COM']),
18+
];
19+
}
1420

1521
/** @test */
1622
public function lowercase()
1723
{
1824
$expected = [
19-
['id' => '1', 'name' => 'jane doe', 'email' => 'janedoe@email.com'],
20-
['id' => '2', 'name' => 'john doe', 'email' => 'johndoe@email.com'],
25+
new Row(['id' => '1', 'name' => 'jane doe', 'email' => 'janedoe@email.com']),
26+
new Row(['id' => '2', 'name' => 'john doe', 'email' => 'johndoe@email.com']),
2127
];
2228

2329
$transformer = new ConvertCase;
2430

2531
$transformer->options(['mode' => 'lower']);
2632

27-
$this->assertEquals($expected, array_map($transformer->transform(), $this->data));
33+
$transformer->initialize();
34+
35+
array_map([$transformer, 'transform'], $this->data);
36+
37+
$this->assertEquals($expected, $this->data);
2838
}
2939

3040
/** @test */
3141
public function uppercase()
3242
{
3343
$expected = [
34-
['id' => '1', 'name' => 'JANE DOE', 'email' => 'JANEDOE@EMAIL.COM'],
35-
['id' => '2', 'name' => 'JOHN DOE', 'email' => 'JOHNDOE@EMAIL.COM'],
44+
new Row(['id' => '1', 'name' => 'JANE DOE', 'email' => 'JANEDOE@EMAIL.COM']),
45+
new Row(['id' => '2', 'name' => 'JOHN DOE', 'email' => 'JOHNDOE@EMAIL.COM']),
3646
];
3747

3848
$transformer = new ConvertCase;
3949

4050
$transformer->options(['mode' => 'upper']);
4151

42-
$this->assertEquals($expected, array_map($transformer->transform(), $this->data));
52+
$transformer->initialize();
53+
54+
array_map([$transformer, 'transform'], $this->data);
55+
56+
$this->assertEquals($expected, $this->data);
4357
}
4458

4559
/** @test */
4660
public function titlecase()
4761
{
4862
$expected = [
49-
['id' => '1', 'name' => 'Jane Doe', 'email' => 'Janedoe@email.com'],
50-
['id' => '2', 'name' => 'John Doe', 'email' => 'Johndoe@email.com'],
63+
new Row(['id' => '1', 'name' => 'Jane Doe', 'email' => 'Janedoe@email.com']),
64+
new Row(['id' => '2', 'name' => 'John Doe', 'email' => 'Johndoe@email.com']),
5165
];
5266

5367
$transformer = new ConvertCase;
5468

5569
$transformer->options(['mode' => 'title']);
5670

57-
$this->assertEquals($expected, array_map($transformer->transform(), $this->data));
71+
$transformer->initialize();
72+
73+
array_map([$transformer, 'transform'], $this->data);
74+
75+
$this->assertEquals($expected, $this->data);
5876
}
5977

6078
/** @test */
6179
public function custom_columns()
6280
{
6381
$expected = [
64-
['id' => '1', 'name' => 'jane doe', 'email' => 'janedoe@email.com'],
65-
['id' => '2', 'name' => 'john doe', 'email' => 'JOHNDOE@EMAIL.COM'],
82+
new Row(['id' => '1', 'name' => 'jane doe', 'email' => 'janedoe@email.com']),
83+
new Row(['id' => '2', 'name' => 'john doe', 'email' => 'JOHNDOE@EMAIL.COM']),
6684
];
6785

6886
$transformer = new ConvertCase;
6987

7088
$transformer->options(['columns' => ['name']]);
7189

72-
$this->assertEquals($expected, array_map($transformer->transform(), $this->data));
90+
$transformer->initialize();
91+
92+
array_map([$transformer, 'transform'], $this->data);
93+
94+
$this->assertEquals($expected, $this->data);
7395
}
7496
}

0 commit comments

Comments
 (0)