Reader mockup

This commit is contained in:
2022-09-03 14:39:08 +02:00
parent 0542c7e844
commit 65e8f39da4
5 changed files with 66 additions and 0 deletions

View File

@ -2,6 +2,8 @@
namespace NoccyLabs\Dataset;
use NoccyLabs\Dataset\Readers\CsvReader;
use NoccyLabs\Dataset\Readers\JsonReader;
class Dataset
{
@ -20,5 +22,25 @@ class Dataset
return $this->identifier;
}
public function open(): ReaderInterface
{
$filename = $this->options['filename'];
$reader = $this->determineReaderForFile($filename);
$inst = new $reader($filename, $this->options);
return $inst;
}
private function determineReaderForFile(string $filename): string
{
if ($reader = $this->options['reader']??null) {
return $reader;
}
$ext = pathinfo($filename, PATHINFO_EXTENSION);
return match ($ext) {
'json' => JsonReader::class,
'csv' => CsvReader::class,
default => throw new \RuntimeException("Unable to determine reader for dataset file")
};
}
}