This repository has been archived by the owner on Dec 5, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataProcessor.php
187 lines (150 loc) · 5.63 KB
/
DataProcessor.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* DataProcessor.php
* @author: Miroslav Čillík <miro@keboola.com>
* @created: 24.6.13
*/
namespace Keboola\Google\DriveBundle\Extractor;
use Keboola\Csv\CsvFile;
use Keboola\Syrup\Utility\Utility;
class DataProcessor
{
protected $config;
/** @var CsvFile */
protected $inputCsv;
/** @var CsvFile */
protected $outputCsv;
protected $outputFile;
public function __construct($inputFile, $config)
{
$this->config = $config;
$this->inputCsv = new CsvFile($inputFile);
$this->outputFile = substr($inputFile, 0, (count($inputFile) - 5)) . '_out.csv';
$this->outputCsv = new CsvFile($this->outputFile);
}
/**
* Process file using config settings
* @return string Output file name
*/
public function process()
{
$i = 0;
$csvHeader = array();
$csvTransposeHeader = null;
$csvHeaderRaw = array();
foreach ($this->inputCsv as $csvRow) {
if ($i < $this->config['header']['rows']) {
if ($i == $this->config['header']['rows'] - 1) {
$csvHeaderRaw = $csvRow;
$csvOutHeaderArr = $csvHeaderRaw;
if (isset($this->config['header']['columns'])) {
$csvOutHeaderArr = $this->config['header']['columns'];
}
if (isset($this->config['transform']['transpose'])) {
$csvOutHeaderArr = $this->transposeHeader($csvOutHeaderArr);
}
if (isset($this->config['transform']['merge'])) {
$csvOutHeaderArr = $this->mergeHeader($csvOutHeaderArr);
}
if (!isset($this->config['header']['sanitize']) || $this->config['header']['sanitize'] != 0) {
$csvOutHeaderArr = $this->normalizeCsvHeader($csvOutHeaderArr);
}
$this->outputCsv->writeRow($csvOutHeaderArr);
} else {
$csvTransposeHeader = $csvRow;
}
} else {
if (isset($this->config['transform'])) {
// Transpose
if (isset($this->config['transform']['transpose'])) {
$this->transpose($csvRow, $csvHeaderRaw, $csvTransposeHeader);
}
// Merge
if (isset($this->config['transform']['merge'])) {
$csvRow = $this->merge($csvRow);
}
}
if (!isset($this->config['transform']['transpose'])) {
$this->outputCsv->writeRow($csvRow);
}
}
$i++;
}
return $this->outputFile;
}
protected function transpose($csvRow, $csvHeaderRaw, $csvTransposeHeader)
{
$transposeFrom = $this->config['transform']['transpose']['from'];
$outRowArr = array_slice($csvRow, 0, ($transposeFrom - 1));
$transposeCsvRow = array_slice($csvRow, ($transposeFrom - 1), null, true);
foreach ($transposeCsvRow as $k => $v) {
$outRowArr['key'] = $csvHeaderRaw[$k];
$outRowArr['value'] = $v;
if (!is_null($csvTransposeHeader) && !empty($csvTransposeHeader[$k])) {
$outRowArr[$this->config['header']['transpose']['name']] = $csvTransposeHeader[$k];
}
$this->outputCsv->writeRow($outRowArr);
}
}
protected function transposeHeader($csvHeader)
{
$transposeFrom = $this->config['transform']['transpose']['from'];
$csvOutHeaderArr = array_slice($csvHeader, 0, ($transposeFrom - 1));
$csvOutHeaderArr[] = 'key';
$csvOutHeaderArr[] = 'value';
if (isset($this->config['header']['transpose']['name'])) {
$csvOutHeaderArr[] = $this->config['header']['transpose']['name'];
}
return $csvOutHeaderArr;
}
protected function merge($csvRow)
{
$from = $this->config['transform']['merge']['from'];
$length = $this->config['transform']['merge']['length'];
$size = count($csvRow);
$outRowArr = array_slice($csvRow, 0, $from);
$mergeArr = array_slice($csvRow, $from);
for ($i = 0; $i < $size; $i = $i + $length) {
$slice = array_slice($mergeArr, $i, $length);
$isEmpty = true;
foreach ($slice as $col) {
if (!empty($col)) {
$isEmpty = false;
break;
}
}
if (!$isEmpty) {
$outRowArr = array_merge($outRowArr, $slice);
break;
}
}
return $outRowArr;
}
protected function mergeHeader($csvHeader)
{
$from = $this->config['transform']['merge']['from'];
$length = $this->config['transform']['merge']['length'];
$csvOutHeaderArr = array_slice($csvHeader, 0, $from + $length);
return $csvOutHeaderArr;
}
protected function normalizeCsvHeader($header)
{
foreach ($header as &$col) {
$col = $this->sanitize($col);
}
return $header;
}
protected function sanitize($string)
{
$string = str_replace('#', 'count', $string);
$string = Utility::unaccent($string);
$string = preg_replace("/[\n\r]/","",$string);
$string = preg_replace("/[^A-Za-z0-9_\s]/", '', $string);
$string = trim($string);
$string = str_replace(' ', '_', $string);
if (strlen($string) < 2) {
$string = 'empty';
}
return $string;
}
}