Skip to content

Commit ba22d84

Browse files
committed
replaceable encoder for converters
1 parent 9d080ea commit ba22d84

25 files changed

+113
-42
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace XBase\DataConverter\Encoder;
4+
5+
interface EncoderInterface
6+
{
7+
public function encode(string $string, string $fromEncoding, string $toEncoding): string;
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace XBase\DataConverter\Encoder;
4+
5+
class IconvEncoder implements EncoderInterface
6+
{
7+
public function encode(string $string, string $fromEncoding, string $toEncoding): string
8+
{
9+
return iconv($fromEncoding, $toEncoding, $string);
10+
}
11+
}

src/DataConverter/Field/AbstractFieldDataConverter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace XBase\DataConverter\Field;
44

5+
use XBase\DataConverter\Encoder\EncoderInterface;
56
use XBase\Header\Column;
67
use XBase\Table\Table;
78

@@ -13,9 +14,13 @@ abstract class AbstractFieldDataConverter implements FieldDataConverterInterface
1314
/** @var Column */
1415
protected $column;
1516

16-
public function __construct(Table $table, Column $column)
17+
/** @var EncoderInterface */
18+
protected $encoder;
19+
20+
public function __construct(Table $table, Column $column, EncoderInterface $encoder)
1721
{
1822
$this->table = $table;
1923
$this->column = $column;
24+
$this->encoder = $encoder;
2025
}
2126
}

src/DataConverter/Field/DBase/CharConverter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static function getType(): string
1515
public function fromBinaryString(string $value)
1616
{
1717
if ($inCharset = $this->table->options['encoding']) {
18-
$value = iconv($inCharset, 'utf-8', $value);
18+
$value = $this->encoder->encode($value, $inCharset, 'utf-8');
1919
}
2020

2121
return trim($value);
@@ -24,7 +24,7 @@ public function fromBinaryString(string $value)
2424
public function toBinaryString($value): string
2525
{
2626
if ($value && $outCharset = $this->table->options['encoding']) {
27-
$value = iconv('utf-8', $outCharset, $value);
27+
$value = $this->encoder->encode($value, 'utf-8', $outCharset);
2828
}
2929

3030
return str_pad($value ?? '', $this->column->length);

src/DataConverter/Field/VisualFoxpro/VarFieldConverter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function fromBinaryString(string $value): string
1919
}
2020

2121
if ($inCharset = $this->table->options['encoding']) {
22-
$value = iconv($inCharset, 'utf-8', $value);
22+
$value = $this->encoder->encode($value, $inCharset, 'utf-8');
2323
}
2424

2525
return $value;
@@ -32,7 +32,7 @@ public function toBinaryString($value): string
3232
{
3333
$value = $value ?? '';
3434
if ($outCharset = $this->table->options['encoding']) {
35-
$value = iconv('utf-8', $outCharset, $value);
35+
$value = $this->encoder->encode($value, 'utf-8', $outCharset);
3636
}
3737

3838
return str_pad($value, $this->column->length - 1, chr(0x00)).chr(0x03);

src/DataConverter/Record/DBaseDataConverter.php

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

33
namespace XBase\DataConverter\Record;
44

5+
use XBase\DataConverter\Encoder\EncoderInterface;
56
use XBase\DataConverter\Field\DBase\CharConverter;
67
use XBase\DataConverter\Field\DBase\DateConverter;
78
use XBase\DataConverter\Field\DBase\IgnoreConverter;
@@ -20,9 +21,13 @@ class DBaseDataConverter implements RecordDataConverterInterface
2021
/** @var Table */
2122
protected $table;
2223

23-
public function __construct(Table $table)
24+
/** @var EncoderInterface */
25+
protected $encoder;
26+
27+
public function __construct(Table $table, EncoderInterface $encoder)
2428
{
2529
$this->table = $table;
30+
$this->encoder = $encoder;
2631
}
2732

2833
/**
@@ -80,7 +85,7 @@ private function findFieldConverter(Column $column): FieldDataConverterInterface
8085
{
8186
foreach (static::getFieldConverters() as $class) {
8287
if ($column->type === $class::getType()) {
83-
return new $class($this->table, $column);
88+
return new $class($this->table, $column, $this->encoder);
8489
}
8590
}
8691

src/Memo/AbstractMemo.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace XBase\Memo;
44

5+
use XBase\DataConverter\Encoder\EncoderInterface;
56
use XBase\Stream\Stream;
67
use XBase\Table\Table;
78

@@ -16,12 +17,16 @@ abstract class AbstractMemo implements MemoInterface
1617
/** @var string */
1718
protected $filepath;
1819

20+
/** @var EncoderInterface */
21+
protected $encoder;
22+
1923
/**
2024
* @param string $filepath Path to memo file
2125
*/
22-
public function __construct(Table $table, string $filepath)
26+
public function __construct(Table $table, string $filepath, EncoderInterface $encoder)
2327
{
2428
$this->table = $table;
29+
$this->encoder = $encoder;
2530

2631
$this->filepath = $filepath;
2732
$this->open();

src/Memo/DBase3Memo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function get(int $pointer): ?MemoObject
5151
$result = substr($result, 0, -1); // remove endline symbol (0x00)
5252
}
5353
if ($this->table->options['encoding']) {
54-
$result = iconv($this->table->options['encoding'], 'utf-8', $result);
54+
$result = $this->encoder->encode($result, $this->table->options['encoding'], 'utf-8');
5555
}
5656
}
5757

src/Memo/DBase4Memo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function get(int $pointer): ?MemoObject
4848
$info = $this->guessDataType($result);
4949
assert(isset($info['type']));
5050
if (MemoObject::TYPE_TEXT === $info['type'] && $this->table->options['encoding']) {
51-
$result = iconv($this->table->options['encoding'], 'utf-8', $result);
51+
$result = $this->encoder->encode($result, $this->table->options['encoding'], 'utf-8');
5252
}
5353

5454
return new MemoObject($result, $info['type'], $pointer, $memoLength[1]);

src/Memo/FoxproMemo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function get(int $pointer): ?MemoObject
5656
$info = $this->guessDataType($result);
5757
assert(isset($info['type']));
5858
if ($this->table->options['encoding']) {
59-
$result = iconv($this->table->options['encoding'], 'utf-8', $result);
59+
$result = $this->encoder->encode($result, $this->table->options['encoding'], 'utf-8');
6060
}
6161

6262
return new MemoObject($result, $info['type'], $pointer, $memoLength[1], $info);

0 commit comments

Comments
 (0)