126 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			126 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace NoccyLabs\Dataset;
 | |
| 
 | |
| use Iterator;
 | |
| 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;
 | |
| 
 | |
|     /**
 | |
|      * 
 | |
|      * 
 | |
|      * @param string $identifier The identifier for the dataset (vendor/package#dataset)
 | |
|      * @param array $options Configured options for the dataset
 | |
|      * @param string|null $version The package version
 | |
|      */
 | |
|     public function __construct(string $identifier, array $options, ?string $version=null)
 | |
|     {
 | |
|         $this->identifier = $identifier;
 | |
|         $this->options = $options;
 | |
|         $this->version = $version??"0.0.0.0";
 | |
| 
 | |
|         [$this->packageName, $this->datasetName] = explode("#", $identifier, 2);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 
 | |
|      * @return string
 | |
|      */
 | |
|     public function getIdentifier(): string
 | |
|     {
 | |
|         return $this->identifier;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 
 | |
|      * @return string
 | |
|      */
 | |
|     public function getPackageName(): string
 | |
|     {
 | |
|         return $this->packageName;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 
 | |
|      * @return string
 | |
|      */
 | |
|     public function getDatasetName(): string
 | |
|     {
 | |
|         return $this->datasetName;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 
 | |
|      * @return string
 | |
|      */
 | |
|     public function getVersion(): string
 | |
|     {
 | |
|         return $this->version;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 
 | |
|      * @return string|null
 | |
|      */
 | |
|     public function getComment(): ?string
 | |
|     {
 | |
|         return array_key_exists('comment', $this->options) ? $this->options['comment'] : null;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 
 | |
|      * @return string|null
 | |
|      */
 | |
|     public function getLicense(): ?string
 | |
|     {
 | |
|         return array_key_exists('license', $this->options) ? $this->options['license'] : null;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 
 | |
|      * @return Iterator
 | |
|      */
 | |
|     public function open(): Iterator
 | |
|     {
 | |
|         $filename = $this->options['filename'];
 | |
|         $reader = $this->determineReaderForFile($filename);
 | |
|         $inst = new $reader($filename, $this->options);
 | |
|         return $inst;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 
 | |
|      * @param string $filename
 | |
|      * @return string
 | |
|      */
 | |
|     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")
 | |
|         };
 | |
|     }
 | |
|     
 | |
| } |