Skip to content

Commit

Permalink
Added RowOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed May 28, 2022
1 parent 7ced376 commit 1c82549
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ vendor
.idea

node_modules
.usdocker
.usdocker
.phpunit.result.cache
80 changes: 80 additions & 0 deletions src/RowOutput.php
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;
}
}
90 changes: 90 additions & 0 deletions tests/RowOutputTest.php
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"));
}
}
4 changes: 0 additions & 4 deletions tests/RowValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
use ByJG\AnyDataset\Core\RowValidator;
use PHPUnit\Framework\TestCase;

require_once "Sample/ModelPublic.php";
require_once "Sample/ModelGetter.php";
require_once "Sample/ModelPropertyPattern.php";

class RowValidatorTest extends TestCase
{

Expand Down

0 comments on commit 1c82549

Please sign in to comment.