Skip to content

Commit 2267452

Browse files
committed
fix 89
1 parent e338d20 commit 2267452

File tree

11 files changed

+365
-183
lines changed

11 files changed

+365
-183
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/vendor
33
/.php_cs.cache
44
/composer.lock
5+
/.phpunit.result.cache

src/XBase/Column/AbstractColumn.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,32 @@ abstract class AbstractColumn implements ColumnInterface
66
{
77
/** @var string */
88
protected $name;
9+
910
/** @var string */
1011
protected $rawName;
12+
1113
/** @var string */
1214
protected $type;
15+
1316
/** @var int */
1417
protected $length;
18+
1519
/** @var int */
1620
protected $decimalCount;
1721

1822
/**@var int Field address within record. */
1923
protected $memAddress;
24+
2025
protected $workAreaID;
26+
27+
/** @var bool */
2128
protected $setFields;
29+
2230
protected $indexed;
31+
2332
/** @var int|null Data starts from index */
2433
protected $bytePos;
34+
2535
/** @var int */
2636
protected $colIndex;
2737

src/XBase/Column/ColumnFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public static function getClass(string $version): string
1313
case TableType::DBASE_7_NOMEMO:
1414
return DBase7Column::class;
1515

16+
case TableType::VISUAL_FOXPRO:
17+
case TableType::VISUAL_FOXPRO_AI:
18+
case TableType::VISUAL_FOXPRO_VAR:
19+
return VisualFoxproColumn::class;
20+
1621
default:
1722
return DBaseColumn::class;
1823
}

src/XBase/Column/DBaseColumn.php

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
class DBaseColumn extends AbstractColumn
99
{
10+
protected $reserved1;
11+
12+
protected $reserved2;
13+
14+
protected $reserved3;
15+
1016
public static function getHeaderLength(): int
1117
{
1218
return 32;
@@ -20,25 +26,38 @@ public static function create(string $memoryChunk, int $colIndex, ?int $bytePos
2026

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

23-
return new self(
24-
$s->read(11),
25-
$s->read(),
26-
$s->readUInt(),
27-
$s->readUChar(),
28-
$s->readUChar(),
29-
$s->read(2),
30-
$s->readUChar(),
31-
$s->read(2),
32-
0 !== $s->read(),
33-
$s->read(7),
34-
0 !== $s->read(),
29+
return new static(
30+
$s->read(11),//0-10
31+
$s->read(),//11
32+
$s->readUInt(),//12-15
33+
$s->readUChar(),//16
34+
$s->readUChar(),//17
35+
$s->read(2),//18-19
36+
$s->readUChar(),//20
37+
$s->read(2),//21-22
38+
0 !== $s->readUChar(),//23
39+
$s->read(7),//24-30
40+
0 !== $s->readUChar(),//31
3541
$colIndex,
3642
$bytePos
3743
);
3844
}
3945

40-
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)
41-
{
46+
public function __construct(
47+
string $name,
48+
string $type,
49+
int $memAddress,
50+
int $length,
51+
int $decimalCount,
52+
$reserved1,
53+
int $workAreaID,
54+
$reserved2,
55+
bool $setFields,
56+
$reserved3,
57+
bool $indexed,
58+
int $colIndex,
59+
?int $bytePos = null
60+
) {
4261
$name = (false !== strpos($name, chr(0x00))) ? substr($name, 0, strpos($name, chr(0x00))) : $name;
4362

4463
$this->rawName = $name;
@@ -48,8 +67,11 @@ public function __construct(string $name, string $type, int $memAddress, int $le
4867
$this->memAddress = $memAddress;
4968
$this->length = $length;
5069
$this->decimalCount = $decimalCount;
70+
$this->reserved1 = $reserved1;
5171
$this->workAreaID = $workAreaID;
72+
$this->reserved2 = $reserved2;
5273
$this->setFields = $setFields;
74+
$this->reserved3 = $reserved3;
5375
$this->indexed = $indexed;
5476
$this->colIndex = $colIndex;
5577
$this->bytePos = $bytePos;
@@ -80,4 +102,19 @@ public function toString()
80102
{
81103
return $this->name;
82104
}
105+
106+
public function getReserved1()
107+
{
108+
return $this->reserved1;
109+
}
110+
111+
public function getReserved2()
112+
{
113+
return $this->reserved2;
114+
}
115+
116+
public function getReserved3()
117+
{
118+
return $this->reserved3;
119+
}
83120
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace XBase\Column;
4+
5+
use XBase\Enum\FieldType;
6+
7+
class VisualFoxproColumn extends DBaseColumn
8+
{
9+
public function getDataLength()
10+
{
11+
switch ($this->type) {
12+
case FieldType::BLOB:
13+
case FieldType::MEMO:
14+
return 4;
15+
default:
16+
return parent::getDataLength();
17+
}
18+
}
19+
}

src/XBase/Memo/MemoFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function create(Table $table): ?MemoInterface
2424
}
2525
$memoFilepath = $fileInfo['dirname'].DIRECTORY_SEPARATOR.$fileInfo['filename'].$memoExt;
2626
if (!file_exists($memoFilepath)) {
27-
return null;
27+
return null; //todo create file?
2828
}
2929

3030
return $refClass->newInstance($memoFilepath, $table->getConvertFrom());

src/XBase/Record/AbstractRecord.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ public function isInserted()
8383
return $this->inserted;
8484
}
8585

86+
public function setInserted(bool $inserted): void
87+
{
88+
$this->inserted = $inserted;
89+
}
90+
8691
/**
8792
* @return ColumnInterface[]
8893
*/
@@ -399,16 +404,16 @@ public function setObject(ColumnInterface $column, $value)
399404
case FieldType::CHAR:
400405
$this->setString($column, $value);
401406
return false;
402-
case FieldType::DOUBLE:
403-
case FieldType::FLOAT:
404-
$this->setFloat($column, $value);
405-
return false;
407+
// case FieldType::DOUBLE:
408+
// case FieldType::FLOAT:
409+
// $this->setFloat($column, $value);
410+
// return false;
406411
case FieldType::DATE:
407412
$this->setDate($column, $value);
408413
return false;
409-
case FieldType::DATETIME:
410-
$this->setDateTime($column, $value);
411-
return false;
414+
// case FieldType::DATETIME:
415+
// $this->setDateTime($column, $value);
416+
// return false;
412417
case FieldType::LOGICAL:
413418
$this->setBoolean($column, $value);
414419
return false;
@@ -439,14 +444,16 @@ public function setDate(ColumnInterface $column, $value)
439444
if ($value instanceof \DateTimeInterface) {
440445
$this->forceSetString($column, $value->format('Ymd'));
441446
return false;
447+
} elseif (is_int($value)) {
448+
$value = date('Ymd', $value);
442449
}
443450

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

449-
$this->forceSetString($column, date('Ymd', $value));
456+
$this->forceSetString($column, $value);
450457

451458
return true;
452459
}

src/XBase/Record/VisualFoxproRecord.php

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@ public function getObject(ColumnInterface $column)
3434
}
3535
}
3636

37+
public function setObject(ColumnInterface $column, $value)
38+
{
39+
switch ($column->getType()) {
40+
case FieldType::INTEGER:
41+
return $this->setInt($column, $value);
42+
case FieldType::DOUBLE:
43+
return $this->setDouble($column, $value);
44+
case FieldType::DATETIME:
45+
return $this->setDateTime($column, $value);
46+
case FieldType::CURRENCY:
47+
return $this->setCurrency($column, $value);
48+
case FieldType::FLOAT:
49+
return $this->setFloat($column, $value);
50+
case FieldType::VAR_FIELD:
51+
case FieldType::VARBINARY:
52+
return $this->setVarchar($column, $value);
53+
default:
54+
return parent::setObject($column, $value);
55+
}
56+
}
57+
3758
public function getGeneral(string $columnName)
3859
{
3960
$data = unpack('L', $this->choppedData[$columnName]);
@@ -67,6 +88,26 @@ public function getInt(string $columnName)
6788
return $ret;
6889
}
6990

91+
/**
92+
* @param $value
93+
*
94+
* @return bool
95+
*/
96+
public function setInt(ColumnInterface $column, $value)
97+
{
98+
if (FieldType::INTEGER !== $column->getType()) {
99+
trigger_error($column->getName().' is not a Number column', E_USER_ERROR);
100+
}
101+
102+
if (0 == strlen($value)) {
103+
$this->forceSetString($column, '');
104+
return false;
105+
}
106+
107+
$value = str_replace(',', '.', $value);
108+
$this->forceSetString($column, number_format($value, $column->getDecimalCount(), '.', ''));
109+
}
110+
70111
/**
71112
* @return int
72113
*/
@@ -83,6 +124,13 @@ public function getDouble(string $columnName)
83124
return 0;
84125
}
85126

127+
public function setDouble(ColumnInterface $column, $value): self
128+
{
129+
//todo
130+
131+
return $this;
132+
}
133+
86134
public function getCurrency(string $columnName)
87135
{
88136
$s = $this->choppedData[$columnName];
@@ -96,6 +144,13 @@ public function getCurrency(string $columnName)
96144
return 0;
97145
}
98146

147+
private function setCurrency(ColumnInterface $column, $value): self
148+
{
149+
//todo
150+
151+
return $this;
152+
}
153+
99154
public function getVarchar(string $columnName)
100155
{
101156
$s = $this->forceGetString($columnName);
@@ -105,6 +160,13 @@ public function getVarchar(string $columnName)
105160
return $s;
106161
}
107162

163+
private function setVarchar(ColumnInterface $column, $value): self
164+
{
165+
//todo
166+
167+
return $this;
168+
}
169+
108170
public function getVarbinary(string $columnName)
109171
{
110172
$s = $this->forceGetString($columnName);
@@ -121,7 +183,7 @@ public function getVarbinary(string $columnName)
121183
*/
122184
public function setDateTime(ColumnInterface $column, $value)
123185
{
124-
if (FieldType::DATETIME != $column->getType()) {
186+
if (FieldType::DATETIME !== $column->getType()) {
125187
trigger_error($column->getName().' is not a DateTime column', E_USER_ERROR);
126188
}
127189

@@ -148,7 +210,7 @@ public function setDateTime(ColumnInterface $column, $value)
148210
*/
149211
public function setFloat(ColumnInterface $column, $value)
150212
{
151-
if (FieldType::FLOAT != $column->getType()) {
213+
if (FieldType::FLOAT !== $column->getType()) {
152214
trigger_error($column->getName().' is not a Float column', E_USER_ERROR);
153215
}
154216

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

164-
/**
165-
* @param $value
166-
*
167-
* @return bool
168-
*/
169-
public function setInt(ColumnInterface $column, $value)
170-
{
171-
if (FieldType::NUMERIC != $column->getType()) {
172-
trigger_error($column->getName().' is not a Number column', E_USER_ERROR);
173-
}
174-
175-
if (0 == strlen($value)) {
176-
$this->forceSetString($column, '');
177-
return false;
178-
}
179-
180-
$value = str_replace(',', '.', $value);
181-
$this->forceSetString($column, number_format($value, $column->getDecimalCount(), '.', ''));
182-
}
183-
184226
/**
185227
* @return bool|float|int
186228
*/

0 commit comments

Comments
 (0)