Skip to content

Commit df7332d

Browse files
authored
Merge pull request #77 from gam6itko/master
php7.1; tests; travis
2 parents ce78739 + 66b96e0 commit df7332d

File tree

13 files changed

+403
-46
lines changed

13 files changed

+403
-46
lines changed

.gitattributes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.* export-ignore
2+
/examples export-ignore
3+
/tests export-ignore
4+
phpunit.xml.dist export-ignore

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/.idea
12
/vendor
23
/.php_cs.cache
34
/composer.lock

.php_cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
$rules = [
4+
'@PSR2' => true,
5+
];
6+
7+
$finder = PhpCsFixer\Finder::create()
8+
->in('src')
9+
->in('tests');
10+
11+
return PhpCsFixer\Config::create()
12+
->setFinder($finder)
13+
->setRiskyAllowed(true)
14+
->setRules($rules)
15+
->setUsingCache(true);

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: php
2+
php:
3+
- '7.1'
4+
- '7.2'
5+
- '7.3'
6+
- '7.4snapshot'
7+
directories:
8+
- $HOME/.composer/cache/files
9+
script:
10+
- composer install
11+
- vendor/bin/phpunit --configuration phpunit.xml.dist

README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ $ composer require hisamu/php-xbase
1313
Sample usage
1414
-----
1515
``` php
16-
<?php
17-
1816
use XBase\Table;
1917

2018
$table = new Table(dirname(__FILE__).'/test.dbf');
@@ -38,8 +36,6 @@ Performance
3836
You can pass an array of the columns that you need to the constructor, then if your table has columns that you don't use they will not be loaded. This way the parser can run a lot faster.
3937

4038
``` php
41-
<?php
42-
4339
use XBase\Table;
4440

4541
$table = new Table(dirname(__FILE__).'/test.dbf', array('my_column', 'another_column'));
@@ -65,8 +61,6 @@ Writing Data
6561
To open a table for writing, you have to use a `WritableTable` object, as on this example:
6662

6763
``` php
68-
<?php
69-
7064
use XBase\WritableTable;
7165

7266
$table = new WritableTable(dirname(__FILE__).'/test.dbf');
@@ -82,6 +76,23 @@ for ($i = 0; $i < 10; $i++) {
8276
$table->close();
8377
```
8478

79+
Delete record
80+
-----
81+
82+
``` php
83+
use XBase\WritableTable;
84+
85+
$table = new WritableTable('file.dbf');
86+
$table->openWrite();
87+
while ($record = $table->nextRecord()) {
88+
if ($record->getBoolean('delete_this_row')){
89+
$table->deleteRecord(); //mark record deleted
90+
}
91+
}
92+
$table->pack(); //save
93+
$table->close();
94+
```
95+
8596
Troubleshooting
8697
-----
8798

composer.json

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "hisamu/php-xbase",
33
"type": "library",
44
"description": "A simple parser for *.dbf, *.fpt files using PHP",
5-
"keywords": ["dbase", "foxpro"],
5+
"keywords": ["dbase", "dbf","foxpro"],
66
"homepage": "https://github.com/luads/php-xbase",
77
"license": "MIT",
88
"authors" : [
@@ -16,11 +16,24 @@
1616
}
1717
],
1818
"require": {
19-
"php": ">=5.3.0"
19+
"php": ">=7.1"
2020
},
2121
"autoload": {
22-
"psr-0": {
23-
"XBase": "src/"
22+
"psr-4": {
23+
"XBase\\": "src/XBase"
24+
}
25+
},
26+
"require-dev": {
27+
"phpunit/phpunit": "^7.0"
28+
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"XBase\\Tests\\": "tests/"
32+
}
33+
},
34+
"extra": {
35+
"branch-alias": {
36+
"dev-master": "1.1.x-dev"
2437
}
2538
}
2639
}

phpunit.xml.dist

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<phpunit bootstrap="vendor/autoload.php">
2+
<testsuites>
3+
<testsuite name="php-xbase">
4+
<directory>tests</directory>
5+
</testsuite>
6+
</testsuites>
7+
</phpunit>

src/XBase/Column.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Column
88
public $name;
99
/** @var string */
1010
public $rawname;
11-
/** @var */
11+
/** @var string */
1212
public $type;
1313
/** @var int */
1414
public $length;

src/XBase/Record.php

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ class Record
4646
* Record constructor.
4747
*
4848
* @param Table $table
49-
* @param $recordIndex
50-
* @param bool $rawData
49+
* @param $recordIndex
50+
* @param bool $rawData
5151
*/
5252
public function __construct(Table $table, $recordIndex, $rawData = false)
5353
{
5454
$this->table = $table;
5555
$this->memoFile = $table->memoFile;
5656
$this->recordIndex = $recordIndex;
57-
$this->choppedData = array();
57+
$this->choppedData = [];
5858

5959
if ($rawData && strlen($rawData) > 0) {
6060
$this->inserted = false;
@@ -131,8 +131,14 @@ public function getRecordIndex()
131131
return $this->recordIndex;
132132
}
133133

134+
public function setRecordIndex($index)
135+
{
136+
$this->recordIndex = $index;
137+
}
138+
134139
/**
135140
* @param string $columnName
141+
*
136142
* @return mixed
137143
*/
138144
public function getString($columnName)
@@ -345,7 +351,7 @@ public function getNum($columnName)
345351
{
346352
$s = $this->forceGetString($columnName);
347353

348-
if (!$s) {
354+
if (!is_string($s)) {
349355
return false;
350356
}
351357

@@ -364,7 +370,7 @@ public function getNum($columnName)
364370

365371
/**
366372
* @param string $columnName
367-
* @param $length
373+
* @param $length
368374
*
369375
* @return bool|float|int
370376
*/
@@ -426,7 +432,7 @@ public function setStringByIndex($columnIndex, $value)
426432

427433
/**
428434
* @param Column $columnObj
429-
* @param $value
435+
* @param $value
430436
*/
431437
public function setString($columnObj, $value)
432438
{
@@ -443,7 +449,7 @@ public function setString($columnObj, $value)
443449

444450
/**
445451
* @param Column $columnObj
446-
* @param $value
452+
* @param $value
447453
*/
448454
public function forceSetString($columnObj, $value)
449455
{
@@ -456,7 +462,7 @@ public function forceSetString($columnObj, $value)
456462

457463
/**
458464
* @param string $columnName
459-
* @param $value
465+
* @param $value
460466
*
461467
* @return bool
462468
*/
@@ -467,7 +473,7 @@ public function setObjectByName($columnName, $value)
467473

468474
/**
469475
* @param int $columnIndex
470-
* @param $value
476+
* @param $value
471477
*
472478
* @return bool
473479
*/
@@ -478,7 +484,7 @@ public function setObjectByIndex($columnIndex, $value)
478484

479485
/**
480486
* @param Column $columnObj
481-
* @param $value
487+
* @param $value
482488
*
483489
* @return bool
484490
*/
@@ -511,19 +517,23 @@ public function setObject($columnObj, $value)
511517
return false;
512518
}
513519

514-
trigger_error('cannot handle datatype' . $columnObj->getType(), E_USER_ERROR);
520+
trigger_error('cannot handle datatype'.$columnObj->getType(), E_USER_ERROR);
515521
}
516522

517523
/**
518524
* @param Column $columnObj
519-
* @param $value
525+
* @param $value
520526
*
521527
* @return bool
522528
*/
523529
public function setDate($columnObj, $value)
524530
{
525531
if ($columnObj->getType() != self::DBFFIELD_TYPE_DATE) {
526-
trigger_error($columnObj->getName() . ' is not a Date column', E_USER_ERROR);
532+
trigger_error($columnObj->getName().' is not a Date column', E_USER_ERROR);
533+
}
534+
535+
if ($value instanceof \DateTimeInterface) {
536+
$value = $value->format('U');
527537
}
528538

529539
if (strlen($value) == 0) {
@@ -536,14 +546,14 @@ public function setDate($columnObj, $value)
536546

537547
/**
538548
* @param Column $columnObj
539-
* @param $value
549+
* @param $value
540550
*
541551
* @return bool
542552
*/
543553
public function setDateTime($columnObj, $value)
544554
{
545555
if ($columnObj->getType() != self::DBFFIELD_TYPE_DATETIME) {
546-
trigger_error($columnObj->getName() . ' is not a DateTime column', E_USER_ERROR);
556+
trigger_error($columnObj->getName().' is not a DateTime column', E_USER_ERROR);
547557
}
548558

549559
if (strlen($value) == 0) {
@@ -555,19 +565,19 @@ public function setDateTime($columnObj, $value)
555565
$d = $this->zerodate + (mktime(0, 0, 0, $a['mon'], $a['mday'], $a['year']) / 86400);
556566
$d = pack('i', $d);
557567
$t = pack('i', mktime($a['hours'], $a['minutes'], $a['seconds'], 0, 0, 0));
558-
$this->choppedData[$columnObj->getColIndex()] = $d . $t;
568+
$this->choppedData[$columnObj->getColIndex()] = $d.$t;
559569
}
560570

561571
/**
562572
* @param Column $columnObj
563-
* @param $value
573+
* @param $value
564574
*
565575
* @return bool
566576
*/
567577
public function setBoolean($columnObj, $value)
568578
{
569579
if ($columnObj->getType() != self::DBFFIELD_TYPE_LOGICAL) {
570-
trigger_error($columnObj->getName() . ' is not a DateTime column', E_USER_ERROR);
580+
trigger_error($columnObj->getName().' is not a DateTime column', E_USER_ERROR);
571581
}
572582

573583
switch (strtoupper($value)) {
@@ -590,27 +600,27 @@ public function setBoolean($columnObj, $value)
590600

591601
/**
592602
* @param Column $columnObj
593-
* @param $value
603+
* @param $value
594604
*/
595605
public function setMemo($columnObj, $value)
596606
{
597607
if ($columnObj->getType() != self::DBFFIELD_TYPE_MEMO) {
598-
trigger_error($columnObj->getName() . ' is not a Memo column', E_USER_ERROR);
608+
trigger_error($columnObj->getName().' is not a Memo column', E_USER_ERROR);
599609
}
600610

601611
$this->forceSetString($columnObj, $value);
602612
}
603613

604614
/**
605615
* @param Column $columnObj
606-
* @param $value
616+
* @param $value
607617
*
608618
* @return bool
609619
*/
610620
public function setFloat($columnObj, $value)
611621
{
612622
if ($columnObj->getType() != self::DBFFIELD_TYPE_FLOATING) {
613-
trigger_error($columnObj->getName() . ' is not a Float column', E_USER_ERROR);
623+
trigger_error($columnObj->getName().' is not a Float column', E_USER_ERROR);
614624
}
615625

616626
if (strlen($value) == 0) {
@@ -624,14 +634,14 @@ public function setFloat($columnObj, $value)
624634

625635
/**
626636
* @param Column $columnObj
627-
* @param $value
637+
* @param $value
628638
*
629639
* @return bool
630640
*/
631641
public function setInt($columnObj, $value)
632642
{
633643
if ($columnObj->getType() != self::DBFFIELD_TYPE_NUMERIC) {
634-
trigger_error($columnObj->getName() . ' is not a Number column', E_USER_ERROR);
644+
trigger_error($columnObj->getName().' is not a Number column', E_USER_ERROR);
635645
}
636646

637647
if (strlen($value) == 0) {
@@ -648,7 +658,7 @@ public function setInt($columnObj, $value)
648658
*/
649659
public function serializeRawData()
650660
{
651-
return ($this->deleted ? '*' : ' ') . implode('', $this->choppedData);
661+
return ($this->deleted ? '*' : ' ').implode('', $this->choppedData);
652662
}
653663

654664
/**
@@ -658,7 +668,7 @@ public function serializeRawData()
658668
*/
659669
public function getData()
660670
{
661-
$fields = array();
671+
$fields = [];
662672

663673
foreach ($this->getColumns() as $column) {
664674
$fields[$column->name] = $this->getObject($column);
@@ -674,7 +684,7 @@ public function getData()
674684
*/
675685
public function getChoppedData()
676686
{
677-
$fields = array();
687+
$fields = [];
678688

679689
foreach ($this->choppedData as $columnName => $columnValue) {
680690
$fields[$columnName] = $this->forceGetString($columnName);

0 commit comments

Comments
 (0)