Skip to content

Commit a5654f2

Browse files
authored
Merge pull request #12 from byjg/rowformatter
Added RowOutput
2 parents 7ced376 + 759d81e commit a5654f2

File tree

7 files changed

+279
-9
lines changed

7 files changed

+279
-9
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ vendor
66
.idea
77

88
node_modules
9-
.usdocker
9+
.usdocker
10+
.phpunit.result.cache

README.md

+88-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,46 @@ while ($iterator->hasNext()) {
7777
}
7878
```
7979

80-
### Validate Field contents
80+
or
81+
82+
```php
83+
foreach ($iterator as $row) {
84+
print_r($row->get("field1"));
85+
}
86+
```
87+
88+
## Additional Classes
89+
90+
### RowOutpout - Format Field Output
91+
92+
This class defines custom format for the field output.
93+
94+
```php
95+
<?php
96+
$output = RowOutput::getInstance()
97+
->addFormat("field1", "Test {field1}")
98+
->addFormat("field2", "Showing {} and {field3}");
99+
->addCustomFormat("field3", function ($row, $field, $value) {
100+
// return the formatted output.
101+
// $row: The row object with all values
102+
// $field: The field has been processed
103+
// $value: The field value
104+
});
105+
106+
// This will output the field1 formatted:
107+
echo $output->print($row, "field1");
108+
109+
// This will apply the format defintion to all fields at once:
110+
$ouput->apply($row);
111+
```
112+
113+
Notes about the format pattern:
114+
115+
- `{}` represents the current value
116+
- `{.}` represents the field name
117+
- `{field_name}` return the value of $row->get(field_name)
118+
119+
### RowValidator - Validate Field contents
81120

82121
```php
83122
<?php
@@ -93,6 +132,54 @@ $validator = RowValidator::getInstance()
93132
$validator->validate($row) // Will return an array with the error messages. Empty array if not errors.
94133
```
95134

135+
## Formatters
136+
137+
AnyDataset comes with an extensible set to format the AnyDataset. The interface is:
138+
139+
```php
140+
namespace ByJG\AnyDataset\Core\Formatter;
141+
142+
interface FormatterInterface
143+
{
144+
/**
145+
* Return the object in your original format, normally as object
146+
*
147+
* @return mixed
148+
*/
149+
public function raw();
150+
151+
/**
152+
* Return the object transformed to string.
153+
*
154+
* @return string
155+
*/
156+
public function toText();
157+
158+
/**
159+
* Save the contents to a file
160+
*
161+
* @param string $filename
162+
* @return void
163+
*/
164+
public function saveToFile($filename);
165+
}
166+
```
167+
168+
AnyDataset implements two formatters:
169+
170+
- JsonFormatter
171+
- XmlFormatter
172+
173+
Example:
174+
175+
```php
176+
<?php
177+
$formatter = new XmlFormatter($anydataset->getIterator());
178+
$formatter->raw(); // Return a DOM object
179+
$formatter->toText(); // Return the XML as a text
180+
$formatter->saveToFile("/path/to/file.xml"); // Save the XML Text to a file.
181+
```
182+
96183
## Install
97184

98185
Just type: `composer require "byjg/anydataset=4.1.*"`

src/Formatter/BaseFormatter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use \ByJG\AnyDataset\Core\Row;
77
use InvalidArgumentException;
88

9-
abstract class BaseFormatter
9+
abstract class BaseFormatter implements FormatterInterface
1010
{
1111
/**
1212
* @var GenericIterator|Row

src/Formatter/FormatterInterface.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@
22

33
namespace ByJG\AnyDataset\Core\Formatter;
44

5-
use \ByJG\AnyDataset\Core\AnyDataset;
6-
75
interface FormatterInterface
86
{
7+
/**
8+
* Return the object in your original format, normally as object
9+
*
10+
* @return mixed
11+
*/
912
public function raw();
1013

14+
/**
15+
* Return the object transformed to string.
16+
*
17+
* @return string
18+
*/
1119
public function toText();
1220

21+
/**
22+
* Save the contents to a file
23+
*
24+
* @param string $filename
25+
* @return void
26+
*/
1327
public function saveToFile($filename);
1428
}

src/RowOutput.php

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace ByJG\AnyDataset\Core;
4+
use Closure;
5+
6+
class RowOutput
7+
{
8+
const FORMAT = 'format';
9+
const CUSTOM = 'custom';
10+
11+
protected $fieldList = [];
12+
13+
public static function getInstance()
14+
{
15+
return new RowOutput();
16+
}
17+
18+
public function print($row, $field)
19+
{
20+
if (!isset($this->fieldList[$field])) {
21+
return $row->get($field);
22+
}
23+
24+
$data = $this->fieldList[$field];
25+
26+
switch ($data[0]) {
27+
case self::FORMAT:
28+
return $this->formatPattern($row, $field, $data[1]);
29+
case self::CUSTOM:
30+
return $this->formatCustom($row, $field, $data[1]);
31+
}
32+
}
33+
34+
public function apply($row)
35+
{
36+
foreach ($this->fieldList as $key => $value) {
37+
$row->set($key, $this->print($row, $key));
38+
}
39+
}
40+
41+
protected function formatPattern($row, $field, $pattern)
42+
{
43+
$rowParsed = $row->toArray();
44+
foreach ($rowParsed as $key => $value) {
45+
$rowParsed['{' . $key . '}'] = $value;
46+
unset($rowParsed[$key]);
47+
}
48+
$rowParsed['{.}'] = $field;
49+
$rowParsed['{}'] = $row->get($field);
50+
51+
return strtr($pattern, $rowParsed);
52+
}
53+
54+
protected function formatCustom($row, $field, $closure)
55+
{
56+
return $closure($row, $field, $row->get($field));
57+
}
58+
59+
/**
60+
* @param string $field
61+
* @param string $pattern
62+
* @return RowOutput
63+
*/
64+
public function addFormat($field, $pattern)
65+
{
66+
$this->fieldList[$field] = [ self::FORMAT, $pattern ];
67+
return $this;
68+
}
69+
70+
/**
71+
* Undocumented function
72+
*
73+
* @param string $field
74+
* @param Closure $closure
75+
* @return RowOutput
76+
*/
77+
public function addCustomFormat($field, Closure $closure)
78+
{
79+
$this->fieldList[$field] = [ self::CUSTOM, $closure ];
80+
return $this;
81+
}
82+
}

tests/RowOutputTest.php

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace Tests\AnyDataset\Dataset;
4+
5+
use ByJG\AnyDataset\Core\Row;
6+
use ByJG\AnyDataset\Core\RowValidator;
7+
use PHPUnit\Framework\TestCase;
8+
use ByJG\AnyDataset\Core\RowOutput;
9+
10+
class RowOutputTest extends TestCase
11+
{
12+
13+
public function testFormatPattern()
14+
{
15+
$row1 = new Row([
16+
"field1" => 10,
17+
"field2" => "test",
18+
"field3" => 20.30,
19+
"field4" => "2021-11-20"
20+
]);
21+
22+
$row2 = new Row([
23+
"field1" => 1,
24+
"field2" => "OK",
25+
"field3" => 3,
26+
]);
27+
28+
$formatter = RowOutput::getInstance()
29+
->addFormat("field1", "Test {field1}")
30+
->addFormat("field2", "{.}: Showing {} and {field3}");
31+
32+
$this->assertEquals("Test 10", $formatter->print($row1, "field1"));
33+
$this->assertEquals("field2: Showing test and 20.3", $formatter->print($row1, "field2"));
34+
$this->assertEquals("20.30", $formatter->print($row1, "field3"));
35+
36+
$this->assertEquals("Test 1", $formatter->print($row2, "field1"));
37+
$this->assertEquals("field2: Showing OK and 3", $formatter->print($row2, "field2"));
38+
$this->assertEquals("3", $formatter->print($row2, "field3"));
39+
}
40+
41+
public function testFormatCustom()
42+
{
43+
$row = new Row([
44+
"field1" => 10,
45+
"field2" => "test",
46+
"field3" => 20.30,
47+
"field4" => "2021-11-20"
48+
]);
49+
50+
$formatter = RowOutput::getInstance()
51+
->addCustomFormat("field1", function ($row, $field, $value) {
52+
return $value * 4;
53+
})
54+
->addCustomFormat("field3", function ($row, $field, $value) {
55+
return "$field x 3 = " . ($value * 3);
56+
});
57+
58+
$this->assertEquals("40", $formatter->print($row, "field1"));
59+
$this->assertEquals("field3 x 3 = 60.9", $formatter->print($row, "field3"));
60+
$this->assertEquals("2021-11-20", $formatter->print($row, "field4"));
61+
}
62+
63+
public function testApply()
64+
{
65+
$row = new Row([
66+
"field1" => 10,
67+
"field2" => "test",
68+
"field3" => 20.30,
69+
"field4" => "2021-11-20"
70+
]);
71+
72+
$formatter = RowOutput::getInstance()
73+
->addFormat("field1", "Value: {field1}")
74+
->addCustomFormat("field3", function ($row, $field, $value) {
75+
return "$field x 3 = " . ($value * 3);
76+
});
77+
78+
$this->assertEquals("10", $row->get("field1"));
79+
$this->assertEquals("test", $row->get("field2"));
80+
$this->assertEquals("20.30", $row->get("field3"));
81+
$this->assertEquals("2021-11-20", $row->get("field4"));
82+
83+
$formatter->apply($row);
84+
85+
$this->assertEquals("Value: 10", $row->get("field1"));
86+
$this->assertEquals("test", $row->get("field2"));
87+
$this->assertEquals("field3 x 3 = 60.9", $row->get("field3"));
88+
$this->assertEquals("2021-11-20", $row->get("field4"));
89+
}
90+
}

tests/RowValidatorTest.php

-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
use ByJG\AnyDataset\Core\RowValidator;
77
use PHPUnit\Framework\TestCase;
88

9-
require_once "Sample/ModelPublic.php";
10-
require_once "Sample/ModelGetter.php";
11-
require_once "Sample/ModelPropertyPattern.php";
12-
139
class RowValidatorTest extends TestCase
1410
{
1511

0 commit comments

Comments
 (0)