Hoa central
Write.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Stringbuffer;
38 
39 use Hoa\Stream;
40 
49 class Write extends Stringbuffer implements Stream\IStream\Out
50 {
59  public function write($string, $length)
60  {
61  if (0 > $length) {
62  throw new Exception(
63  'Length must be greater than 0, given %d.',
64  0,
65  $length
66  );
67  }
68 
69  return fwrite($this->getStream(), $string, $length);
70  }
71 
78  public function writeString($string)
79  {
80  $string = (string) $string;
81 
82  return $this->write($string, strlen($string));
83  }
84 
91  public function writeCharacter($char)
92  {
93  return $this->write((string) $char[0], 1);
94  }
95 
102  public function writeBoolean($boolean)
103  {
104  return $this->write((string) (bool) $boolean, 1);
105  }
106 
113  public function writeInteger($integer)
114  {
115  $integer = (string) (int) $integer;
116 
117  return $this->write($integer, strlen($integer));
118  }
119 
126  public function writeFloat($float)
127  {
128  $float = (string) (float) $float;
129 
130  return $this->write($float, strlen($float));
131  }
132 
139  public function writeArray(Array $array)
140  {
141  $array = var_export($array, true);
142 
143  return $this->write($array, strlen($array));
144  }
145 
152  public function writeLine($line)
153  {
154  if (false === $n = strpos($line, "\n")) {
155  return $this->write($line . "\n", strlen($line) + 1);
156  }
157 
158  ++$n;
159 
160  return $this->write(substr($line, 0, $n), $n);
161  }
162 
169  public function writeAll($string)
170  {
171  return $this->write($string, strlen($string));
172  }
173 
180  public function truncate($size)
181  {
182  return ftruncate($this->getStream(), $size);
183  }
184 }
writeArray(Array $array)
Definition: Write.php:139
writeBoolean($boolean)
Definition: Write.php:102
writeString($string)
Definition: Write.php:78
writeCharacter($char)
Definition: Write.php:91
write($string, $length)
Definition: Write.php:59
writeInteger($integer)
Definition: Write.php:113