Hoa central
Write.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 Write extends Basic implements Stream\IStream\Out
52 {
61  public function write($string, $length)
62  {
63  if (0 > $length) {
64  throw new Xml\Exception(
65  'Length must be greater than 0, given %d.',
66  0,
67  $length
68  );
69  }
70 
71  if (null === parent::$_buffer) {
72  parent::$_buffer = new Stringbuffer\ReadWrite();
73  parent::$_buffer->initializeWith($this->__toString());
74  }
75 
76  $l = parent::$_buffer->write($string, $length);
77 
78  if ($l !== $length) {
79  return false;
80  }
81 
82  $this[0] = parent::$_buffer->readAll();
83 
84  return $l;
85  }
86 
93  public function writeString($string)
94  {
95  $string = (string) $string;
96 
97  return $this->write($string, strlen($string));
98  }
99 
106  public function writeCharacter($char)
107  {
108  return $this->write((string) $char[0], 1);
109  }
110 
117  public function writeBoolean($boolean)
118  {
119  return $this->write((string) (bool) $boolean, 1);
120  }
121 
128  public function writeInteger($integer)
129  {
130  $integer = (string) (int) $integer;
131 
132  return $this->write($integer, strlen($integer));
133  }
134 
141  public function writeFloat($float)
142  {
143  $float = (string) (float) $float;
144 
145  return $this->write($float, strlen($float));
146  }
147 
154  public function writeArray(Array $array)
155  {
156  $document = $this->readDOM()->ownerDocument;
157 
158  foreach ($array as $name => $value) {
159  if (is_object($value)) {
160  if (!isset($this->{$name})) {
161  $this->addChild($name);
162  }
163 
164  $this->{$name}->readDOM()->parentNode->appendChild(
165  $document->importNode(clone $value->readDOM(), true)
166  );
167  } elseif (is_array($value) && !empty($value)) {
168  if (!isset($value[0])) {
169  $handle = $this->addChild($name);
170  }
171 
172  foreach ($value as $subname => $subvalue) {
173  if (is_object($subvalue)) {
174  if (!isset($this->{$name})) {
175  $this->addChild($name);
176  }
177 
178  $this->{$name}->readDOM()->parentNode->appendChild(
179  $document->importNode(clone $subvalue->readDOM(), true)
180  );
181  } else {
182  if (!isset($this->{$name})) {
183  $this->addChild($name);
184  }
185 
186  if (is_array($subvalue)) {
187  $handle->addChild($subname, null)
188  ->writeArray($subvalue);
189 
190  continue;
191  }
192 
193  if (is_bool($subvalue)) {
194  $subvalue = $subvalue ? 'true' : 'false';
195  }
196 
197  if (is_string($subname)) {
198  $handle->addChild($subname, $subvalue);
199  } else {
200  $this->addChild($name, $subvalue);
201  }
202  }
203  }
204  }
205  }
206 
207  return;
208  }
209 
216  public function writeLine($line)
217  {
218  if (false === $n = strpos($line, "\n")) {
219  return $this->write($line . "\n", strlen($line) + 1);
220  }
221 
222  ++$n;
223 
224  return $this->write(substr($line, 0, $n), $n);
225  }
226 
233  public function writeAll($string)
234  {
235  return $this->write($string, strlen($string));
236  }
237 
244  public function truncate($size)
245  {
246  return parent::$_buffer->truncate($size);
247  }
248 
256  public function writeDOM(\DOMNode $dom)
257  {
258  $sx = simplexml_import_dom($dom, __CLASS__);
259 
260  throw new Xml\Exception('Hmm, TODO?', 42);
261 
262  return true;
263  }
264 
272  public function writeAttributes(Array $attributes)
273  {
274  foreach ($attributes as $name => $value) {
275  $this->writeAttribute($name, $value);
276  }
277 
278  return;
279  }
280 
289  public function writeAttribute($name, $value)
290  {
291  $this[$name] = $value;
292 
293  return;
294  }
295 
302  public function removeAttribute($name)
303  {
304  unset($this[$name]);
305 
306  return;
307  }
308 }
writeAttributes(Array $attributes)
Definition: Write.php:272
removeAttribute($name)
Definition: Write.php:302
write($string, $length)
Definition: Write.php:61
writeInteger($integer)
Definition: Write.php:128
writeCharacter($char)
Definition: Write.php:106
writeString($string)
Definition: Write.php:93
writeArray(Array $array)
Definition: Write.php:154
writeBoolean($boolean)
Definition: Write.php:117
writeDOM(\DOMNode $dom)
Definition: Write.php:256
writeAttribute($name, $value)
Definition: Write.php:289
write($string, $length)
Definition: Write.php:59
writeFloat($float)
Definition: Write.php:141
writeAll($string)
Definition: Write.php:233