2022-09-03 14:39:08 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace NoccyLabs\Dataset\Readers;
|
|
|
|
|
|
|
|
use NoccyLabs\Dataset\ReaderInterface;
|
|
|
|
|
2022-09-03 16:12:22 +02:00
|
|
|
class JsonReader implements ReaderInterface
|
2022-09-03 14:39:08 +02:00
|
|
|
{
|
2022-09-03 15:27:21 +02:00
|
|
|
private array $files = [];
|
|
|
|
|
2022-10-31 00:42:29 +01:00
|
|
|
private array $options = [];
|
|
|
|
|
2022-09-03 15:27:21 +02:00
|
|
|
private int $currentFile = 0;
|
|
|
|
|
|
|
|
private ?int $loadedFile = null;
|
|
|
|
|
|
|
|
private array $data = [];
|
|
|
|
|
|
|
|
private int $currentIndex = 0;
|
|
|
|
|
|
|
|
private int $counter = 0;
|
|
|
|
|
2022-09-03 14:39:08 +02:00
|
|
|
public function __construct(string $filename, array $options)
|
|
|
|
{
|
2022-09-03 15:27:21 +02:00
|
|
|
$this->files = glob($filename);
|
2022-10-31 00:42:29 +01:00
|
|
|
$this->options = $options;
|
2022-09-03 15:27:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
2022-09-03 15:31:28 +02:00
|
|
|
//printf("Reached end of set at slice=%d\n", $this->currentFile);
|
2022-09-03 15:27:21 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-10-31 02:58:34 +01:00
|
|
|
|
|
|
|
$flags = ($this->options['bigintAsString']??false)?JSON_BIGINT_AS_STRING:0;
|
2022-09-03 15:27:21 +02:00
|
|
|
$file = $this->files[$this->currentFile];
|
2022-10-31 02:58:34 +01:00
|
|
|
$json = @json_decode(@file_get_contents($file), true, flags:$flags);
|
2022-09-03 15:27:21 +02:00
|
|
|
|
|
|
|
$this->loadData($json);
|
|
|
|
$this->loadedFile = $this->currentFile;
|
2022-09-03 15:31:28 +02:00
|
|
|
//printf("loaded slice %d: %s\n", $this->currentFile, $file);
|
2022-09-03 15:27:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2022-09-03 15:31:28 +02:00
|
|
|
//printf("Rewinding to slice=%d index=%d\n", $this->currentFile, $this->currentIndex);
|
2022-09-03 15:27:21 +02:00
|
|
|
$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;
|
2022-09-03 15:31:28 +02:00
|
|
|
//printf("Rolling over to slice=%d index=%d counter=%d\n", $this->currentFile, $this->currentIndex, $this->counter);
|
2022-09-03 15:27:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//$this->checkLoadedSlice();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function valid(): bool
|
|
|
|
{
|
|
|
|
$this->checkLoadedSlice();
|
|
|
|
return ($this->currentFile < count($this->files) && ($this->currentIndex < count($this->data)));
|
2022-09-03 14:39:08 +02:00
|
|
|
|
|
|
|
}
|
2022-09-03 15:31:28 +02:00
|
|
|
}
|