1
|
|
|
<?php |
2
|
|
|
namespace Werkspot\BingAdsApiBundle\Api\Helper; |
3
|
|
|
|
4
|
|
|
use DateTime; |
5
|
|
|
|
6
|
|
|
class Csv |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @param array $lines |
10
|
|
|
* @param bool $includingColumnHeaders |
11
|
|
|
* @param int $fileHeaders |
12
|
|
|
* @param int $columnHeaders |
13
|
|
|
* |
14
|
|
|
* @return string[] |
15
|
|
|
*/ |
16
|
4 |
|
public function removeHeaders(array $lines, $includingColumnHeaders = true, $fileHeaders = 10, $columnHeaders = 1) |
17
|
|
|
{ |
18
|
4 |
|
$lines = array_values($lines); |
19
|
|
|
|
20
|
4 |
|
$removeLines = ($includingColumnHeaders) ? ($fileHeaders + $columnHeaders) : $fileHeaders; |
21
|
4 |
|
for ($i = 0; $i < ($removeLines); ++$i) { |
22
|
4 |
|
unset($lines[$i]); |
23
|
4 |
|
} |
24
|
|
|
|
25
|
4 |
|
$lines = array_values($lines); |
26
|
|
|
|
27
|
4 |
|
return $lines; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $lines |
32
|
|
|
* @param int $noOfLinesToRemove |
33
|
|
|
* |
34
|
|
|
* @return string[] |
35
|
|
|
*/ |
36
|
2 |
|
public function removeLastLines(array $lines, $noOfLinesToRemove = 1) |
37
|
|
|
{ |
38
|
2 |
|
$totalLines = count($lines); |
39
|
2 |
|
$removeFrom = $totalLines - $noOfLinesToRemove; |
40
|
|
|
|
41
|
2 |
|
for ($i = $removeFrom; $i < $totalLines; ++$i) { |
42
|
2 |
|
unset($lines[$i]); |
43
|
2 |
|
} |
44
|
|
|
|
45
|
2 |
|
$lines = array_values($lines); |
46
|
|
|
|
47
|
2 |
|
return $lines; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $lines |
52
|
|
|
* @param string $separator |
53
|
|
|
* @param string $enclosure |
54
|
|
|
* |
55
|
|
|
* @return string[] |
56
|
|
|
*/ |
57
|
1 |
|
public function convertDateMDYtoYMD(array $lines, $separator = ',', $enclosure = '"') |
58
|
|
|
{ |
59
|
1 |
|
foreach ($lines as $key => $line) { |
60
|
1 |
|
$columns = str_getcsv($line, $separator); |
61
|
1 |
|
$isChanged = false; |
62
|
1 |
|
foreach ($columns as $columnKey => $column) { |
63
|
1 |
|
if (preg_match('/^([1-9]|1[0-2])\/([1-9]|[1-2][0-9]|3[0-1])\/[0-9]{4}$/', $column)) { |
64
|
1 |
|
$date = DateTime::createFromFormat('m/d/Y', $column); |
65
|
1 |
|
$columns[$columnKey] = $date->format('Y/m/d'); |
66
|
1 |
|
$isChanged = true; |
67
|
1 |
|
} |
68
|
1 |
|
} |
69
|
1 |
|
if ($isChanged) { |
70
|
1 |
|
$lines[$key] = $this->arrayToCsvLine($columns, $separator, $enclosure); |
71
|
1 |
|
} |
72
|
1 |
|
} |
73
|
|
|
|
74
|
1 |
|
return $lines; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $array |
79
|
|
|
* @param string $separator |
80
|
|
|
* @param null|string $enclosure |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
2 |
|
public function arrayToCsvLine(array $array, $separator = ',', $enclosure = null) |
85
|
|
|
{ |
86
|
2 |
|
$csvStr = ''; |
87
|
|
|
|
88
|
2 |
|
$length = count($array); |
89
|
2 |
|
for ($i = 0; $i < $length; ++$i) { |
90
|
2 |
|
if ($enclosure !== null) { |
91
|
2 |
|
$csvStr .= $enclosure . str_replace($enclosure, $enclosure . $enclosure, $array[$i]) . $enclosure; |
92
|
2 |
|
} else { |
93
|
1 |
|
$csvStr .= $array[$i]; |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
$csvStr .= ($i < count($array) - 1) ? $separator : "\r\n"; |
97
|
2 |
|
} |
98
|
|
|
|
99
|
2 |
|
return $csvStr; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|