Completed
Push — master ( 1843e0...50e9b5 )
by Adam
06:24
created

Csv   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 99
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A fromArray() 0 8 1
A fromVOArray() 0 8 1
A toArray() 0 4 1
A hack() 0 9 2
A hackWrite() 0 6 2
A hackRead() 0 8 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
     * @return static
24
     */
25 16
    public static function fromArray(
26
        array $array,
27
        $delimiter = ',',
28
        $enclosure = '"',
29
        $escape = '\\'
30
    ) {
31 16
        return new static(self::hack($array, $delimiter, $enclosure, $escape));
32
    }
33
34
    /**
35
     * @param  VOArray $voArray
36
     * @param  string  $delimiter
37
     * @param  string  $enclosure
38
     * @param  string  $escape
39
     * @return static
40
     */
41 1
    public static function fromVOArray(
42
        VOArray $voArray,
43
        $delimiter = ',',
44
        $enclosure = '"',
45
        $escape = '\\'
46
    ) {
47 1
        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 2
    public function toArray($delimiter = ",", $enclosure = '"', $escape = "\\")
57
    {
58 2
        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 16
    private static function hack(array $array, $delimiter, $enclosure, $escape)
76
    {
77 16
        $handle = fopen('php://temp', 'r+');
78 16
        VOArray::fromArray($array)->isMultiDim()
79 16
            ? self::hackWrite($handle, $array, $delimiter, $enclosure, $escape)
80 16
            : fputcsv($handle, $array, $delimiter, $enclosure, $escape);
81 16
        rewind($handle);
82 16
        return self::hackRead($handle);
83
    }
84
85
    /**
86
     * @param $handle
87
     * @param array $array
88
     * @param $delimiter
89
     * @param $enclosure
90
     * @param $escape
91
     */
92 1
    private static function hackWrite($handle, array $array, $delimiter, $enclosure, $escape)
93
    {
94 1
        foreach ($array as $line) {
95 1
            fputcsv($handle, $line, $delimiter, $enclosure, $escape);
96 1
        }
97 1
    }
98
99
    /**
100
     * @param  $handle
101
     * @param  string $contents
102
     * @return string
103
     */
104 16
    private static function hackRead($handle, $contents = '')
105
    {
106 16
        while (!feof($handle)) {
107 16
            $contents .= fread($handle, 8192);
108 16
        }
109 16
        fclose($handle);
110 16
        return $contents;
111
    }
112
}
113