Hoa central
ReadWrite.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Stringbuffer;
38 
39 use Hoa\Stream;
40 
49 class ReadWrite
50  extends Stringbuffer
51  implements Stream\IStream\In,
53 {
59  public function eof()
60  {
61  return feof($this->getStream());
62  }
63 
71  public function read($length)
72  {
73  if (0 > $length) {
74  throw new Exception(
75  'Length must be greater than 0, given %d.',
76  0,
77  $length
78  );
79  }
80 
81  return fread($this->getStream(), $length);
82  }
83 
90  public function readString($length)
91  {
92  return $this->read($length);
93  }
94 
100  public function readCharacter()
101  {
102  return fgetc($this->getStream());
103  }
104 
110  public function readBoolean()
111  {
112  return (bool) $this->read(1);
113  }
114 
121  public function readInteger($length = 1)
122  {
123  return (int) $this->read($length);
124  }
125 
132  public function readFloat($length = 1)
133  {
134  return (float) $this->read($length);
135  }
136 
144  public function readArray($format = null)
145  {
146  return $this->scanf($format);
147  }
148 
154  public function readLine()
155  {
156  return fgets($this->getStream());
157  }
158 
165  public function readAll($offset = 0)
166  {
167  return stream_get_contents($this->getStream(), -1, $offset);
168  }
169 
176  public function scanf($format)
177  {
178  return fscanf($this->getStream(), $format);
179  }
180 
189  public function write($string, $length)
190  {
191  if (0 > $length) {
192  throw new Exception(
193  'Length must be greater than 0, given %d.',
194  1,
195  $length
196  );
197  }
198 
199  return fwrite($this->getStream(), $string, $length);
200  }
201 
208  public function writeString($string)
209  {
210  $string = (string) $string;
211 
212  return $this->write($string, strlen($string));
213  }
214 
221  public function writeCharacter($char)
222  {
223  return $this->write((string) $char[0], 1);
224  }
225 
232  public function writeBoolean($boolean)
233  {
234  return $this->write((string) (bool) $boolean, 1);
235  }
236 
243  public function writeInteger($integer)
244  {
245  $integer = (string) (int) $integer;
246 
247  return $this->write($integer, strlen($integer));
248  }
249 
256  public function writeFloat($float)
257  {
258  $float = (string) (float) $float;
259 
260  return $this->write($float, strlen($float));
261  }
262 
269  public function writeArray(Array $array)
270  {
271  $array = var_export($array, true);
272 
273  return $this->write($array, strlen($array));
274  }
275 
282  public function writeLine($line)
283  {
284  if (false === $n = strpos($line, "\n")) {
285  return $this->write($line . "\n", strlen($line) + 1);
286  }
287 
288  ++$n;
289 
290  return $this->write(substr($line, 0, $n), $n);
291  }
292 
299  public function writeAll($string)
300  {
301  return $this->write($string, strlen($string));
302  }
303 
310  public function truncate($size)
311  {
312  return ftruncate($this->getStream(), $size);
313  }
314 
320  public function __toString()
321  {
322  return $this->readAll();
323  }
324 }
writeArray(Array $array)
Definition: ReadWrite.php:269
write($string, $length)
Definition: ReadWrite.php:189