Completed
Push — master ( c64181...1843e0 )
by Adam
13:40
created

Csv::fromMultiArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 2
crap 2
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);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($array); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
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