Hoa central
Write.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\File\Temporary;
38 
39 use Hoa\File;
40 use Hoa\Stream;
41 
50 class Write extends Temporary implements Stream\IStream\Out
51 {
62  public function __construct(
63  $streamName,
64  $mode = parent::MODE_APPEND_WRITE,
65  $context = null,
66  $wait = false
67  ) {
68  parent::__construct($streamName, $mode, $context, $wait);
69 
70  return;
71  }
72 
82  protected function &_open($streamName, Stream\Context $context = null)
83  {
84  static $createModes = [
85  parent::MODE_TRUNCATE_WRITE,
86  parent::MODE_APPEND_WRITE,
87  parent::MODE_CREATE_WRITE
88  ];
89 
90  if (!in_array($this->getMode(), $createModes)) {
91  throw new File\Exception(
92  'Open mode are not supported; given %d. Only %s are supported.',
93  0,
94  [$this->getMode(), implode(', ', $createModes)]
95  );
96  }
97 
98  preg_match('#^(\w+)://#', $streamName, $match);
99 
100  if (((isset($match[1]) && $match[1] == 'file') || !isset($match[1])) &&
101  !file_exists($streamName)) {
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 File\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 }
writeArray(Array $array)
Definition: Write.php:202
writeBoolean($boolean)
Definition: Write.php:165
writeInteger($integer)
Definition: Write.php:176
write($string, $length)
Definition: Write.php:122
& _open($streamName, Stream\Context $context=null)
Definition: Write.php:82
__construct($streamName, $mode=parent::MODE_APPEND_WRITE, $context=null, $wait=false)
Definition: Write.php:62