|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CSVelte: Slender, elegant CSV for PHP |
|
4
|
|
|
* |
|
5
|
|
|
* Inspired by Python's CSV module and Frictionless Data and the W3C's CSV |
|
6
|
|
|
* standardization efforts, CSVelte was written in an effort to take all the |
|
7
|
|
|
* suck out of working with CSV. |
|
8
|
|
|
* |
|
9
|
|
|
* @copyright Copyright (c) 2018 Luke Visinoni |
|
10
|
|
|
* @author Luke Visinoni <[email protected]> |
|
11
|
|
|
* @license See LICENSE file (MIT license) |
|
12
|
|
|
*/ |
|
13
|
|
|
namespace CSVelte; |
|
14
|
|
|
|
|
15
|
|
|
use CSVelte\Contract\Streamable; |
|
16
|
|
|
use Noz\Collection\Collection; |
|
17
|
|
|
|
|
18
|
|
|
use function Noz\collect; |
|
19
|
|
|
use function Stringy\create as s; |
|
20
|
|
|
use Traversable; |
|
21
|
|
|
|
|
22
|
|
|
class Writer |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var Streamable The output stream to write to */ |
|
25
|
|
|
protected $output; |
|
26
|
|
|
|
|
27
|
|
|
/** @var Dialect The *dialect* of CSV to write */ |
|
28
|
|
|
protected $dialect; |
|
29
|
|
|
|
|
30
|
|
|
/** @var Collection The header row */ |
|
31
|
|
|
protected $header; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Writer constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* Although this is the constructor, I don't envision it being used much in userland. I think much more common |
|
37
|
|
|
* methods of creating writers will be available within CSVelte base class such as CSVelte::toSplFileObject, |
|
38
|
|
|
* CSVelte::toPath(), CSVelte::toOutputBuffer(), etc. |
|
39
|
|
|
* |
|
40
|
|
|
* @param Streamable $output The destination streamable being written to |
|
41
|
|
|
* @param Dialect $dialect The dialect being written |
|
42
|
|
|
*/ |
|
43
|
4 |
|
public function __construct(Streamable $output, Dialect $dialect = null) |
|
44
|
|
|
{ |
|
45
|
4 |
|
if (is_null($dialect)) { |
|
46
|
2 |
|
$dialect = new Dialect; |
|
47
|
2 |
|
} |
|
48
|
4 |
|
$this->setOutputStream($output) |
|
49
|
4 |
|
->setDialect($dialect); |
|
50
|
4 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Set the CSV dialect |
|
54
|
|
|
* |
|
55
|
|
|
* @param Dialect $dialect The *dialect* of CSV to use |
|
56
|
|
|
* |
|
57
|
|
|
* @return self |
|
58
|
|
|
*/ |
|
59
|
4 |
|
public function setDialect(Dialect $dialect) |
|
60
|
|
|
{ |
|
61
|
4 |
|
$this->dialect = $dialect; |
|
62
|
4 |
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get dialect |
|
67
|
|
|
* |
|
68
|
|
|
* @return Dialect |
|
69
|
|
|
*/ |
|
70
|
4 |
|
public function getDialect() |
|
71
|
|
|
{ |
|
72
|
4 |
|
return $this->dialect; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Set output stream |
|
77
|
|
|
* |
|
78
|
|
|
* @param Streamable $stream The output stream to write to |
|
79
|
|
|
* |
|
80
|
|
|
* @return self |
|
81
|
|
|
*/ |
|
82
|
4 |
|
protected function setOutputStream(Streamable $stream) |
|
83
|
|
|
{ |
|
84
|
4 |
|
$this->output = $stream; |
|
85
|
4 |
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Insert a single record into CSV output |
|
90
|
|
|
* |
|
91
|
|
|
* Returns total bytes written to the output stream. |
|
92
|
|
|
* |
|
93
|
|
|
* @param array|Traversable $data A row of data to write to the CSV output |
|
94
|
|
|
* |
|
95
|
|
|
* @return false|int |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function insertRow($data) |
|
98
|
|
|
{ |
|
99
|
2 |
|
$d = $this->getDialect(); |
|
100
|
2 |
|
$data = collect($data) |
|
101
|
|
|
->map(function($field) use ($d) { |
|
102
|
2 |
|
if ($qstyle = $d->getQuoteStyle()) { |
|
103
|
2 |
|
$wrap = false; |
|
104
|
|
|
switch ($qstyle) { |
|
105
|
2 |
|
case Dialect::QUOTE_ALL: |
|
106
|
|
|
$wrap = true; |
|
107
|
|
|
break; |
|
108
|
2 |
|
case Dialect::QUOTE_MINIMAL: |
|
109
|
2 |
|
if (s($field)->containsAny([$d->getQuoteChar(), $d->getDelimiter(), $d->getLineTerminator()])) { |
|
110
|
2 |
|
$wrap = true; |
|
111
|
2 |
|
} |
|
112
|
2 |
|
break; |
|
113
|
|
|
case Dialect::QUOTE_NONNUMERIC: |
|
114
|
|
|
if (is_numeric((string) $field)) { |
|
115
|
|
|
$wrap = true; |
|
116
|
|
|
} |
|
117
|
|
|
break; |
|
118
|
|
|
} |
|
119
|
2 |
|
if ($wrap) { |
|
120
|
2 |
|
$field = s($field); |
|
121
|
2 |
|
if ($field->contains($d->getQuoteChar())) { |
|
122
|
2 |
|
$escapeChar = $d->isDoubleQuote() ? $d->getQuoteChar() : '\\' /*$d->getEscapeChar()*/; |
|
|
|
|
|
|
123
|
2 |
|
$field = $field->replace($d->getQuoteChar(), $d->getQuoteChar() . $d->getQuoteChar()); |
|
124
|
2 |
|
} |
|
125
|
2 |
|
$field = $field->surround($d->getQuoteChar()); |
|
126
|
2 |
|
} |
|
127
|
2 |
|
} |
|
128
|
2 |
|
return (string) $field; |
|
129
|
2 |
|
}); |
|
130
|
2 |
|
$str = s($data->join($d->getDelimiter())) |
|
131
|
2 |
|
->append($d->getLineTerminator()); |
|
132
|
|
|
|
|
133
|
2 |
|
return $this->output->write((string) $str); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Write multiple rows to CSV output |
|
138
|
|
|
* |
|
139
|
|
|
* Returns total bytes written to the output stream. |
|
140
|
|
|
* |
|
141
|
|
|
* @param array|Traversable $data An array of rows of data to write to the CSV output |
|
142
|
|
|
* |
|
143
|
|
|
* @return int |
|
144
|
|
|
*/ |
|
145
|
1 |
|
public function insertAll($data) |
|
146
|
|
|
{ |
|
147
|
1 |
|
return collect($data) |
|
148
|
1 |
|
->map(function($row, $lineNo, $i) { |
|
|
|
|
|
|
149
|
1 |
|
return $this->insertRow($row); |
|
150
|
1 |
|
}) |
|
151
|
1 |
|
->sum(); |
|
152
|
|
|
} |
|
153
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.