Skip to content

Commit fa94758

Browse files
authored
bug fix issue #287 (#288)
* bug fix issue #287
1 parent 0d0b12f commit fa94758

File tree

5 files changed

+68
-14
lines changed

5 files changed

+68
-14
lines changed

.scrutinizer.yml

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
1+
build:
2+
nodes:
3+
analysis:
4+
project_setup:
5+
override: true
6+
tests:
7+
override:
8+
- php-scrutinizer-run --enable-security-analysis
19
filter:
2-
paths: [src/*]
3-
excluded_paths: [tests/*]
10+
paths:
11+
- src/
12+
excluded_paths:
13+
- tests/
414
checks:
515
php:
616
code_rating: true
17+
duplication: true
718
tools:
819
external_code_coverage:
920
timeout: 600
1021
runs: 2
1122
php_code_coverage: false
1223
php_loc:
1324
enabled: true
14-
excluded_dirs: [tests, vendor]
15-
php_cpd:
25+
excluded_dirs:
26+
- tests/
27+
- vendor/
28+
php_cpd: false
29+
php_sim:
1630
enabled: true
17-
excluded_dirs: [tests, vendor]
31+
filter:
32+
paths: ['src/']

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ install:
2929

3030
script:
3131
- composer phpunit
32+
- if [ "$VALIDATE_CODING_STYLE" == "true" ]; then composer phpcs; fi
33+
- if [ "$RUN_PHPSTAN" == "true" ]; then composer phpstan; fi
3234

3335
after_script:
3436
- if [ "$COLLECT_COVERAGE" == "true" ]; then wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover build/clover.xml; fi
35-
- if [ "$VALIDATE_CODING_STYLE" == "true" ]; then composer phpcs; fi
36-
- if [ "$RUN_PHPSTAN" == "true" ]; then composer phpstan; fi

src/CharsetConverter.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,17 @@ public function __invoke(array $record): array
208208
/**
209209
* Walker method to convert the offset and the value of a CSV record field
210210
*
211-
* @param string|null $value
212-
* @param string|int $offset
211+
* @param mixed $value
212+
* @param mixed $offset
213213
*/
214214
protected function encodeField(&$value, &$offset)
215215
{
216-
if (null !== $value) {
217-
$value = mb_convert_encoding($value, $this->output_encoding, $this->input_encoding);
216+
if (null !== $value && !is_numeric($value)) {
217+
$value = mb_convert_encoding((string) $value, $this->output_encoding, $this->input_encoding);
218218
}
219219

220-
if (!is_int($offset)) {
221-
$offset = mb_convert_encoding($offset, $this->output_encoding, $this->input_encoding);
220+
if (!is_numeric($offset)) {
221+
$offset = mb_convert_encoding((string) $offset, $this->output_encoding, $this->input_encoding);
222222
}
223223
}
224224

src/Writer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function insertOne(array $record): int
129129
$record = array_reduce($this->formatters, [$this, 'formatRecord'], $record);
130130
$this->validateRecord($record);
131131
$bytes = $this->document->fputcsv($record, $this->delimiter, $this->enclosure, $this->escape);
132-
if (false !== $bytes && null !== $bytes) {
132+
if ('' !== (string) $bytes) {
133133
return $bytes + $this->consolidate();
134134
}
135135

tests/CharsetConverterTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,43 @@ public function testOnCreateFailedWithWrongParams()
150150
$converter->filtername = CharsetConverter::FILTERNAME.'.foo/bar';
151151
$this->assertFalse($converter->onCreate());
152152
}
153+
154+
/**
155+
* @covers ::convert
156+
* @covers ::encodeField
157+
*
158+
* @dataProvider converterProvider
159+
* @param array $record
160+
* @param array $expected
161+
*/
162+
public function testConvertOnlyStringField(array $record, array $expected)
163+
{
164+
$converter = (new CharsetConverter())
165+
->inputEncoding('iso-8859-15')
166+
->outputEncoding('utf-8');
167+
$res = $converter->convert([$record]);
168+
$this->assertSame($expected, $res[0]);
169+
}
170+
171+
public function converterProvider()
172+
{
173+
return [
174+
'only numeric values' => [
175+
'record' => [1, 2, 3],
176+
'expected' => [1, 2, 3],
177+
],
178+
'only string values' => [
179+
'record' => ['1', '2', '3'],
180+
'expected' => ['1', '2', '3'],
181+
],
182+
'mixed values' => [
183+
'record' => [1, '2', 3],
184+
'expected' => [1, '2', 3],
185+
],
186+
'mixed offset' => [
187+
'record' => [1 => 1, '2' => '2', 3 => 3],
188+
'expected' => [1 => 1, '2' => '2', 3 => 3],
189+
],
190+
];
191+
}
153192
}

0 commit comments

Comments
 (0)