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