Hoa central
ReadWrite.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Xml\Element;
38 
39 use Hoa\Stream;
41 use Hoa\Xml;
42 
51 class ReadWrite
52  extends Basic
53  implements Stream\IStream\In,
55 {
61  public function eof()
62  {
63  if (null === parent::$_buffer) {
64  return true;
65  }
66 
67  return parent::$_buffer->eof();
68  }
69 
77  public function read($length)
78  {
79  if (0 > $length) {
80  throw new Xml\Exception(
81  'Length must be greater than 0, given %d.',
82  0,
83  $length
84  );
85  }
86 
87  if (null === parent::$_buffer) {
88  parent::$_buffer = new Stringbuffer\ReadWrite();
89  parent::$_buffer->initializeWith($this->__toString());
90  }
91 
92  return parent::$_buffer->read($length);
93  }
94 
101  public function readString($length)
102  {
103  return $this->read($length);
104  }
105 
111  public function readCharacter()
112  {
113  return $this->read(1);
114  }
115 
121  public function readBoolean()
122  {
123  return (bool) $this->read(1);
124  }
125 
132  public function readInteger($length = 1)
133  {
134  return (int) $this->read($length);
135  }
136 
143  public function readFloat($length = 1)
144  {
145  return (float) $this->read($length);
146  }
147 
154  public function readArray($argument = null)
155  {
156  return (array) $this;
157  }
158 
164  public function readLine()
165  {
166  $handle = $this->readAll();
167  $n = strpos($handle, "\n");
168 
169  if (false === $n) {
170  return $handle;
171  }
172 
173  return substr($handle, 0, $n);
174  }
175 
182  public function readAll($offset = 0)
183  {
184  return $this->__toString();
185  }
186 
193  public function scanf($format)
194  {
195  return sscanf($this->readAll(), $format);
196  }
197 
203  public function readDOM()
204  {
205  return dom_import_simplexml($this);
206  }
207 
216  public function write($string, $length)
217  {
218  if (0 > $length) {
219  throw new Xml\Exception(
220  'Length must be greater than 0, given %d.',
221  1,
222  $length
223  );
224  }
225 
226  if (null === parent::$_buffer) {
227  parent::$_buffer = new Stringbuffer\ReadWrite();
228  parent::$_buffer->initializeWith($this->__toString());
229  }
230 
231  $l = parent::$_buffer->write($string, $length);
232 
233  if ($l !== $length) {
234  return false;
235  }
236 
237  $this[0] = parent::$_buffer->readAll();
238 
239  return $l;
240  }
241 
248  public function writeString($string)
249  {
250  $string = (string) $string;
251 
252  return $this->write($string, strlen($string));
253  }
254 
261  public function writeCharacter($char)
262  {
263  return $this->write((string) $char[0], 1);
264  }
265 
272  public function writeBoolean($boolean)
273  {
274  return $this->write((string) (bool) $boolean, 1);
275  }
276 
283  public function writeInteger($integer)
284  {
285  $integer = (string) (int) $integer;
286 
287  return $this->write($integer, strlen($integer));
288  }
289 
296  public function writeFloat($float)
297  {
298  $float = (string) (float) $float;
299 
300  return $this->write($float, strlen($float));
301  }
302 
310  public function writeArray(Array $array)
311  {
312  $document = $this->readDOM()->ownerDocument;
313 
314  foreach ($array as $name => $value) {
315  if (is_object($value)) {
316  if (!isset($this->{$name})) {
317  $this->addChild($name);
318  }
319 
320  $this->{$name}->readDOM()->parentNode->appendChild(
321  $document->importNode(clone $value->readDOM(), true)
322  );
323  } elseif (is_array($value) && !empty($value)) {
324  if (!isset($value[0])) {
325  $handle = $this->addChild($name);
326  }
327 
328  foreach ($value as $subname => $subvalue) {
329  if (is_object($subvalue)) {
330  if (!isset($this->{$name})) {
331  $this->addChild($name);
332  }
333 
334  $this->{$name}->readDOM()->parentNode->appendChild(
335  $document->importNode(clone $subvalue->readDOM(), true)
336  );
337  } else {
338  if (!isset($this->{$name})) {
339  $this->addChild($name);
340  }
341 
342  if (is_array($subvalue)) {
343  $handle->addChild($subname, null)
344  ->writeArray($subvalue);
345 
346  continue;
347  }
348 
349  if (is_bool($subvalue)) {
350  $subvalue = $subvalue ? 'true' : 'false';
351  }
352 
353  if (is_string($subname)) {
354  $handle->addChild($subname, $subvalue);
355  } else {
356  $this->addChild($name, $subvalue);
357  }
358  }
359  }
360  }
361  }
362 
363  return;
364  }
365 
372  public function writeLine($line)
373  {
374  if (false === $n = strpos($line, "\n")) {
375  return $this->write($line . "\n", strlen($line) + 1);
376  }
377 
378  ++$n;
379 
380  return $this->write(substr($line, 0, $n), $n);
381  }
382 
389  public function writeAll($string)
390  {
391  return $this->write($string, strlen($string));
392  }
393 
400  public function truncate($size)
401  {
402  if (null === parent::$_buffer) {
403  parent::$_buffer = new Stringbuffer\ReadWrite();
404  parent::$_buffer->initializeWith($this->__toString());
405  }
406 
407  return parent::$_buffer->truncate($size);
408  }
409 
417  public function writeDOM(\DOMNode $dom)
418  {
419  $sx = simplexml_import_dom($dom, get_class($this));
420 
421  throw new Xml\Exception('Hmm, TODO?', 42);
422 
423  return true;
424  }
425 
433  public function writeAttributes(Array $attributes)
434  {
435  foreach ($attributes as $name => $value) {
436  $this->writeAttribute($name, $value);
437  }
438 
439  return;
440  }
441 
450  public function writeAttribute($name, $value)
451  {
452  $this[$name] = $value;
453 
454  return;
455  }
456 
468  public function writeCustomAttributes($set, Array $pairs)
469  {
470  foreach ($pairs as $attribute => $value) {
471  $this->writeAttribute($set . '-' . $attribute, $value);
472  }
473 
474  return;
475  }
476 
483  public function removeAttribute($name)
484  {
485  unset($this[$name]);
486 
487  return;
488  }
489 }
writeAttributes(Array $attributes)
Definition: ReadWrite.php:433
readArray($argument=null)
Definition: ReadWrite.php:154
writeArray(Array $array)
Definition: ReadWrite.php:310
writeCustomAttributes($set, Array $pairs)
Definition: ReadWrite.php:468
writeDOM(\DOMNode $dom)
Definition: ReadWrite.php:417
write($string, $length)
Definition: ReadWrite.php:189
writeAttribute($name, $value)
Definition: ReadWrite.php:450
write($string, $length)
Definition: ReadWrite.php:216