-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MemoFactory for different Memo types;
VisualFoxproTest; add FieldTypes [GENERAL, BLOB, CURRENCY, VAR_FIELD, VARBINARY]
- Loading branch information
Showing
31 changed files
with
971 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ | |
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "1.1.x-dev" | ||
"dev-master": "1.2.x-dev" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace XBase\Memo; | ||
|
||
abstract class AbstractMemo implements MemoInterface | ||
{ | ||
/** @var resource */ | ||
protected $fp; | ||
/** @var string */ | ||
protected $filepath; | ||
/** @var string */ | ||
protected $convertFrom; | ||
|
||
/** | ||
* Memo constructor. | ||
* | ||
* @param string $filepath | ||
* @param string $convertFrom | ||
*/ | ||
public function __construct($filepath, $convertFrom = null) | ||
{ | ||
$this->filepath = $filepath; | ||
$this->convertFrom = $convertFrom; //todo autodetect from languageCode | ||
$this->open(); | ||
$this->readHeader(); | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
$this->close(); | ||
} | ||
|
||
protected function readHeader() | ||
{ | ||
|
||
} | ||
|
||
public static function getExtension() | ||
{ | ||
return 'dbt'; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function isOpen() | ||
{ | ||
return null !== $this->fp; | ||
} | ||
|
||
public function open() | ||
{ | ||
$this->close(); | ||
$this->fp = fopen($this->filepath, 'rb'); | ||
} | ||
|
||
public function close() | ||
{ | ||
if (null !== $this->fp) { | ||
fclose($this->fp); | ||
} | ||
$this->fp = null; | ||
} | ||
|
||
protected function guessDataType(string $result) | ||
{ | ||
if (strlen($result) > 4) { | ||
$bytes = unpack('n*', substr($result, 0, 4)); | ||
switch ($bytes[1]) { | ||
case 0x4D42: //BMP | ||
case 0xFFD8: //JPEG | ||
return MemoObject::TYPE_IMAGE; | ||
case 0x8950: //PNG | ||
if (0x4E47 === $bytes[2]) { | ||
return MemoObject::TYPE_IMAGE; | ||
} | ||
break; | ||
} | ||
} | ||
|
||
return MemoObject::TYPE_TEXT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace XBase\Memo; | ||
|
||
class DBase3Memo extends AbstractMemo | ||
{ | ||
const BLOCK_LENGTH = 512; | ||
|
||
public static function getExtension() | ||
{ | ||
return 'dbt'; | ||
} | ||
|
||
public function get($pointer) | ||
{ | ||
if (!$this->isOpen()) { | ||
$this->open(); | ||
} | ||
|
||
fseek($this->fp, $pointer * self::BLOCK_LENGTH); | ||
|
||
$endMarker = chr(0x1A).chr(0x1A).chr(0x00); | ||
$result = ''; | ||
while (!feof($this->fp)) { | ||
$result .= fread($this->fp, 1); | ||
|
||
$substr = substr($result, -3); | ||
if ($endMarker === $substr) { | ||
$result = substr($result, 0, -3); | ||
break; | ||
} | ||
} | ||
|
||
$type = $this->guessDataType($result); | ||
if (MemoObject::TYPE_TEXT === $type && chr(0x00) === substr($result, -1)) { | ||
$result = substr($result, 0, -1); // remove endline symbol (0x00) | ||
} | ||
|
||
if (MemoObject::TYPE_TEXT === $type && $this->convertFrom) { | ||
$result = iconv($this->convertFrom, 'utf-8', $result); | ||
} | ||
|
||
return new MemoObject($type, $result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace XBase\Memo; | ||
|
||
class DBase4Memo extends DBase3Memo | ||
{ | ||
const BLOCK_SIGN = 0xFFFF0800; | ||
const BLOCK_SIGN_LENGTH = 4; | ||
const BLOCK_LENGTH_LENGTH = 4; | ||
|
||
/** @var int */ | ||
protected $blockSize; | ||
/** @var int */ | ||
protected $blockLength; | ||
|
||
protected function readHeader() | ||
{ | ||
fseek($this->fp, 4); | ||
$bytes = unpack("N", fread($this->fp, 4)); | ||
$this->blockSize = $bytes[1]; | ||
|
||
fseek($this->fp, 20); | ||
$bytes = unpack("S", fread($this->fp, 2)); | ||
$this->blockLength = $bytes[1]; | ||
} | ||
|
||
public function get($pointer) | ||
{ | ||
if (!$this->isOpen()) { | ||
$this->open(); | ||
} | ||
|
||
fseek($this->fp, $pointer * $this->blockLength); | ||
$sign = unpack("N", fread($this->fp, self::BLOCK_SIGN_LENGTH)); | ||
if ($sign[1] !== self::BLOCK_SIGN) { | ||
throw new \LogicException('Wrong dBaseIV block sign/'); | ||
} | ||
|
||
$memoLength = unpack("L", fread($this->fp, self::BLOCK_LENGTH_LENGTH)); | ||
$result = fread($this->fp, $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); | ||
} | ||
|
||
return new MemoObject($type, $result); | ||
} | ||
} |
Oops, something went wrong.