Skip to content

Commit 7ad49fd

Browse files
committed
Bug Fix League\Csv
- bug fix stream filter thephpleague#72 - improve public API by deprecating (get|set)EncodingFrom
1 parent 811c90a commit 7ad49fd

File tree

5 files changed

+69
-12
lines changed

5 files changed

+69
-12
lines changed

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@
22

33
All Notable changes to `Csv` will be documented in this file
44

5+
## Next - 2016-xx-xx
6+
7+
### Added
8+
9+
- `Ouput::getInputEncoding`
10+
- `Ouput::setInputEncoding`
11+
12+
### Deprecated
13+
14+
- `Ouput::getEncodingFrom` replaced by `Ouput::getInputEncoding`
15+
- `Ouput::setEncodingFrom` replaced by `Ouput::setInputEncoding`
16+
17+
### Fixed
18+
19+
- Stream Filters are now url encoded before usage [issue #72](https://github.com/thephpleague/csv/issues/72)
20+
21+
### Removed
22+
23+
- None
24+
525
## 8.0.0 - 2015-12-11
626

727
### Added

src/AbstractCsv.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ protected function newInstance($class, $open_mode)
190190
$csv->delimiter = $this->delimiter;
191191
$csv->enclosure = $this->enclosure;
192192
$csv->escape = $this->escape;
193-
$csv->encodingFrom = $this->encodingFrom;
193+
$csv->input_encoding = $this->input_encoding;
194194
$csv->input_bom = $this->input_bom;
195195
$csv->output_bom = $this->output_bom;
196196
$csv->newline = $this->newline;

src/Config/Output.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ trait Output
3333
*
3434
* @var string
3535
*/
36-
protected $encodingFrom = 'UTF-8';
36+
protected $input_encoding = 'UTF-8';
3737

3838
/**
3939
* The Input file BOM character
@@ -54,26 +54,56 @@ trait Output
5454
*
5555
* @return static
5656
*/
57-
public function setEncodingFrom($str)
57+
public function setInputEncoding($str)
5858
{
5959
$str = str_replace('_', '-', $str);
6060
$str = filter_var($str, FILTER_SANITIZE_STRING, ['flags' => FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH]);
6161
if (empty($str)) {
6262
throw new InvalidArgumentException('you should use a valid charset');
6363
}
64-
$this->encodingFrom = strtoupper($str);
64+
$this->input_encoding = strtoupper($str);
6565

6666
return $this;
6767
}
6868

69+
/**
70+
* Sets the CSV encoding charset
71+
*
72+
* DEPRECATION WARNING! This method will be removed in the next major point release
73+
*
74+
* @deprecated deprecated since version 4.1
75+
*
76+
* @param string $str
77+
*
78+
* @return static
79+
*/
80+
public function setEncodingFrom($str)
81+
{
82+
return $this->setInputEncoding($str);
83+
}
84+
85+
/**
86+
* Gets the source CSV encoding charset
87+
*
88+
* @return string
89+
*/
90+
public function getInputEncoding()
91+
{
92+
return $this->input_encoding;
93+
}
94+
6995
/**
7096
* Gets the source CSV encoding charset
7197
*
98+
* DEPRECATION WARNING! This method will be removed in the next major point release
99+
*
100+
* @deprecated deprecated since version 4.1
101+
*
72102
* @return string
73103
*/
74104
public function getEncodingFrom()
75105
{
76-
return $this->encodingFrom;
106+
return $this->input_encoding;
77107
}
78108

79109
/**
@@ -219,12 +249,12 @@ abstract protected function getQueryIterator();
219249
*/
220250
protected function convertToUtf8(Iterator $iterator)
221251
{
222-
if (stripos($this->encodingFrom, 'UTF-8') !== false) {
252+
if (stripos($this->input_encoding, 'UTF-8') !== false) {
223253
return $iterator;
224254
}
225255

226256
$convertCell = function ($value) {
227-
return mb_convert_encoding($value, 'UTF-8', $this->encodingFrom);
257+
return mb_convert_encoding($value, 'UTF-8', $this->input_encoding);
228258
};
229259

230260
$convertRow = function (array $row) use ($convertCell) {

src/Modifier/StreamFilter.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected function initStreamFilter($path)
8383
return;
8484
}
8585
$this->stream_uri = $matches['resource'];
86-
$this->stream_filters = explode('|', $matches['filters']);
86+
$this->stream_filters = array_map('urldecode', explode('|', $matches['filters']));
8787
$this->stream_filter_mode = $this->fetchStreamModeAsInt($matches['mode']);
8888
}
8989

@@ -209,7 +209,7 @@ protected function sanitizeStreamFilter($filter_name)
209209
{
210210
$this->assertStreamable();
211211

212-
return $this->validateString($filter_name);
212+
return urldecode($this->validateString($filter_name));
213213
}
214214

215215
/**
@@ -228,7 +228,7 @@ public function hasStreamFilter($filter_name)
228228
{
229229
$this->assertStreamable();
230230

231-
return false !== array_search($filter_name, $this->stream_filters, true);
231+
return false !== array_search(urldecode($filter_name), $this->stream_filters, true);
232232
}
233233

234234
/**
@@ -241,7 +241,7 @@ public function hasStreamFilter($filter_name)
241241
public function removeStreamFilter($filter_name)
242242
{
243243
$this->assertStreamable();
244-
$res = array_search($filter_name, $this->stream_filters, true);
244+
$res = array_search(urldecode($filter_name), $this->stream_filters, true);
245245
if (false !== $res) {
246246
unset($this->stream_filters[$res]);
247247
}
@@ -276,7 +276,7 @@ protected function getStreamFilterPath()
276276

277277
return 'php://filter/'
278278
.$this->getStreamFilterPrefix()
279-
.implode('|', $this->stream_filters)
279+
.implode('|', array_map('urlencode', $this->stream_filters))
280280
.'/resource='.$this->stream_uri;
281281
}
282282

test/StreamFilterTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,11 @@ public function testSetStreamFilterWriterNewLine()
142142
$this->assertTrue($csv->hasStreamFilter(FilterReplace::FILTER_NAME."\r\n:\n"));
143143
$csv->insertOne([1, 'two', 3, "new\r\nline"]);
144144
}
145+
146+
public function testUrlEncodeFilterParameters()
147+
{
148+
$csv = Reader::createFromPath(__DIR__.'/data/foo.csv');
149+
$csv->appendStreamFilter('convert.iconv.UTF-8/ASCII//TRANSLIT');
150+
$this->assertCount(1, $csv->fetchAll());
151+
}
145152
}

0 commit comments

Comments
 (0)