From 365050026f9aced03ac6a6264daaf60398d9cd1c Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Sat, 3 Sep 2022 15:27:21 +0200 Subject: [PATCH] Implement JsonReader --- src/DatasetManager.php | 1 + src/Readers/JsonReader.php | 81 +++++++++++++++++++++++++++++++++++++- 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/DatasetManager.php b/src/DatasetManager.php index 881cb2c..bd64c7d 100644 --- a/src/DatasetManager.php +++ b/src/DatasetManager.php @@ -86,6 +86,7 @@ class DatasetManager $this->loadDatasets($info, ltrim($prefix . "." . $name, "."), $package, $path); return; } + $info['filename'] = $path . "/" . $info['filename']; $pn = sprintf("%s#%s", $package, ltrim($prefix.".".$name,".")); $ds = new Dataset($pn, $info); $this->registerDataset($ds); diff --git a/src/Readers/JsonReader.php b/src/Readers/JsonReader.php index 3e3906c..8cb6f1b 100644 --- a/src/Readers/JsonReader.php +++ b/src/Readers/JsonReader.php @@ -2,12 +2,91 @@ namespace NoccyLabs\Dataset\Readers; +use Iterator; use NoccyLabs\Dataset\ReaderInterface; -class JsonReader implements ReaderInterface +class JsonReader implements ReaderInterface, Iterator { + private array $files = []; + + private int $currentFile = 0; + + private ?int $loadedFile = null; + + private array $data = []; + + private int $currentIndex = 0; + + private int $counter = 0; + public function __construct(string $filename, array $options) { + $this->files = glob($filename); + } + + private function checkLoadedSlice() + { + // If the current file is the loaded file, we're already set + if ($this->currentFile === $this->loadedFile) return; + + if ($this->currentFile >= count($this->files)) { + printf("Reached end of set at slice=%d\n", $this->currentFile); + return; + } + $file = $this->files[$this->currentFile]; + $json = @json_decode(@file_get_contents($file), true); + + $this->loadData($json); + $this->loadedFile = $this->currentFile; + printf("loaded slice %d: %s\n", $this->currentFile, $file); + } + + private function loadData(array $data) + { + // FIXME parse data according to directives if present + $this->data = $data; + $this->currentIndex = 0; + } + + public function rewind(): void + { + $this->currentFile = 0; + $this->currentIndex = 0; + $this->counter = 0; + printf("Rewinding to slice=%d index=%d\n", $this->currentFile, $this->currentIndex); + $this->checkLoadedSlice(); + } + + public function key(): mixed + { + //$this->checkLoadedSlice(); + return $this->counter; + } + + public function current(): mixed + { + //$this->checkLoadedSlice(); + return $this->data[$this->currentIndex]; + } + + public function next(): void + { + $this->counter++; + $this->currentIndex++; + + if ($this->currentIndex >= count($this->data)) { + $this->currentFile++; + $this->currentIndex = 0; + printf("Rolling over to slice=%d index=%d counter=%d\n", $this->currentFile, $this->currentIndex, $this->counter); + } + + //$this->checkLoadedSlice(); + } + + public function valid(): bool + { + $this->checkLoadedSlice(); + return ($this->currentFile < count($this->files) && ($this->currentIndex < count($this->data))); } } \ No newline at end of file