Skip to content

Commit

Permalink
Fix rename columns transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
leomarquine committed Nov 5, 2018
1 parent 5ec7c9c commit 4869a79
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
22 changes: 11 additions & 11 deletions src/Transformers/RenameColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Marquine\Etl\Transformers;

use Marquine\Etl\Row;

class RenameColumns extends Transformer
{
/**
Expand All @@ -21,19 +23,17 @@ class RenameColumns extends Transformer
];

/**
* Get the transformer handler.
* Transform the given row.
*
* @return callable
* @param \Marquine\Etl\Row $row
* @return void
*/
public function transform()
public function transform(Row $row)
{
return function ($row) {
foreach ($this->columns as $old => $new) {
$row[$new] = $row[$old];
unset($row[$old]);
}

return $row;
};
foreach ($this->columns as $old => $new) {
$value = $row->get($old);
$row->remove($old);
$row->set($new, $value);
}
}
}
13 changes: 8 additions & 5 deletions tests/Transformers/RenameColumnsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Transformers;

use Tests\TestCase;
use Marquine\Etl\Row;
use Marquine\Etl\Transformers\RenameColumns;

class RenameColumnsTest extends TestCase
Expand All @@ -11,19 +12,21 @@ class RenameColumnsTest extends TestCase
public function rename_column()
{
$data = [
['id' => '1', 'name' => 'John Doe', 'email_address' => 'johndoe@email.com'],
['id' => '2', 'name' => 'Jane Doe', 'email_address' => 'janedoe@email.com'],
new Row(['id' => '1', 'name' => 'John Doe', 'email_address' => 'johndoe@email.com']),
new Row(['id' => '2', 'name' => 'Jane Doe', 'email_address' => 'janedoe@email.com']),
];

$expected = [
['id' => '1', 'name' => 'John Doe', 'email' => 'johndoe@email.com'],
['id' => '2', 'name' => 'Jane Doe', 'email' => 'janedoe@email.com'],
new Row(['id' => '1', 'name' => 'John Doe', 'email' => 'johndoe@email.com']),
new Row(['id' => '2', 'name' => 'Jane Doe', 'email' => 'janedoe@email.com']),
];

$transformer = new RenameColumns;

$transformer->options(['columns' => ['email_address' => 'email']]);

$this->assertEquals($expected, array_map($transformer->transform(), $data));
array_map([$transformer, 'transform'], $data);

$this->assertEquals($expected, $data);
}
}

0 comments on commit 4869a79

Please sign in to comment.