Hoa central
Write.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 
49 class Write extends Link 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 File\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)) {
102  'File %s does not exist.',
103  1,
104  $streamName
105  );
106  }
107 
108  $out = parent::_open($streamName, $context);
109 
110  return $out;
111  }
112 
121  public function write($string, $length)
122  {
123  if (0 > $length) {
124  throw new File\Exception(
125  'Length must be greater than 0, given %d.',
126  2,
127  $length
128  );
129  }
130 
131  return fwrite($this->getStream(), $string, $length);
132  }
133 
140  public function writeString($string)
141  {
142  $string = (string) $string;
143 
144  return $this->write($string, strlen($string));
145  }
146 
153  public function writeCharacter($char)
154  {
155  return $this->write((string) $char[0], 1);
156  }
157 
164  public function writeBoolean($boolean)
165  {
166  return $this->write((string) (bool) $boolean, 1);
167  }
168 
175  public function writeInteger($integer)
176  {
177  $integer = (string) (int) $integer;
178 
179  return $this->write($integer, strlen($integer));
180  }
181 
188  public function writeFloat($float)
189  {
190  $float = (string) (float) $float;
191 
192  return $this->write($float, strlen($float));
193  }
194 
201  public function writeArray(Array $array)
202  {
203  $array = var_export($array, true);
204 
205  return $this->write($array, strlen($array));
206  }
207 
214  public function writeLine($line)
215  {
216  if (false === $n = strpos($line, "\n")) {
217  return $this->write($line . "\n", strlen($line) + 1);
218  }
219 
220  ++$n;
221 
222  return $this->write(substr($line, 0, $n), $n);
223  }
224 
231  public function writeAll($string)
232  {
233  return $this->write($string, strlen($string));
234  }
235 
242  public function truncate($size)
243  {
244  return ftruncate($this->getStream(), $size);
245  }
246 }
writeCharacter($char)
Definition: Write.php:153
writeArray(Array $array)
Definition: Write.php:201
__construct($streamName, $mode=parent::MODE_APPEND_WRITE, $context=null, $wait=false)
Definition: Write.php:61
writeLine($line)
Definition: Write.php:214
write($string, $length)
Definition: Write.php:121
writeString($string)
Definition: Write.php:140
& _open($streamName, Stream\Context $context=null)
Definition: Write.php:81
writeInteger($integer)
Definition: Write.php:175
writeFloat($float)
Definition: Write.php:188
writeAll($string)
Definition: Write.php:231
writeBoolean($boolean)
Definition: Write.php:164