|null Decoded body, once asked for. */ private ?array $decoded = null; public function __construct( private readonly FileExporter $exporter, private readonly string $relative_path, private readonly int $offset, private readonly int $chunk_size, private readonly array $chunk ) { // Left empty: decoding here would bring back the copies this class avoids. parent::__construct( [] ); } /** @return array */ public function get_data() { if ( null !== $this->decoded ) { return $this->data; } $decoded = json_decode( $this->get_body(), true ); $this->data = is_array( $decoded ) ? $decoded : []; $this->decoded = $this->data; return $this->data; } /** @return string JSON. */ public function get_body(): string { return implode( '', iterator_to_array( $this->body_fragments(), false ) ); } /** @return \Generator */ public function body_fragments(): \Generator { yield from $this->exporter->chunk_body_fragments( $this->relative_path, $this->offset, $this->chunk_size, $this->chunk ); } }