Reader mockup
This commit is contained in:
		@@ -2,6 +2,8 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace NoccyLabs\Dataset;
 | 
					namespace NoccyLabs\Dataset;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use NoccyLabs\Dataset\Readers\CsvReader;
 | 
				
			||||||
 | 
					use NoccyLabs\Dataset\Readers\JsonReader;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Dataset
 | 
					class Dataset
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -20,5 +22,25 @@ class Dataset
 | 
				
			|||||||
        return $this->identifier;
 | 
					        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")
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -27,6 +27,16 @@ class DatasetManager
 | 
				
			|||||||
        return self::$datasets;
 | 
					        return self::$datasets;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function getDataset(string $identifier): Dataset
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return self::$dataset[$identifier];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function openDataset(string $identifier): ReaderInterface
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return $this->getDataset($identifier)->open();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Find the vendor directory and try to locate all bundled datasets
 | 
					     * Find the vendor directory and try to locate all bundled datasets
 | 
				
			||||||
     * 
 | 
					     * 
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										8
									
								
								src/ReaderInterface.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/ReaderInterface.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace NoccyLabs\Dataset;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					interface ReaderInterface
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public function __construct(string $filename, array $options);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										13
									
								
								src/Readers/CsvReader.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/Readers/CsvReader.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace NoccyLabs\Dataset\Readers;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use NoccyLabs\Dataset\ReaderInterface;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class CsvReader implements ReaderInterface
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public function __construct(string $filename, array $options)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										13
									
								
								src/Readers/JsonReader.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/Readers/JsonReader.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace NoccyLabs\Dataset\Readers;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use NoccyLabs\Dataset\ReaderInterface;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class JsonReader implements ReaderInterface
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public function __construct(string $filename, array $options)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user