|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BestServedCold\PhalueObjects\Format; |
|
4
|
|
|
|
|
5
|
|
|
use BestServedCold\PhalueObjects\Contract\VOArrayable; |
|
6
|
|
|
use BestServedCold\PhalueObjects\VOArray; |
|
7
|
|
|
use BestServedCold\PhalueObjects\VOString; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Csv |
|
11
|
|
|
* |
|
12
|
|
|
* @package BestServedCold\PhalueObjects\Format |
|
13
|
|
|
*/ |
|
14
|
|
|
class Csv extends VOString implements VOArrayable |
|
15
|
|
|
{ |
|
16
|
|
|
use StringMixin; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param array $array |
|
20
|
|
|
* @param string $delimiter |
|
21
|
|
|
* @param string $enclosure |
|
22
|
|
|
* @param string $escape |
|
23
|
16 |
|
* @return static |
|
24
|
|
|
*/ |
|
25
|
16 |
|
public static function fromArray( |
|
26
|
|
|
array $array, |
|
27
|
|
|
$delimiter = ',', |
|
28
|
|
|
$enclosure = '"', |
|
29
|
|
|
$escape = '\\' |
|
30
|
|
|
) { |
|
31
|
|
|
return new static(self::hack($array, $delimiter, $enclosure, $escape)); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param VOArray $voArray |
|
36
|
2 |
|
* @param string $delimiter |
|
37
|
|
|
* @param string $enclosure |
|
38
|
2 |
|
* @param string $escape |
|
39
|
|
|
* @return static |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function fromVOArray( |
|
42
|
|
|
VOArray $voArray, |
|
43
|
|
|
$delimiter = ',', |
|
44
|
|
|
$enclosure = '"', |
|
45
|
|
|
$escape = '\\' |
|
46
|
|
|
) { |
|
47
|
|
|
return static::fromArray($voArray->getValue(), $delimiter, $enclosure, $escape); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $delimiter |
|
52
|
|
|
* @param string $enclosure |
|
53
|
|
|
* @param string $escape |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
|
|
public function toArray($delimiter = ",", $enclosure = '"', $escape = "\\") |
|
57
|
|
|
{ |
|
58
|
|
|
return str_getcsv($this->getValue(), $delimiter, $enclosure, $escape); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Hack |
|
63
|
|
|
* |
|
64
|
|
|
* PHP does not have a function to convert an array to CSV, however, fputcsv() does. |
|
65
|
|
|
* We do this in memory using a file handle. It's not pretty, but it works. |
|
66
|
|
|
* |
|
67
|
|
|
* I'm planning on abstracting this out into the File namespace in the near future. |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $array |
|
70
|
|
|
* @param string $delimiter |
|
71
|
|
|
* @param string $enclosure |
|
72
|
|
|
* @param string $escape |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
private static function hack(array $array, $delimiter, $enclosure, $escape) |
|
76
|
|
|
{ |
|
77
|
|
|
var_dump($array); |
|
|
|
|
|
|
78
|
|
|
$handle = fopen('php://temp', 'r+'); |
|
79
|
|
|
VOArray::fromArray($array)->isMultiDim() |
|
80
|
|
|
? self::hackWrite($handle, $array, $delimiter, $enclosure, $escape) |
|
81
|
|
|
: fputcsv($handle, $array, $delimiter, $enclosure, $escape); |
|
82
|
|
|
rewind($handle); |
|
83
|
|
|
return self::hackRead($handle); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param $handle |
|
88
|
|
|
* @param array $array |
|
89
|
|
|
* @param $delimiter |
|
90
|
|
|
* @param $enclosure |
|
91
|
|
|
* @param $escape |
|
92
|
|
|
*/ |
|
93
|
|
|
private static function hackWrite($handle, array $array, $delimiter, $enclosure, $escape) |
|
94
|
|
|
{ |
|
95
|
|
|
foreach ($array as $line) { |
|
96
|
|
|
fputcsv($handle, $line, $delimiter, $enclosure, $escape); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param $handle |
|
102
|
|
|
* @param string $contents |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
private static function hackRead($handle, $contents = '') |
|
106
|
|
|
{ |
|
107
|
|
|
while (!feof($handle)) { |
|
108
|
|
|
$contents .= fread($handle, 8192); |
|
109
|
|
|
} |
|
110
|
|
|
fclose($handle); |
|
111
|
|
|
return $contents; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|