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