Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed tests to work with windows file separator too using OS agnostic… #23

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function getField($fieldName) {

/**
* @param \org\majkel\dbase\Field $field
* @return $this
* @return \org\majkel\dbase\Builder
* @throws \org\majkel\dbase\Exception
*/
public function addField(Field $field) {
Expand All @@ -60,7 +60,18 @@ public function addField(Field $field) {

/**
* @param $fieldName
* @return $this
* @param $newFieldName
* @return \org\majkel\dbase\Builder
* @throws \org\majkel\dbase\Exception
*/
public function renameField($fieldName, $newFieldName) {
$this->getHeader()->renameField($fieldName, $newFieldName);
return $this;
}

/**
* @param $fieldName
* @return \org\majkel\dbase\Builder
* @throws \org\majkel\dbase\Exception
*/
public function removeField($fieldName) {
Expand All @@ -83,7 +94,7 @@ public static function fromTable(Table $table) {
/**
* @param string $filePath
* @return \org\majkel\dbase\Builder
* @throws Exception
* @throws \org\majkel\dbase\Exception
*/
public static function fromFile($filePath) {
return self::fromTable(Table::fromFile($filePath, Table::MODE_READ));
Expand Down Expand Up @@ -111,7 +122,7 @@ public function setFormatType($formatType) {

/**
* @param string $memoType
* @return Builder
* @return \org\majkel\dbase\Builder
*/
public function setMemoType($memoType) {
$this->memoType = $memoType;
Expand All @@ -120,8 +131,8 @@ public function setMemoType($memoType) {

/**
* @param string $filePath
* @return Table
* @throws Exception
* @return \org\majkel\dbase\Table
* @throws \org\majkel\dbase\Exception
*/
public function build($filePath) {
$header = $this->getHeader();
Expand Down
20 changes: 12 additions & 8 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ abstract class Field {
const TYPE_DATE = 'D';
const TYPE_NUMERIC = 'N';
const TYPE_MEMO = 'M';
const TYPE_FLOAT = 'F';

const MAX_NAME_LENGTH = 10;

Expand Down Expand Up @@ -108,7 +109,7 @@ public function getName() {
}

/**
* Sets filed name
* Sets field name
*
* @param string $name
*
Expand All @@ -124,15 +125,15 @@ public function setName($name) {
}

/**
* Returns filed length
* Returns field length
* @return integer
*/
public function getLength() {
return $this->length;
}

/**
* Sets filed length
* Sets field length
* @param integer $length
* @return \org\majkel\dbase\Field
*/
Expand All @@ -142,15 +143,15 @@ public function setLength($length) {
}

/**
* Returns filed decimalCount
* Returns field decimalCount
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out the typos

* @return integer
*/
public function getDecimalCount() {
return $this->decimalCount;
}

/**
* Sets filed decimalCount
* Sets field decimalCount
* @param integer $decimalCount
* @return \org\majkel\dbase\Field
*/
Expand All @@ -170,7 +171,7 @@ public function setLoad($load) {
}

/**
* Determines whether to load filed value
* Determines whether to load field value
* @return boolean;
*/
public function isLoad() {
Expand Down Expand Up @@ -232,7 +233,7 @@ public function isMemoEntry() {
}

/**
* Constructs Filed based on type
* Constructs field based on type
* @param string $type
* @return \org\majkel\dbase\Field
* @throws Exception
Expand All @@ -249,6 +250,8 @@ public static function create($type) {
return new field\MemoField;
case Field::TYPE_NUMERIC:
return new field\NumericField;
case Field::TYPE_FLOAT:
return new field\FloatField;
default:
throw new Exception("Unsupported field `$type`");
}
Expand All @@ -262,9 +265,10 @@ public static function getTypes() {
return array(
self::TYPE_CHARACTER,
self::TYPE_LOGICAL,
self::TYPE_DATE ,
self::TYPE_DATE,
self::TYPE_NUMERIC,
self::TYPE_MEMO,
self::TYPE_FLOAT,
);
}
}
6 changes: 4 additions & 2 deletions src/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ abstract class Format {

const AUTO = 'auto';
const DBASE3 = 'dbase3';
const DBASE4 = 'dbase4';
const FOXPRO = 'foxpro';

const NAME = 'Abstract DBase Format';
Expand Down Expand Up @@ -490,9 +491,9 @@ protected function readHeader() {
* @param integer $day
* @return DateTime
*/
protected function getLastDate($year, $moth, $day) {
protected function getLastDate($year, $month, $day) {
$date = new DateTime;
$date->setDate($year + 1900, $moth, $day);
$date->setDate($year + 1900, $month, $day);
$date->setTime(0, 0, 0);
return $date;
}
Expand Down Expand Up @@ -564,6 +565,7 @@ protected function createRecord($data) {
public static function getSupportedFormats() {
return array(
Format::DBASE3,
Format::DBASE4,
Format::FOXPRO,
);
}
Expand Down
3 changes: 3 additions & 0 deletions src/FormatFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public function initializeFormats() {
$this->registerFormat(Format::DBASE3, function ($filePath, $mode) {
return new format\DBase3($filePath, $mode);
});
$this->registerFormat(Format::DBASE4, function ($filePath, $mode) {
return new format\DBase4($filePath, $mode);
});
$this->registerFormat(Format::FOXPRO, function ($filePath, $mode) {
return new format\FoxPro($filePath, $mode);
});
Expand Down
14 changes: 14 additions & 0 deletions src/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ public function addField(Field $field) {
$this->fields[$field->getName()] = $field;
return $this;
}

/**
* @param \org\majkel\dbase\Field $field
* @param \org\majkel\dbase\$newName
* @return \org\majkel\dbase\Header
* @throws \org\majkel\dbase\Exception
*/
public function renameField($fieldName, $newFieldName) {
if ($this->isFieldsLocked()) {
throw new Exception("Header is locked. Use TableBuilder to construct new table with new definition.");
}
$this->fields[$fieldName]->setName($newFieldName);
return $this;
}

/**
* @param string $name
Expand Down
2 changes: 1 addition & 1 deletion src/field/DateField.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class DateField extends Field {

/**
* CharacterField constructor.
* DateField constructor.
*/
public function __construct() {
$this->length = 8;
Expand Down
58 changes: 58 additions & 0 deletions src/field/FloatField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

namespace org\majkel\dbase\field;

use \org\majkel\dbase\Field;

/**
* Description of Field
*
* @author stokescomp
*/
class FloatField extends Field {

/**
* FloatField constructor.
*/
public function __construct() {
$this->length = 16;
$this->decimalCount = 14;
}

/**
* {@inheritdoc}
*/
public function toData($value) {
if (is_null($value)) {
return "";
} else {
$value = number_format((float) $value, $this->getDecimalCount(), '.', '');
return substr(strval($value), 0, $this->getLength());
}
}

/**
* {@inheritdoc}
*/
public function fromData($data) {
if (!$this->getDecimalCount()) {
return (integer)substr($data, 0, $this->getLength());
} else {
return (float)number_format(substr($data, 0, $this->getLength()), $this->getDecimalCount(), '.', '');
}
}

/**
* {@inheritdoc}
*/
public function getType() {
return Field::TYPE_FLOAT;
}

}
2 changes: 1 addition & 1 deletion src/field/LogicalField.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class LogicalField extends Field {

/**
* CharacterField constructor.
* LogicalField constructor.
*/
public function __construct() {
$this->length = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/field/MemoField.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MemoField extends Field {
const CHAR_MASK = "\x00\x\x1A\x04\x02";

/**
* CharacterField constructor.
* MemoField constructor.
*/
public function __construct() {
$this->length = 10;
Expand Down
2 changes: 1 addition & 1 deletion src/field/NumericField.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class NumericField extends Field {

/**
* CharacterField constructor.
* NumericField constructor.
*/
public function __construct() {
$this->length = 10;
Expand Down
2 changes: 1 addition & 1 deletion src/format/DBase3.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function createHeader($data) {
*/
public function supportsType($type) {
return in_array($type, array(Field::TYPE_CHARACTER, Field::TYPE_DATE,
Field::TYPE_LOGICAL, Field::TYPE_MEMO, Field::TYPE_NUMERIC));
Field::TYPE_LOGICAL, Field::TYPE_MEMO, Field::TYPE_NUMERIC, Field::TYPE_FLOAT));
}

/**
Expand Down
52 changes: 52 additions & 0 deletions src/format/DBase4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

namespace org\majkel\dbase\format;

use \org\majkel\dbase\Format;
use org\majkel\dbase\Field;

/**
* Description of dBase4
*
* @author stokescomp
*/
class DBase4 extends Format {

const NAME = 'dBASE IV';

/**
* {@inheritdoc}
*/
protected function createHeader($data) {
$header = parent::createHeader($data);
return $header->setValid($header->getVersion() & $this->getVersion());
}

/**
* {@inheritdoc}
*/
public function supportsType($type) {
return in_array($type, array(Field::TYPE_CHARACTER, Field::TYPE_DATE,
Field::TYPE_LOGICAL, Field::TYPE_MEMO, Field::TYPE_NUMERIC, Field::TYPE_FLOAT));
}

/**
* @return string
*/
public function getType() {
return Format::DBASE4;
}

/**
* @return integer
*/
protected function getVersion() {
return 4;
}
}
2 changes: 1 addition & 1 deletion src/format/FoxPro.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function createHeader($data) {
*/
public function supportsType($type) {
return in_array($type, array(Field::TYPE_CHARACTER, Field::TYPE_DATE,
Field::TYPE_LOGICAL, Field::TYPE_MEMO, Field::TYPE_NUMERIC));
Field::TYPE_LOGICAL, Field::TYPE_MEMO, Field::TYPE_NUMERIC, Field::TYPE_FLOAT));
}

/**
Expand Down
Binary file modified tests/fixtures/dBase3.dbf
Binary file not shown.
Binary file modified tests/fixtures/dBase3.dbt
Binary file not shown.
Binary file added tests/fixtures/dBase4.dbf
Binary file not shown.
Binary file added tests/fixtures/dbase4.dbt
Binary file not shown.
Binary file added tests/fixtures/simple4.dbf
Binary file not shown.
Binary file added tests/fixtures/visualFoxpro.dbf
Binary file not shown.
Binary file added tests/fixtures/visualFoxpro.fpt
Binary file not shown.
18 changes: 18 additions & 0 deletions tests/integration/DBase3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ public function testReadDbase3() {
self::assertSame('memo1', $record->MEMO);
}

/**
* @medium
* @coversNothing
* @throws Exception
*/
public function testReadVisualFoxpro() {
$dbf = Table::fromFile('tests/fixtures/visualFoxpro.dbf');
self::assertSame($dbf->getRecordsCount(), 6);
$record = $dbf->getRecord(0);
self::assertSame('4', $record->SL_CHPODPL);
self::assertSame('Bezp', $record->CHP_ODPLAT);
self::assertSame(22, $record->NUM);
self::assertSame('2015-06-26', $record->DAT->format('Y-m-d'));
self::assertSame(true, $record['LOGIC']);
self::assertSame(1.123456789, $record->FLOAT);
self::assertSame('memo1', $record->MEMO);
}

/**
* @medium
* @coversNothing
Expand Down
Loading