php-dataset/src/Dataset.php

54 lines
1.3 KiB
PHP

<?php
namespace NoccyLabs\Dataset;
use NoccyLabs\Dataset\Readers\CsvReader;
use NoccyLabs\Dataset\Readers\JsonReader;
class Dataset
{
protected string $identifier;
protected array $options;
protected ?string $version;
public function __construct(string $identifier, array $options, ?string $version=null)
{
$this->identifier = $identifier;
$this->options = $options;
$this->version = $version;
}
public function getIdentifier(): string
{
return $this->identifier;
}
public function getVersion(): ?string
{
return $this->version;
}
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")
};
}
}