php-dataset/src/Dataset.php

80 lines
1.9 KiB
PHP

<?php
namespace NoccyLabs\Dataset;
use NoccyLabs\Dataset\Readers\CsvReader;
use NoccyLabs\Dataset\Readers\JsonReader;
class Dataset
{
protected string $packageName;
protected string $datasetName;
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;
[$this->packageName, $this->datasetName] = explode("#", $identifier, 2);
}
public function getIdentifier(): string
{
return $this->identifier;
}
public function getPackageName(): string
{
return $this->packageName;
}
public function getDatasetName(): string
{
return $this->datasetName;
}
public function getVersion(): ?string
{
return $this->version;
}
public function getComment(): ?string
{
return array_key_exists('comment', $this->options) ? $this->options['comment'] : null;
}
public function getLicense(): ?string
{
return array_key_exists('license', $this->options) ? $this->options['license'] : null;
}
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")
};
}
}