MediaWiki  master
StringStream.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest;
4 
17 
19  private $contents;
21  private $offset = 0;
22 
31  public function __construct( $contents = '' ) {
32  $this->contents = $contents;
33  }
34 
35  public function copyToStream( $stream ) {
36  fwrite( $stream, $this->getContents() );
37  }
38 
39  public function __toString() {
40  return $this->contents;
41  }
42 
43  public function close() {
44  }
45 
46  public function detach() {
47  return null;
48  }
49 
50  public function getSize() {
51  return strlen( $this->contents );
52  }
53 
54  public function tell() {
55  return $this->offset;
56  }
57 
58  public function eof() {
59  return $this->offset >= strlen( $this->contents );
60  }
61 
62  public function isSeekable() {
63  return true;
64  }
65 
66  public function seek( $offset, $whence = SEEK_SET ) {
67  switch ( $whence ) {
68  case SEEK_SET:
69  $this->offset = $offset;
70  break;
71 
72  case SEEK_CUR:
73  $this->offset += $offset;
74  break;
75 
76  case SEEK_END:
77  $this->offset = strlen( $this->contents ) + $offset;
78  break;
79 
80  default:
81  throw new \InvalidArgumentException( "Invalid value for \$whence" );
82  }
83  if ( $this->offset > strlen( $this->contents ) ) {
84  throw new \InvalidArgumentException( "Cannot seek beyond the end of a StringStream" );
85  }
86  if ( $this->offset < 0 ) {
87  throw new \InvalidArgumentException( "Cannot seek before the start of a StringStream" );
88  }
89  }
90 
91  public function rewind() {
92  $this->offset = 0;
93  }
94 
95  public function isWritable() {
96  return true;
97  }
98 
99  public function write( $string ) {
100  if ( $this->offset === strlen( $this->contents ) ) {
101  $this->contents .= $string;
102  } else {
103  $this->contents = substr_replace( $this->contents, $string,
104  $this->offset, strlen( $string ) );
105  }
106  $this->offset += strlen( $string );
107  return strlen( $string );
108  }
109 
110  public function isReadable() {
111  return true;
112  }
113 
114  public function read( $length ) {
115  if ( $this->offset === 0 && $length >= strlen( $this->contents ) ) {
116  $ret = $this->contents;
117  } elseif ( $this->offset >= strlen( $this->contents ) ) {
118  $ret = '';
119  } else {
120  $ret = substr( $this->contents, $this->offset, $length );
121  }
122  $this->offset += strlen( $ret );
123  return $ret;
124  }
125 
126  public function getContents() {
127  if ( $this->offset === 0 ) {
128  $ret = $this->contents;
129  } elseif ( $this->offset >= strlen( $this->contents ) ) {
130  $ret = '';
131  } else {
132  $ret = substr( $this->contents, $this->offset );
133  }
134  $this->offset = strlen( $this->contents );
135  return $ret;
136  }
137 
138  public function getMetadata( $key = null ) {
139  return null;
140  }
141 }
MediaWiki\Rest\StringStream\__construct
__construct( $contents='')
Construct a StringStream with the given contents.
Definition: StringStream.php:31
MediaWiki\Rest\StringStream\seek
seek( $offset, $whence=SEEK_SET)
Definition: StringStream.php:66
MediaWiki\Rest\StringStream\write
write( $string)
Definition: StringStream.php:99
MediaWiki\Rest\StringStream\getMetadata
getMetadata( $key=null)
Definition: StringStream.php:138
MediaWiki\Rest\StringStream\rewind
rewind()
Definition: StringStream.php:91
MediaWiki\Rest\StringStream
A stream class which uses a string as the underlying storage.
Definition: StringStream.php:16
MediaWiki\Rest\StringStream\isReadable
isReadable()
Definition: StringStream.php:110
MediaWiki\Rest\StringStream\$offset
int $offset
Definition: StringStream.php:21
MediaWiki\Rest\CopyableStreamInterface
An interface for a stream with a copyToStream() function.
Definition: CopyableStreamInterface.php:8
MediaWiki\Rest\StringStream\$contents
string $contents
Definition: StringStream.php:19
MediaWiki\Rest\StringStream\detach
detach()
Definition: StringStream.php:46
MediaWiki\Rest\StringStream\isWritable
isWritable()
Definition: StringStream.php:95
MediaWiki\Rest
MediaWiki\Rest\StringStream\close
close()
Definition: StringStream.php:43
MediaWiki\Rest\StringStream\read
read( $length)
Definition: StringStream.php:114
MediaWiki\Rest\StringStream\tell
tell()
Definition: StringStream.php:54
MediaWiki\Rest\StringStream\isSeekable
isSeekable()
Definition: StringStream.php:62
MediaWiki\Rest\StringStream\copyToStream
copyToStream( $stream)
Copy this stream to a specified stream resource.
Definition: StringStream.php:35
MediaWiki\Rest\StringStream\__toString
__toString()
Definition: StringStream.php:39
MediaWiki\Rest\StringStream\eof
eof()
Definition: StringStream.php:58
MediaWiki\Rest\StringStream\getSize
getSize()
Definition: StringStream.php:50
MediaWiki\Rest\StringStream\getContents
getContents()
Definition: StringStream.php:126