Skip to content

Commit

Permalink
fix 89
Browse files Browse the repository at this point in the history
  • Loading branch information
gam6itko committed Sep 10, 2020
1 parent e338d20 commit 2267452
Show file tree
Hide file tree
Showing 11 changed files with 365 additions and 183 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/vendor
/.php_cs.cache
/composer.lock
/.phpunit.result.cache
10 changes: 10 additions & 0 deletions src/XBase/Column/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,32 @@ abstract class AbstractColumn implements ColumnInterface
{
/** @var string */
protected $name;

/** @var string */
protected $rawName;

/** @var string */
protected $type;

/** @var int */
protected $length;

/** @var int */
protected $decimalCount;

/**@var int Field address within record. */
protected $memAddress;

protected $workAreaID;

/** @var bool */
protected $setFields;

protected $indexed;

/** @var int|null Data starts from index */
protected $bytePos;

/** @var int */
protected $colIndex;

Expand Down
5 changes: 5 additions & 0 deletions src/XBase/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public static function getClass(string $version): string
case TableType::DBASE_7_NOMEMO:
return DBase7Column::class;

case TableType::VISUAL_FOXPRO:
case TableType::VISUAL_FOXPRO_AI:
case TableType::VISUAL_FOXPRO_VAR:
return VisualFoxproColumn::class;

default:
return DBaseColumn::class;
}
Expand Down
65 changes: 51 additions & 14 deletions src/XBase/Column/DBaseColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

class DBaseColumn extends AbstractColumn
{
protected $reserved1;

protected $reserved2;

protected $reserved3;

public static function getHeaderLength(): int
{
return 32;
Expand All @@ -20,25 +26,38 @@ public static function create(string $memoryChunk, int $colIndex, ?int $bytePos

$s = Stream::createFromString($memoryChunk);

return new self(
$s->read(11),
$s->read(),
$s->readUInt(),
$s->readUChar(),
$s->readUChar(),
$s->read(2),
$s->readUChar(),
$s->read(2),
0 !== $s->read(),
$s->read(7),
0 !== $s->read(),
return new static(
$s->read(11),//0-10
$s->read(),//11
$s->readUInt(),//12-15
$s->readUChar(),//16
$s->readUChar(),//17
$s->read(2),//18-19
$s->readUChar(),//20
$s->read(2),//21-22
0 !== $s->readUChar(),//23
$s->read(7),//24-30
0 !== $s->readUChar(),//31
$colIndex,
$bytePos
);
}

public function __construct(string $name, string $type, int $memAddress, int $length, int $decimalCount, $reserved1, int $workAreaID, $reserved2, bool $setFields, $reserved3, bool $indexed, int $colIndex, ?int $bytePos = null)
{
public function __construct(
string $name,
string $type,
int $memAddress,
int $length,
int $decimalCount,
$reserved1,
int $workAreaID,
$reserved2,
bool $setFields,
$reserved3,
bool $indexed,
int $colIndex,
?int $bytePos = null
) {
$name = (false !== strpos($name, chr(0x00))) ? substr($name, 0, strpos($name, chr(0x00))) : $name;

$this->rawName = $name;
Expand All @@ -48,8 +67,11 @@ public function __construct(string $name, string $type, int $memAddress, int $le
$this->memAddress = $memAddress;
$this->length = $length;
$this->decimalCount = $decimalCount;
$this->reserved1 = $reserved1;
$this->workAreaID = $workAreaID;
$this->reserved2 = $reserved2;
$this->setFields = $setFields;
$this->reserved3 = $reserved3;
$this->indexed = $indexed;
$this->colIndex = $colIndex;
$this->bytePos = $bytePos;
Expand Down Expand Up @@ -80,4 +102,19 @@ public function toString()
{
return $this->name;
}

public function getReserved1()
{
return $this->reserved1;
}

public function getReserved2()
{
return $this->reserved2;
}

public function getReserved3()
{
return $this->reserved3;
}
}
19 changes: 19 additions & 0 deletions src/XBase/Column/VisualFoxproColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace XBase\Column;

use XBase\Enum\FieldType;

class VisualFoxproColumn extends DBaseColumn
{
public function getDataLength()
{
switch ($this->type) {
case FieldType::BLOB:
case FieldType::MEMO:
return 4;
default:
return parent::getDataLength();
}
}
}
2 changes: 1 addition & 1 deletion src/XBase/Memo/MemoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static function create(Table $table): ?MemoInterface
}
$memoFilepath = $fileInfo['dirname'].DIRECTORY_SEPARATOR.$fileInfo['filename'].$memoExt;
if (!file_exists($memoFilepath)) {
return null;
return null; //todo create file?
}

return $refClass->newInstance($memoFilepath, $table->getConvertFrom());
Expand Down
23 changes: 15 additions & 8 deletions src/XBase/Record/AbstractRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public function isInserted()
return $this->inserted;
}

public function setInserted(bool $inserted): void
{
$this->inserted = $inserted;
}

/**
* @return ColumnInterface[]
*/
Expand Down Expand Up @@ -399,16 +404,16 @@ public function setObject(ColumnInterface $column, $value)
case FieldType::CHAR:
$this->setString($column, $value);
return false;
case FieldType::DOUBLE:
case FieldType::FLOAT:
$this->setFloat($column, $value);
return false;
// case FieldType::DOUBLE:
// case FieldType::FLOAT:
// $this->setFloat($column, $value);
// return false;
case FieldType::DATE:
$this->setDate($column, $value);
return false;
case FieldType::DATETIME:
$this->setDateTime($column, $value);
return false;
// case FieldType::DATETIME:
// $this->setDateTime($column, $value);
// return false;
case FieldType::LOGICAL:
$this->setBoolean($column, $value);
return false;
Expand Down Expand Up @@ -439,14 +444,16 @@ public function setDate(ColumnInterface $column, $value)
if ($value instanceof \DateTimeInterface) {
$this->forceSetString($column, $value->format('Ymd'));
return false;
} elseif (is_int($value)) {
$value = date('Ymd', $value);
}

if (0 == strlen($value)) {
$this->forceSetString($column, '');
return false;
}

$this->forceSetString($column, date('Ymd', $value));
$this->forceSetString($column, $value);

return true;
}
Expand Down
86 changes: 64 additions & 22 deletions src/XBase/Record/VisualFoxproRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ public function getObject(ColumnInterface $column)
}
}

public function setObject(ColumnInterface $column, $value)
{
switch ($column->getType()) {
case FieldType::INTEGER:
return $this->setInt($column, $value);
case FieldType::DOUBLE:
return $this->setDouble($column, $value);
case FieldType::DATETIME:
return $this->setDateTime($column, $value);
case FieldType::CURRENCY:
return $this->setCurrency($column, $value);
case FieldType::FLOAT:
return $this->setFloat($column, $value);
case FieldType::VAR_FIELD:
case FieldType::VARBINARY:
return $this->setVarchar($column, $value);
default:
return parent::setObject($column, $value);
}
}

public function getGeneral(string $columnName)
{
$data = unpack('L', $this->choppedData[$columnName]);
Expand Down Expand Up @@ -67,6 +88,26 @@ public function getInt(string $columnName)
return $ret;
}

/**
* @param $value
*
* @return bool
*/
public function setInt(ColumnInterface $column, $value)
{
if (FieldType::INTEGER !== $column->getType()) {
trigger_error($column->getName().' is not a Number column', E_USER_ERROR);
}

if (0 == strlen($value)) {
$this->forceSetString($column, '');
return false;
}

$value = str_replace(',', '.', $value);
$this->forceSetString($column, number_format($value, $column->getDecimalCount(), '.', ''));
}

/**
* @return int
*/
Expand All @@ -83,6 +124,13 @@ public function getDouble(string $columnName)
return 0;
}

public function setDouble(ColumnInterface $column, $value): self
{
//todo

return $this;
}

public function getCurrency(string $columnName)
{
$s = $this->choppedData[$columnName];
Expand All @@ -96,6 +144,13 @@ public function getCurrency(string $columnName)
return 0;
}

private function setCurrency(ColumnInterface $column, $value): self
{
//todo

return $this;
}

public function getVarchar(string $columnName)
{
$s = $this->forceGetString($columnName);
Expand All @@ -105,6 +160,13 @@ public function getVarchar(string $columnName)
return $s;
}

private function setVarchar(ColumnInterface $column, $value): self
{
//todo

return $this;
}

public function getVarbinary(string $columnName)
{
$s = $this->forceGetString($columnName);
Expand All @@ -121,7 +183,7 @@ public function getVarbinary(string $columnName)
*/
public function setDateTime(ColumnInterface $column, $value)
{
if (FieldType::DATETIME != $column->getType()) {
if (FieldType::DATETIME !== $column->getType()) {
trigger_error($column->getName().' is not a DateTime column', E_USER_ERROR);
}

Expand All @@ -148,7 +210,7 @@ public function setDateTime(ColumnInterface $column, $value)
*/
public function setFloat(ColumnInterface $column, $value)
{
if (FieldType::FLOAT != $column->getType()) {
if (FieldType::FLOAT !== $column->getType()) {
trigger_error($column->getName().' is not a Float column', E_USER_ERROR);
}

Expand All @@ -161,26 +223,6 @@ public function setFloat(ColumnInterface $column, $value)
$this->forceSetString($column, $value);
}

/**
* @param $value
*
* @return bool
*/
public function setInt(ColumnInterface $column, $value)
{
if (FieldType::NUMERIC != $column->getType()) {
trigger_error($column->getName().' is not a Number column', E_USER_ERROR);
}

if (0 == strlen($value)) {
$this->forceSetString($column, '');
return false;
}

$value = str_replace(',', '.', $value);
$this->forceSetString($column, number_format($value, $column->getDecimalCount(), '.', ''));
}

/**
* @return bool|float|int
*/
Expand Down
Loading

0 comments on commit 2267452

Please sign in to comment.