Skip to content

Commit

Permalink
Fix convert case transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
leomarquine committed Nov 5, 2018
1 parent 104f164 commit 798778f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 36 deletions.
49 changes: 29 additions & 20 deletions src/Transformers/ConvertCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Marquine\Etl\Transformers;

use Marquine\Etl\Row;
use InvalidArgumentException;

class ConvertCase extends Transformer
Expand All @@ -11,7 +12,7 @@ class ConvertCase extends Transformer
*
* @var array
*/
protected $columns;
protected $columns = [];

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

/**
* The int representation of the mode.
*
* @var int
*/
protected $conversionMode;

/**
* Properties that can be set via the options method.
*
Expand All @@ -37,33 +45,34 @@ class ConvertCase extends Transformer
];

/**
* Get the transformer handler.
* Initialize the step.
*
* @return callable
* @return void
*/
public function transform()
public function initialize()
{
$mode = $this->getConversionMode();

return function ($row) use ($mode) {
if ($this->columns) {
foreach ($this->columns as $column) {
$row[$column] = mb_convert_case($row[$column], $mode, $this->encoding);
}
} else {
foreach ($row as $column => $value) {
$row[$column] = mb_convert_case($value, $mode, $this->encoding);
}
}
$this->conversionMode = $this->getConversionMode();
}

return $row;
};
/**
* Transform the given row.
*
* @param \Marquine\Etl\Row $row
* @return void
*/
public function transform(Row $row)
{
$row->transform($this->columns, function ($column) {
return mb_convert_case($column, $this->conversionMode, $this->encoding);
});
}

/**
* Get the conversion mode.
*
* @return string
* @return int
*
* @throws \InvalidArgumentException
*/
protected function getConversionMode()
{
Expand All @@ -80,6 +89,6 @@ protected function getConversionMode()
return MB_CASE_TITLE;
}

throw new InvalidArgumentException('The provided conversion mode is not supported.');
throw new InvalidArgumentException("The conversion mode [{$this->mode}] is invalid.");
}
}
54 changes: 38 additions & 16 deletions tests/Transformers/ConvertCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,94 @@
namespace Tests\Transformers;

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

class ConvertCaseTest extends TestCase
{
protected $data = [
['id' => '1', 'name' => 'jane doe', 'email' => 'janedoe@email.com'],
['id' => '2', 'name' => 'JOHN DOE', 'email' => 'JOHNDOE@EMAIL.COM'],
];
protected function setUp()
{
parent::setUp();

$this->data = [
new Row(['id' => '1', 'name' => 'jane doe', 'email' => 'janedoe@email.com']),
new Row(['id' => '2', 'name' => 'JOHN DOE', 'email' => 'JOHNDOE@EMAIL.COM']),
];
}

/** @test */
public function lowercase()
{
$expected = [
['id' => '1', 'name' => 'jane doe', 'email' => 'janedoe@email.com'],
['id' => '2', 'name' => 'john doe', 'email' => 'johndoe@email.com'],
new Row(['id' => '1', 'name' => 'jane doe', 'email' => 'janedoe@email.com']),
new Row(['id' => '2', 'name' => 'john doe', 'email' => 'johndoe@email.com']),
];

$transformer = new ConvertCase;

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

$this->assertEquals($expected, array_map($transformer->transform(), $this->data));
$transformer->initialize();

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

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

/** @test */
public function uppercase()
{
$expected = [
['id' => '1', 'name' => 'JANE DOE', 'email' => 'JANEDOE@EMAIL.COM'],
['id' => '2', 'name' => 'JOHN DOE', 'email' => 'JOHNDOE@EMAIL.COM'],
new Row(['id' => '1', 'name' => 'JANE DOE', 'email' => 'JANEDOE@EMAIL.COM']),
new Row(['id' => '2', 'name' => 'JOHN DOE', 'email' => 'JOHNDOE@EMAIL.COM']),
];

$transformer = new ConvertCase;

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

$this->assertEquals($expected, array_map($transformer->transform(), $this->data));
$transformer->initialize();

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

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

/** @test */
public function titlecase()
{
$expected = [
['id' => '1', 'name' => 'Jane Doe', 'email' => 'Janedoe@email.com'],
['id' => '2', 'name' => 'John Doe', 'email' => 'Johndoe@email.com'],
new Row(['id' => '1', 'name' => 'Jane Doe', 'email' => 'Janedoe@email.com']),
new Row(['id' => '2', 'name' => 'John Doe', 'email' => 'Johndoe@email.com']),
];

$transformer = new ConvertCase;

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

$this->assertEquals($expected, array_map($transformer->transform(), $this->data));
$transformer->initialize();

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

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

/** @test */
public function custom_columns()
{
$expected = [
['id' => '1', 'name' => 'jane doe', 'email' => 'janedoe@email.com'],
['id' => '2', 'name' => 'john doe', 'email' => 'JOHNDOE@EMAIL.COM'],
new Row(['id' => '1', 'name' => 'jane doe', 'email' => 'janedoe@email.com']),
new Row(['id' => '2', 'name' => 'john doe', 'email' => 'JOHNDOE@EMAIL.COM']),
];

$transformer = new ConvertCase;

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

$this->assertEquals($expected, array_map($transformer->transform(), $this->data));
$transformer->initialize();

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

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

0 comments on commit 798778f

Please sign in to comment.