Skip to content
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
11 changes: 6 additions & 5 deletions src/Memo/MemoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/TableReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
use XBase\Stream\Stream;
use XBase\Table\Table;
use XBase\Table\TableAwareTrait;
use XBase\Traits\FilepathTrait;

/**
* @author Alexander Strizhak <gam6itko@gmail.com>
*/
class TableReader
{
use TableAwareTrait;
use FilepathTrait;

/** @var int Current record position. */
protected $recordPos = -1;
Expand Down Expand Up @@ -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();
}
Expand Down
25 changes: 25 additions & 0 deletions src/Traits/FilepathTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

namespace XBase\Traits;

trait FilepathTrait
{
/**
* Check and return case insensitive file path if available
*/
protected static function resolveFilepath(string $filepath)
{
if (false !== file_exists($filepath)) {
return $filepath;
}

$lowerpath = strtolower($filepath);
foreach (glob(dirname($filepath).DIRECTORY_SEPARATOR.'*') as $file) {
if (strtolower($file) === $lowerpath) {
return $file;
}
}

return false;
}
}
Loading