Skip to content

Commit

Permalink
Table and Memo second argument is options array now
Browse files Browse the repository at this point in the history
  • Loading branch information
gam6itko committed Dec 8, 2020
1 parent 7dbce69 commit 02dafd1
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 72 deletions.
2 changes: 1 addition & 1 deletion example/test-memo.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
die('Bad path to file realpath '.$argv[1]);
}

$table = new Table($filepath, null, 'cp1252');
$table = new Table($filepath, ['encoding' => 'cp1252']);
echo 'Record count: '.$table->getRecordCount() . PHP_EOL;

$columns = $table->getColumns();
Expand Down
3 changes: 3 additions & 0 deletions src/XBase/BlocksMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function get(): array
return $this->squeeze();
}

/**
* Combines several adjacent blocks into one.
*/
private function squeeze(): array
{
$pointers = array_column($this->blocksToDelete, 0);
Expand Down
29 changes: 22 additions & 7 deletions src/XBase/Memo/AbstractMemo.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,38 @@ abstract class AbstractMemo implements MemoInterface
/** @var string */
protected $filepath;

/** @var string */
protected $convertFrom;
/** @var array */
protected $options = [];

/**
* Memo constructor.
*
* @param string $convertFrom
* @param string $filepath Path to memo file
* @param array $options Array of options:<br>
* encoding - convert text data from<br>
* writable - edit mode<br>
*/
public function __construct(Table $table, string $filepath, ?string $convertFrom = null)
public function __construct(Table $table, string $filepath, $options = [])
{
$this->table = $table;
$this->filepath = $filepath;
$this->convertFrom = $convertFrom; //todo autodetect from languageCode
$this->options = $this->resolveOptions($options);
$this->open();
$this->readHeader();
}

protected function resolveOptions($options = []): array
{
if (is_string($options)) {
@trigger_error('You should pass convertFrom as `encoding` option');
$options = ['encoding' => $options];
}

$options = array_merge([
'encoding' => null,
], $options);

return $options;
}

public function __destruct()
{
$this->close();
Expand Down
15 changes: 6 additions & 9 deletions src/XBase/Memo/AbstractWritableMemo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@

use XBase\BlocksMerger;
use XBase\Stream\Stream;
use XBase\Table;
use XBase\Traits\CloneTrait;

abstract class AbstractWritableMemo extends AbstractMemo implements WritableMemoInterface
{
use CloneTrait;

/** @var bool */
protected $writable = false;

/**
* @var BlocksMerger Garbage blocks. Delete blocks while saving.
*/
Expand All @@ -26,15 +22,16 @@ abstract protected function getBlockLengthInBytes(): int;

abstract protected function calculateBlockCount(string $data): int;

public function __construct(Table $table, string $filepath, ?string $convertFrom = null, bool $writable = false)
protected function resolveOptions($options = []): array
{
$this->writable = $writable;
parent::__construct($table, $filepath, $convertFrom);
return array_merge([
'writable' => false,
], parent::resolveOptions($options));
}

public function open(): void
{
if (!$this->writable) {
if (!$this->options['writable']) {
parent::open();

return;
Expand All @@ -54,7 +51,7 @@ protected function readHeader(): void
public function close(): void
{
parent::close();
if ($this->writable && $this->cloneFilepath) {
if ($this->options['writable'] && $this->cloneFilepath) {
unlink($this->cloneFilepath);
$this->cloneFilepath = null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/XBase/Memo/DBase3Memo.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public function get(int $pointer): ?MemoObject
if (chr(0x00) === substr($result, -1)) {
$result = substr($result, 0, -1); // remove endline symbol (0x00)
}
if ($this->convertFrom) {
$result = iconv($this->convertFrom, 'utf-8', $result);
if ($this->options['encoding']) {
$result = iconv($this->options['encoding'], 'utf-8', $result);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/XBase/Memo/DBase4Memo.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public function get(int $pointer): ?MemoObject
// $result = $this->fp->read($memoLength[1] - self::BLOCK_SIGN_LENGTH - self::BLOCK_LENGTH_LENGTH);

$type = $this->guessDataType($result);
if (MemoObject::TYPE_TEXT === $type && $this->convertFrom) {
$result = iconv($this->convertFrom, 'utf-8', $result);
if (MemoObject::TYPE_TEXT === $type && $this->options['encoding']) {
$result = iconv($this->options['encoding'], 'utf-8', $result);
}

return new MemoObject($result, $type, $pointer, $memoLength[1]);
Expand Down
4 changes: 2 additions & 2 deletions src/XBase/Memo/FoxproMemo.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public function get(int $pointer): ?MemoObject
$result = $this->fp->read($memoLength[1]);

$type = $this->guessDataType($result);
if ($this->convertFrom) {
$result = iconv($this->convertFrom, 'utf-8', $result);
if ($this->options['encoding']) {
$result = iconv($this->options['encoding'], 'utf-8', $result);
}

return new MemoObject($result, $type, $pointer, $memoLength[1]);
Expand Down
4 changes: 2 additions & 2 deletions src/XBase/Memo/MemoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class MemoFactory
{
public static function create(Table $table, bool $writable = false): ?MemoInterface
public static function create(Table $table, array $options = []): ?MemoInterface
{
$class = self::getClass($table->getVersion());
$refClass = new \ReflectionClass($class);
Expand All @@ -26,7 +26,7 @@ public static function create(Table $table, bool $writable = false): ?MemoInterf
return null; //todo create file?
}

return $refClass->newInstance($table, $memoFilepath, $table->getConvertFrom(), $writable);
return $refClass->newInstance($table, $memoFilepath, $options);
}

private static function getClass(int $version): string
Expand Down
46 changes: 33 additions & 13 deletions src/XBase/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ class Table
/** @var string Table filepath. */
protected $filepath;

/** @var array|null */
protected $availableColumns;

/** @var Stream */
protected $fp;

Expand All @@ -48,9 +45,6 @@ class Table
/** @var RecordInterface|null */
protected $record;

/** @var string|null */
protected $convertFrom;

/**
* @var int
*
Expand Down Expand Up @@ -137,25 +131,51 @@ class Table
/** @var string|null DBase7 only */
protected $languageName;

/** @var array */
protected $options = [];

/**
* Table constructor.
*
* @param array|null $availableColumns
* @param string|null $convertFrom Encoding of file
* @param array $options Array of options:<br>
* encoding - convert text data from<br>
* columns - available columns<br>
*
* @throws \Exception
*/
public function __construct(string $filepath, $availableColumns = null, $convertFrom = null)
public function __construct(string $filepath, $options = [], $convertFrom = null)
{
$this->filepath = $filepath;
$this->availableColumns = $availableColumns;
$this->convertFrom = $convertFrom; //todo autodetect from languageCode
$this->options = $this->resolveOptions($options, $convertFrom);

$this->open();
$this->readHeader();
$this->openMemo();
}

protected function resolveOptions($options, $convertFrom = null): array
{
// right options
if (!empty($options) && array_intersect(['encoding', 'columns'], array_keys($options))) {
return array_merge([
'columns' => [],
'encoding' => null,
], $options);
}

if (!empty($options)) {
@trigger_error('You should pass availableColumns as `columns` option');
}
if (!empty($convertFrom)) {
@trigger_error('You should pass convertFrom as `encoding` option');
}

return [
'columns' => $options ?? [],
'encoding' => $convertFrom,
];
}

protected function open(): void
{
if (!file_exists($this->filepath)) {
Expand Down Expand Up @@ -206,7 +226,7 @@ protected function readHeader(): void
protected function openMemo(): void
{
if (TableType::hasMemo($this->getVersion())) {
$this->memo = MemoFactory::create($this);
$this->memo = MemoFactory::create($this, $this->options);
}
}

Expand Down Expand Up @@ -517,7 +537,7 @@ public function getDeleteCount()

public function getConvertFrom(): ?string
{
return $this->convertFrom;
return $this->options['encoding'];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/XBase/Traits/CloneTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
trait CloneTrait
{
/** @var string */
/** @var string|null */
private $cloneFilepath;

/**
Expand Down
Loading

0 comments on commit 02dafd1

Please sign in to comment.