-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
172 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ vendor | |
.idea | ||
|
||
node_modules | ||
.usdocker | ||
.usdocker | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace ByJG\AnyDataset\Core; | ||
use Closure; | ||
|
||
class RowOutput | ||
{ | ||
const FORMAT = 'format'; | ||
const CUSTOM = 'custom'; | ||
|
||
protected $fieldList = []; | ||
|
||
public static function getInstance() | ||
{ | ||
return new RowOutput(); | ||
} | ||
|
||
public function print($row, $field) | ||
{ | ||
if (!isset($this->fieldList[$field])) { | ||
return $row->get($field); | ||
} | ||
|
||
$data = $this->fieldList[$field]; | ||
|
||
switch ($data[0]) { | ||
case self::FORMAT: | ||
return $this->formatPattern($row, $field, $data[1]); | ||
case self::CUSTOM: | ||
return $this->formatCustom($row, $field, $data[1]); | ||
} | ||
} | ||
|
||
public function apply($row) | ||
{ | ||
foreach ($this->fieldList as $key => $value) { | ||
$row->set($key, $this->print($row, $key)); | ||
} | ||
} | ||
|
||
protected function formatPattern($row, $field, $pattern) | ||
{ | ||
$rowParsed = $row->toArray(); | ||
foreach ($rowParsed as $key => $value) { | ||
$rowParsed['{' . $key . '}'] = $value; | ||
unset($rowParsed[$key]); | ||
} | ||
|
||
return strtr($pattern, $rowParsed); | ||
} | ||
|
||
protected function formatCustom($row, $field, $closure) | ||
{ | ||
return $closure($row, $field, $row->get($field)); | ||
} | ||
|
||
/** | ||
* @param string $field | ||
* @param string $pattern | ||
* @return RowOutput | ||
*/ | ||
public function addFormat($field, $pattern) | ||
{ | ||
$this->fieldList[$field] = [ self::FORMAT, $pattern ]; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Undocumented function | ||
* | ||
* @param string $field | ||
* @param Closure $closure | ||
* @return RowOutput | ||
*/ | ||
public function addCustomFormat($field, Closure $closure) | ||
{ | ||
$this->fieldList[$field] = [ self::CUSTOM, $closure ]; | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace Tests\AnyDataset\Dataset; | ||
|
||
use ByJG\AnyDataset\Core\Row; | ||
use ByJG\AnyDataset\Core\RowValidator; | ||
use PHPUnit\Framework\TestCase; | ||
use ByJG\AnyDataset\Core\RowOutput; | ||
|
||
class RowOutputTest extends TestCase | ||
{ | ||
|
||
public function testFormatPattern() | ||
{ | ||
$row1 = new Row([ | ||
"field1" => 10, | ||
"field2" => "test", | ||
"field3" => 20.30, | ||
"field4" => "2021-11-20" | ||
]); | ||
|
||
$row2 = new Row([ | ||
"field1" => 1, | ||
"field2" => "OK", | ||
"field3" => 3, | ||
]); | ||
|
||
$formatter = RowOutput::getInstance() | ||
->addFormat("field1", "Test {field1}") | ||
->addFormat("field2", "Showing {field2} and {field3}"); | ||
|
||
$this->assertEquals("Test 10", $formatter->print($row1, "field1")); | ||
$this->assertEquals("Showing test and 20.3", $formatter->print($row1, "field2")); | ||
$this->assertEquals("20.30", $formatter->print($row1, "field3")); | ||
|
||
$this->assertEquals("Test 1", $formatter->print($row2, "field1")); | ||
$this->assertEquals("Showing OK and 3", $formatter->print($row2, "field2")); | ||
$this->assertEquals("3", $formatter->print($row2, "field3")); | ||
} | ||
|
||
public function testFormatCustom() | ||
{ | ||
$row = new Row([ | ||
"field1" => 10, | ||
"field2" => "test", | ||
"field3" => 20.30, | ||
"field4" => "2021-11-20" | ||
]); | ||
|
||
$formatter = RowOutput::getInstance() | ||
->addCustomFormat("field1", function ($row, $field, $value) { | ||
return $value * 4; | ||
}) | ||
->addCustomFormat("field3", function ($row, $field, $value) { | ||
return "$field x 3 = " . ($value * 3); | ||
}); | ||
|
||
$this->assertEquals("40", $formatter->print($row, "field1")); | ||
$this->assertEquals("field3 x 3 = 60.9", $formatter->print($row, "field3")); | ||
$this->assertEquals("2021-11-20", $formatter->print($row, "field4")); | ||
} | ||
|
||
public function testApply() | ||
{ | ||
$row = new Row([ | ||
"field1" => 10, | ||
"field2" => "test", | ||
"field3" => 20.30, | ||
"field4" => "2021-11-20" | ||
]); | ||
|
||
$formatter = RowOutput::getInstance() | ||
->addFormat("field1", "Value: {field1}") | ||
->addCustomFormat("field3", function ($row, $field, $value) { | ||
return "$field x 3 = " . ($value * 3); | ||
}); | ||
|
||
$this->assertEquals("10", $row->get("field1")); | ||
$this->assertEquals("test", $row->get("field2")); | ||
$this->assertEquals("20.30", $row->get("field3")); | ||
$this->assertEquals("2021-11-20", $row->get("field4")); | ||
|
||
$formatter->apply($row); | ||
|
||
$this->assertEquals("Value: 10", $row->get("field1")); | ||
$this->assertEquals("test", $row->get("field2")); | ||
$this->assertEquals("field3 x 3 = 60.9", $row->get("field3")); | ||
$this->assertEquals("2021-11-20", $row->get("field4")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters