diff --git a/src/Memo/MemoFactory.php b/src/Memo/MemoFactory.php index c1e6839..e80e083 100644 --- a/src/Memo/MemoFactory.php +++ b/src/Memo/MemoFactory.php @@ -5,9 +5,12 @@ use XBase\DataConverter\Encoder\EncoderInterface; use XBase\Enum\TableType; use XBase\Table\Table; +use XBase\Traits\FilepathTrait; class MemoFactory { + use FilepathTrait; + public static function create(Table $table, EncoderInterface $encoder): ?MemoInterface { $class = self::getClass($table->getVersion()); @@ -18,17 +21,15 @@ public static function create(Table $table, EncoderInterface $encoder): ?MemoInt $memoExt = $refClass->getMethod('getExtension')->invoke(null); $fileInfo = pathinfo($table->filepath); - // if file extension in UPPERCASE then memo file extension should be in upper case too - $memoExt = 'DBF' === ($fileInfo['extension'] ?? null) ? strtoupper($memoExt) : $memoExt; if ('.' !== substr($memoExt, 0, 1)) { $memoExt = '.'.$memoExt; } - $memoFilepath = $fileInfo['dirname'].DIRECTORY_SEPARATOR.$fileInfo['filename'].$memoExt; - if (!file_exists($memoFilepath)) { + $memoFilePath = $fileInfo['dirname'] . DIRECTORY_SEPARATOR . $fileInfo['filename'] . $memoExt; + if (false === $memoFilePath = self::resolveFilepath($memoFilePath)) { return null; //todo create file? } - return $refClass->newInstance($table, $memoFilepath, $encoder); + return $refClass->newInstance($table, $memoFilePath, $encoder); } private static function getClass(int $version): string diff --git a/src/TableReader.php b/src/TableReader.php index 71023e3..c379983 100644 --- a/src/TableReader.php +++ b/src/TableReader.php @@ -18,6 +18,7 @@ use XBase\Stream\Stream; use XBase\Table\Table; use XBase\Table\TableAwareTrait; +use XBase\Traits\FilepathTrait; /** * @author Alexander Strizhak @@ -25,6 +26,7 @@ class TableReader { use TableAwareTrait; + use FilepathTrait; /** @var int Current record position. */ protected $recordPos = -1; @@ -86,10 +88,14 @@ protected function resolveOptions(array $options): array protected function open(): void { - if (!file_exists($this->getFilepath())) { + if (false === $filepath = self::resolveFilepath($this->getFilepath())) { throw new \Exception(sprintf('File %s cannot be found', $this->getFilepath())); } + if ($filepath !== $this->table->filepath) { + $this->table->filepath = $filepath; + } + if ($this->table->stream) { $this->table->stream->close(); } diff --git a/src/Traits/FilepathTrait.php b/src/Traits/FilepathTrait.php new file mode 100644 index 0000000..c766b74 --- /dev/null +++ b/src/Traits/FilepathTrait.php @@ -0,0 +1,25 @@ +