Hoa central
Write.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\File;
38 
39 use Hoa\Stream;
40 
49 class Write extends File implements Stream\IStream\Out
50 {
61  public function __construct(
62  $streamName,
63  $mode = parent::MODE_APPEND_WRITE,
64  $context = null,
65  $wait = false
66  ) {
67  parent::__construct($streamName, $mode, $context, $wait);
68 
69  return;
70  }
71 
81  protected function &_open($streamName, Stream\Context $context = null)
82  {
83  static $createModes = [
84  parent::MODE_TRUNCATE_WRITE,
85  parent::MODE_APPEND_WRITE,
86  parent::MODE_CREATE_WRITE
87  ];
88 
89  if (!in_array($this->getMode(), $createModes)) {
90  throw new Exception(
91  'Open mode are not supported; given %d. Only %s are supported.',
92  0,
93  [$this->getMode(), implode(', ', $createModes)]
94  );
95  }
96 
97  preg_match('#^(\w+)://#', $streamName, $match);
98 
99  if (((isset($match[1]) && $match[1] == 'file') || !isset($match[1])) &&
100  !file_exists($streamName) &&
101  parent::MODE_TRUNCATE_WRITE == $this->getMode()) {
102  throw new Exception\FileDoesNotExist(
103  'File %s does not exist.',
104  1,
105  $streamName
106  );
107  }
108 
109  $out = parent::_open($streamName, $context);
110 
111  return $out;
112  }
113 
122  public function write($string, $length)
123  {
124  if (0 > $length) {
125  throw new Exception(
126  'Length must be greater than 0, given %d.',
127  2,
128  $length
129  );
130  }
131 
132  return fwrite($this->getStream(), $string, $length);
133  }
134 
141  public function writeString($string)
142  {
143  $string = (string) $string;
144 
145  return $this->write($string, strlen($string));
146  }
147 
154  public function writeCharacter($char)
155  {
156  return $this->write((string) $char[0], 1);
157  }
158 
165  public function writeBoolean($boolean)
166  {
167  return $this->write((string) (bool) $boolean, 1);
168  }
169 
176  public function writeInteger($integer)
177  {
178  $integer = (string) (int) $integer;
179 
180  return $this->write($integer, strlen($integer));
181  }
182 
189  public function writeFloat($float)
190  {
191  $float = (string) (float) $float;
192 
193  return $this->write($float, strlen($float));
194  }
195 
202  public function writeArray(Array $array)
203  {
204  $array = var_export($array, true);
205 
206  return $this->write($array, strlen($array));
207  }
208 
215  public function writeLine($line)
216  {
217  if (false === $n = strpos($line, "\n")) {
218  return $this->write($line . "\n", strlen($line) + 1);
219  }
220 
221  ++$n;
222 
223  return $this->write(substr($line, 0, $n), $n);
224  }
225 
232  public function writeAll($string)
233  {
234  return $this->write($string, strlen($string));
235  }
236 
243  public function truncate($size)
244  {
245  return ftruncate($this->getStream(), $size);
246  }
247 }
__construct($streamName, $mode=parent::MODE_APPEND_WRITE, $context=null, $wait=false)
Definition: Write.php:61
writeAll($string)
Definition: Write.php:232
writeLine($line)
Definition: Write.php:215
write($string, $length)
Definition: Write.php:122
writeBoolean($boolean)
Definition: Write.php:165
writeFloat($float)
Definition: Write.php:189
& _open($streamName, Stream\Context $context=null)
Definition: Write.php:81
writeCharacter($char)
Definition: Write.php:154
truncate($size)
Definition: Write.php:243
writeArray(Array $array)
Definition: Write.php:202
writeInteger($integer)
Definition: Write.php:176
writeString($string)
Definition: Write.php:141