forked from Tactics/TableBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCsvDataTransformer.php
154 lines (134 loc) · 3.43 KB
/
CsvDataTransformer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* Class Csvdatatransformer
* @author Aaron Muylaer <[email protected]>
*/
namespace Tactics\TableBundle;
class CsvDataTransformer implements DataTransformerInterface
{
/**
* @var string
*/
protected $csv = '';
/**
* @var string
*/
protected $delimiter;
/**
* Constructor.
*/
public function __construct($delimiter = ';')
{
$this->delimiter = $delimiter;
}
/**
* @inheritDoc
*/
public function transform(Table $table)
{
$this->writeHeaders($table);
$this->writeRows($table);
$this->removeLastNewLineCharacter();
return $this->csv;
}
/**
* Writes headers to csv string.
*
* @param Tactics\TableBundle\Table $table
*
* @return void
*/
private function writeHeaders(Table $table)
{
foreach ($table as $column) {
$this->csv .= $column->getHeader()->getValue().$this->delimiter;
}
$this->appendNewLineCharacter();
$this->cleanUpLastRow();
}
/**
* Loops over the rows and writes them to the csv formatted string.
*
* @param Tactics\TableBundle\Table $table
*
* @return void
*/
private function writeRows(Table $table)
{
foreach ($table->getRows() as $row) {
$this->csv .= $this->writeRow($table, $row);
}
}
/**
* Writes a single row to the csv formatted string.
*
* @param Tactics\TableBundle\Table $table
* @param array $row
*
* @return void
*/
private function writeRow(Table $table, $row)
{
foreach ($table as $column) {
$cell = $column->getCell($row);
if ($cell['value'] instanceof \DateTime) {
$value = $cell['value']->format('d/m/Y');
} elseif (is_object($cell['value'])) {
try {
$value = (string) $cell['value'];
} catch (\Exception $e) {
$value = '';
}
} else {
$value = $cell['value'];
}
$this->csv .= $value.$this->delimiter;
}
$this->cleanUpLastRow();
$this->appendNewLineCharacter();
}
/**
* Appends a newline character to the csv formatted string.
*
* @return void
*/
private function appendNewLineCharacter()
{
$this->csv .= "\r\n";
}
/**
* Removes last trailing delimiter from csv string.
*
* @return void
*/
private function cleanUpLastRow()
{
$this->csv = $this->str_lreplace($this->delimiter, '', $this->csv);
}
/**
* Removes the last new line character from the csv string.
*
* @return void
*/
private function removeLastNewLineCharacter()
{
$this->csv = $this->str_lreplace("\r\n", '', $this->csv);
}
/**
* Replaces last occurence of a string in a string.
*
* @param string $search The string that needs to be replaced.
* @param string $replace The replacement.
* @param string $subject The string to search in.
*
* @return string
*/
private function str_lreplace($search, $replace, $subject)
{
$pos = strrpos($subject, $search);
if($pos !== false) {
$subject = substr_replace($subject, $replace, $pos, strlen($search));
}
return $subject;
}
}