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