*/ 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( '', $this->fragments ); } /** @return \Generator JSON fragments, in order. */ public function stream_body(): \Generator { foreach ( $this->fragments as $fragment ) { // One large file can be a single fragment, so it is subdivided. $length = strlen( $fragment ); for ( $offset = 0; $offset < $length; $offset += self::FRAGMENT_BYTES ) { $take = substr( $fragment, $offset, self::FRAGMENT_BYTES ); yield $take; } unset( $fragment ); } } /** Large enough that flushing isn't the bottleneck, small enough to stay a minor share of the memory limit. */ private const FRAGMENT_BYTES = 262_144; }