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
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $fieldList = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @return RowOutput |
18
|
|
|
*/ |
19
|
3 |
|
public static function getInstance() |
20
|
|
|
{ |
21
|
3 |
|
return new RowOutput(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param Row $row |
26
|
|
|
* @param string $field |
27
|
|
|
* @return mixed |
28
|
|
|
*/ |
29
|
3 |
|
public function print($row, $field) |
30
|
|
|
{ |
31
|
3 |
|
if (!isset($this->fieldList[$field])) { |
32
|
3 |
|
return $row->get($field); |
33
|
|
|
} |
34
|
|
|
|
35
|
3 |
|
$data = $this->fieldList[$field]; |
36
|
|
|
|
37
|
3 |
|
if ($data[0] == self::CUSTOM) { |
38
|
2 |
|
return $this->formatCustom($row, $field, $data[1]); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// self::FORMAT: |
42
|
2 |
|
return $this->formatPattern($row, $field, $data[1]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Row $row |
47
|
|
|
* @return Row |
48
|
|
|
*/ |
49
|
1 |
|
public function apply($row) |
50
|
|
|
{ |
51
|
1 |
|
$newRow = new Row(); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @psalm-suppress UnusedForeachValue |
55
|
|
|
*/ |
56
|
1 |
|
foreach ($row->toArray() as $key => $value) { |
57
|
1 |
|
$newRow->set($key, $this->print($row, $key)); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
return $newRow; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Row $row |
65
|
|
|
* @param string $field |
66
|
|
|
* @param string $pattern |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
2 |
|
protected function formatPattern($row, $field, $pattern) |
70
|
|
|
{ |
71
|
2 |
|
$rowParsed = $row->toArray(); |
72
|
2 |
|
foreach ($rowParsed as $key => $value) { |
73
|
2 |
|
$rowParsed['{' . $key . '}'] = $value; |
74
|
2 |
|
unset($rowParsed[$key]); |
75
|
|
|
} |
76
|
2 |
|
$rowParsed['{.}'] = $field; |
77
|
2 |
|
$rowParsed['{}'] = $row->get($field); |
78
|
|
|
|
79
|
2 |
|
return strtr($pattern, $rowParsed); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param Row $row |
84
|
|
|
* @param string $field |
85
|
|
|
* @param mixed $closure |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
2 |
|
protected function formatCustom($row, $field, $closure) |
89
|
|
|
{ |
90
|
2 |
|
return $closure($row, $field, $row->get($field)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $field |
95
|
|
|
* @param string $pattern |
96
|
|
|
* @return RowOutput |
97
|
|
|
*/ |
98
|
2 |
|
public function addFormat($field, $pattern) |
99
|
|
|
{ |
100
|
2 |
|
$this->fieldList[$field] = [ self::FORMAT, $pattern ]; |
101
|
2 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $field |
106
|
|
|
* @param Closure $closure |
107
|
|
|
* @return RowOutput |
108
|
|
|
*/ |
109
|
2 |
|
public function addCustomFormat($field, Closure $closure) |
110
|
|
|
{ |
111
|
2 |
|
$this->fieldList[$field] = [ self::CUSTOM, $closure ]; |
112
|
2 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
} |