php-dataset/src/Dataset.php

46 lines
1.1 KiB
PHP
Raw Normal View History

2022-09-03 00:28:03 +00:00
<?php
namespace NoccyLabs\Dataset;
2022-09-03 12:39:08 +00:00
use NoccyLabs\Dataset\Readers\CsvReader;
use NoccyLabs\Dataset\Readers\JsonReader;
2022-09-03 00:28:03 +00:00
class Dataset
{
2022-09-03 12:11:20 +00:00
protected string $identifier;
protected array $options;
2022-09-03 00:28:03 +00:00
2022-09-03 12:11:20 +00:00
public function __construct(string $identifier, array $options)
2022-09-03 00:28:03 +00:00
{
$this->identifier = $identifier;
2022-09-03 12:11:20 +00:00
$this->options = $options;
2022-09-03 00:28:03 +00:00
}
public function getIdentifier(): string
{
return $this->identifier;
}
2022-09-03 12:39:08 +00:00
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")
};
}
2022-09-03 00:28:03 +00:00
}