MediaWiki master
StringStream.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Rest;
4
5use InvalidArgumentException;
6use Stringable;
7
19class StringStream implements Stringable, CopyableStreamInterface {
20
22 private $contents;
24 private $offset = 0;
25
34 public function __construct( $contents = '' ) {
35 $this->contents = $contents;
36 }
37
39 public function copyToStream( $stream ) {
40 fwrite( $stream, $this->getContents() );
41 }
42
43 public function __toString() {
44 return $this->contents;
45 }
46
47 public function close() {
48 }
49
51 public function detach() {
52 return null;
53 }
54
56 public function getSize() {
57 return strlen( $this->contents );
58 }
59
61 public function tell() {
62 return $this->offset;
63 }
64
66 public function eof() {
67 return $this->offset >= strlen( $this->contents );
68 }
69
71 public function isSeekable() {
72 return true;
73 }
74
76 public function seek( $offset, $whence = SEEK_SET ) {
77 switch ( $whence ) {
78 case SEEK_SET:
79 $this->offset = $offset;
80 break;
81
82 case SEEK_CUR:
83 $this->offset += $offset;
84 break;
85
86 case SEEK_END:
87 $this->offset = strlen( $this->contents ) + $offset;
88 break;
89
90 default:
91 throw new InvalidArgumentException( "Invalid value for \$whence" );
92 }
93 if ( $this->offset > strlen( $this->contents ) ) {
94 throw new InvalidArgumentException( "Cannot seek beyond the end of a StringStream" );
95 }
96 if ( $this->offset < 0 ) {
97 throw new InvalidArgumentException( "Cannot seek before the start of a StringStream" );
98 }
99 }
100
102 public function rewind() {
103 $this->offset = 0;
104 }
105
107 public function isWritable() {
108 return true;
109 }
110
112 public function write( $string ) {
113 if ( $this->offset === strlen( $this->contents ) ) {
114 $this->contents .= $string;
115 } else {
116 $this->contents = substr_replace( $this->contents, $string,
117 $this->offset, strlen( $string ) );
118 }
119 $this->offset += strlen( $string );
120 return strlen( $string );
121 }
122
124 public function isReadable() {
125 return true;
126 }
127
129 public function read( $length ) {
130 if ( $this->offset === 0 && $length >= strlen( $this->contents ) ) {
131 $ret = $this->contents;
132 } elseif ( $this->offset >= strlen( $this->contents ) ) {
133 $ret = '';
134 } else {
135 $ret = substr( $this->contents, $this->offset, $length );
136 }
137 $this->offset += strlen( $ret );
138 return $ret;
139 }
140
142 public function getContents() {
143 if ( $this->offset === 0 ) {
144 $ret = $this->contents;
145 } elseif ( $this->offset >= strlen( $this->contents ) ) {
146 $ret = '';
147 } else {
148 $ret = substr( $this->contents, $this->offset );
149 }
150 $this->offset = strlen( $this->contents );
151 return $ret;
152 }
153
155 public function getMetadata( $key = null ) {
156 return null;
157 }
158}
A stream class which uses a string as the underlying storage.
seek( $offset, $whence=SEEK_SET)
copyToStream( $stream)
Copy this stream to a specified stream resource.For some streams, this can be implemented without a t...
__construct( $contents='')
Construct a StringStream with the given contents.
An interface for a stream with a copyToStream() function.