Hoa central
ReadWrite.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\File;
38 
39 use Hoa\Stream;
40 
49 class ReadWrite
50  extends File
51  implements Stream\IStream\In,
53 {
64  public function __construct(
65  $streamName,
66  $mode = parent::MODE_APPEND_READ_WRITE,
67  $context = null,
68  $wait = false
69  ) {
70  parent::__construct($streamName, $mode, $context, $wait);
71 
72  return;
73  }
74 
84  protected function &_open($streamName, Stream\Context $context = null)
85  {
86  static $createModes = [
87  parent::MODE_READ_WRITE,
88  parent::MODE_TRUNCATE_READ_WRITE,
89  parent::MODE_APPEND_READ_WRITE,
90  parent::MODE_CREATE_READ_WRITE
91  ];
92 
93  if (!in_array($this->getMode(), $createModes)) {
94  throw new Exception(
95  'Open mode are not supported; given %d. Only %s are supported.',
96  0,
97  [$this->getMode(), implode(', ', $createModes)]
98  );
99  }
100 
101  preg_match('#^(\w+)://#', $streamName, $match);
102 
103  if (((isset($match[1]) && $match[1] == 'file') || !isset($match[1])) &&
104  !file_exists($streamName) &&
105  parent::MODE_READ_WRITE == $this->getMode()) {
106  throw new Exception\FileDoesNotExist(
107  'File %s does not exist.',
108  1,
109  $streamName
110  );
111  }
112 
113  $out = parent::_open($streamName, $context);
114 
115  return $out;
116  }
117 
123  public function eof()
124  {
125  return feof($this->getStream());
126  }
127 
135  public function read($length)
136  {
137  if (0 > $length) {
138  throw new Exception(
139  'Length must be greater than 0, given %d.',
140  2,
141  $length
142  );
143  }
144 
145  return fread($this->getStream(), $length);
146  }
147 
154  public function readString($length)
155  {
156  return $this->read($length);
157  }
158 
164  public function readCharacter()
165  {
166  return fgetc($this->getStream());
167  }
168 
174  public function readBoolean()
175  {
176  return (bool) $this->read(1);
177  }
178 
185  public function readInteger($length = 1)
186  {
187  return (int) $this->read($length);
188  }
189 
196  public function readFloat($length = 1)
197  {
198  return (float) $this->read($length);
199  }
200 
208  public function readArray($format = null)
209  {
210  return $this->scanf($format);
211  }
212 
218  public function readLine()
219  {
220  return fgets($this->getStream());
221  }
222 
229  public function readAll($offset = 0)
230  {
231  return stream_get_contents($this->getStream(), -1, $offset);
232  }
233 
240  public function scanf($format)
241  {
242  return fscanf($this->getStream(), $format);
243  }
244 
253  public function write($string, $length)
254  {
255  if (0 > $length) {
256  throw new Exception(
257  'Length must be greater than 0, given %d.',
258  3,
259  $length
260  );
261  }
262 
263  return fwrite($this->getStream(), $string, $length);
264  }
265 
272  public function writeString($string)
273  {
274  $string = (string) $string;
275 
276  return $this->write($string, strlen($string));
277  }
278 
285  public function writeCharacter($char)
286  {
287  return $this->write((string) $char[0], 1);
288  }
289 
296  public function writeBoolean($boolean)
297  {
298  return $this->write((string) (bool) $boolean, 1);
299  }
300 
307  public function writeInteger($integer)
308  {
309  $integer = (string) (int) $integer;
310 
311  return $this->write($integer, strlen($integer));
312  }
313 
320  public function writeFloat($float)
321  {
322  $float = (string) (float) $float;
323 
324  return $this->write($float, strlen($float));
325  }
326 
333  public function writeArray(Array $array)
334  {
335  $array = var_export($array, true);
336 
337  return $this->write($array, strlen($array));
338  }
339 
346  public function writeLine($line)
347  {
348  if (false === $n = strpos($line, "\n")) {
349  return $this->write($line . "\n", strlen($line) + 1);
350  }
351 
352  ++$n;
353 
354  return $this->write(substr($line, 0, $n), $n);
355  }
356 
363  public function writeAll($string)
364  {
365  return $this->write($string, strlen($string));
366  }
367 
374  public function truncate($size)
375  {
376  return ftruncate($this->getStream(), $size);
377  }
378 }
writeString($string)
Definition: ReadWrite.php:272
& _open($streamName, Stream\Context $context=null)
Definition: ReadWrite.php:84
writeArray(Array $array)
Definition: ReadWrite.php:333
write($string, $length)
Definition: ReadWrite.php:253
readInteger($length=1)
Definition: ReadWrite.php:185
readAll($offset=0)
Definition: ReadWrite.php:229
writeBoolean($boolean)
Definition: ReadWrite.php:296
writeInteger($integer)
Definition: ReadWrite.php:307
readArray($format=null)
Definition: ReadWrite.php:208
readString($length)
Definition: ReadWrite.php:154
__construct($streamName, $mode=parent::MODE_APPEND_READ_WRITE, $context=null, $wait=false)
Definition: ReadWrite.php:64
readFloat($length=1)
Definition: ReadWrite.php:196