class-basebytereadstream.php
1 week ago
class-deflatereadstream.php
1 week ago
class-filereadstream.php
1 week ago
class-inflatereadstream.php
1 week ago
class-limitedbytereadstream.php
1 week ago
class-transformedreadstream.php
1 week ago
interface-bytereadstream.php
1 week ago
class-transformedreadstream.php
125 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\ByteStream\ReadStream; |
| 4 | |
| 5 | use ArrayAccess; |
| 6 | use ReturnTypeWillChange; |
| 7 | use WordPress\ByteStream\ByteStreamException; |
| 8 | use WordPress\ByteStream\ByteTransformer\ByteTransformer; |
| 9 | |
| 10 | class TransformedReadStream extends BaseByteReadStream implements ArrayAccess { |
| 11 | |
| 12 | /** |
| 13 | * @var ByteReadStream |
| 14 | */ |
| 15 | private $reader; |
| 16 | |
| 17 | /** |
| 18 | * @var ByteTransformer[] |
| 19 | */ |
| 20 | private $filters = array(); |
| 21 | |
| 22 | private $filters_flushed = false; |
| 23 | |
| 24 | public function __construct( ByteReadStream $reader, array $filters = array() ) { |
| 25 | $this->reader = $reader; |
| 26 | $this->filters = $filters; |
| 27 | } |
| 28 | |
| 29 | public function get_upstream_reader(): ByteReadStream { |
| 30 | return $this->reader; |
| 31 | } |
| 32 | |
| 33 | protected function internal_pull( $max_bytes ): string { |
| 34 | $bytes_pulled = $this->reader->pull( $max_bytes ); |
| 35 | if ( 0 === $bytes_pulled ) { |
| 36 | if ( $this->reader->reached_end_of_data() && ! $this->filters_flushed ) { |
| 37 | return $this->flush_filters(); |
| 38 | } |
| 39 | |
| 40 | return ''; |
| 41 | } |
| 42 | |
| 43 | $chunk = $this->reader->consume( $bytes_pulled ); |
| 44 | foreach ( $this->filters as $filter ) { |
| 45 | $chunk = $filter->filter_bytes( $chunk ); |
| 46 | if ( false === $chunk ) { |
| 47 | return ''; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | return $chunk; |
| 52 | } |
| 53 | |
| 54 | private function flush_filters(): string { |
| 55 | $this->filters_flushed = true; |
| 56 | |
| 57 | $flush = ''; |
| 58 | foreach ( $this->filters as $filter ) { |
| 59 | $flush = $filter->filter_bytes( $flush ); |
| 60 | $flush .= $filter->flush(); |
| 61 | } |
| 62 | |
| 63 | return $flush; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * |
| 68 | * @param string $offset The offset to get. |
| 69 | * @throws ByteStreamException If the filter is not found. |
| 70 | */ |
| 71 | #[ReturnTypeWillChange] |
| 72 | public function offsetGet( $offset ) { |
| 73 | if ( ! isset( $this->filters[ $offset ] ) ) { |
| 74 | throw new ByteStreamException( esc_html( sprintf( 'Filter %s not found', $offset ) ) ); |
| 75 | } |
| 76 | |
| 77 | return $this->filters[ $offset ]; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * |
| 82 | * @param string $offset The offset to check. |
| 83 | * @return bool Whether the filter exists. |
| 84 | */ |
| 85 | #[ReturnTypeWillChange] |
| 86 | public function offsetExists( $offset ) { |
| 87 | return isset( $this->filters[ $offset ] ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @param string $offset The offset to set. |
| 92 | * @param ByteTransformer $value The filter to set. |
| 93 | * @throws ByteStreamException If the filters are immutable. |
| 94 | */ |
| 95 | #[ReturnTypeWillChange] |
| 96 | public function offsetSet( $offset, $value ) { |
| 97 | throw new ByteStreamException( 'Filters are immutable' ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @param string $offset The offset to unset. |
| 102 | * @throws ByteStreamException If the filters are immutable. |
| 103 | */ |
| 104 | #[ReturnTypeWillChange] |
| 105 | public function offsetUnset( $offset ) { |
| 106 | throw new ByteStreamException( 'Filters are immutable' ); |
| 107 | } |
| 108 | |
| 109 | public function length(): ?int { |
| 110 | return null; |
| 111 | } |
| 112 | |
| 113 | protected function internal_reached_end_of_data(): bool { |
| 114 | return $this->filters_flushed; |
| 115 | } |
| 116 | |
| 117 | protected function seek_outside_of_buffer( int $target_offset ): void { |
| 118 | throw new ByteStreamException( 'Seek is not supported on TransformedProducer' ); |
| 119 | } |
| 120 | |
| 121 | protected function internal_close_reading(): void { |
| 122 | $this->filters_flushed = true; |
| 123 | } |
| 124 | } |
| 125 |