php-dataset/src/Readers/JsonReader.php

97 lines
2.5 KiB
PHP

<?php
namespace NoccyLabs\Dataset\Readers;
use NoccyLabs\Dataset\ReaderInterface;
class JsonReader implements ReaderInterface
{
private array $files = [];
private array $options = [];
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);
$this->options = $options;
}
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;
}
$flags = ($this->options['bigintAsString']??false)?JSON_BIGINT_AS_STRING:0;
$file = $this->files[$this->currentFile];
$json = @json_decode(@file_get_contents($file), true, 512, $flags);
$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()
{
//$this->checkLoadedSlice();
return $this->counter;
}
public function current()
{
//$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)));
}
}