MediaWiki REL1_37
StringStream.php
Go to the documentation of this file.
1<?php
2
3namespace 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}
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.