XRL  latest
Simple XML-RPC Library (both client and server)
Dom.php
1 <?php
2 /*
3  * This file is part of XRL, a simple XML-RPC Library for PHP.
4  *
5  * Copyright (c) 2012, XRL Team. All rights reserved.
6  * XRL is licensed under the 3-clause BSD License.
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace fpoirotte\XRL\Types;
13 
24 {
26  public function __toString()
27  {
28  return $this->value->asXML();
29  }
30 
32  public function set($value)
33  {
34  if (!is_object($value)) {
35  throw new \InvalidArgumentException('Expected an object');
36  }
37 
38  if ($value instanceof \DOMNode) {
39  $value = simplexml_import_dom($value);
40  }
41 
42  if ($value instanceof \XMLWriter) {
43  $value = new \SimpleXMLElement($value->outputMemory(), LIBXML_NONET);
44  }
45 
46  if (!($value instanceof \SimpleXMLElement)) {
47  throw new \InvalidArgumentException('Expected a SimpleXMLElement object');
48  }
49 
50  $this->value = $value;
51  }
52 
54  public function write(\XMLWriter $writer, \DateTimeZone $timezone, $stringTag)
55  {
56  $xml = $this->value->asXML();
57 
58  // Not all combinations of PHP/libxml support stripping
59  // the XML declaration. So, we do it ourselves here.
60  if (strlen($xml) >= 6 && !strncmp($xml, '<?xml', 5) &&
61  strpos(" \t\r\n", $xml[5]) !== false) {
62  $xml = (string) substr($xml, strpos($xml, '?>') + 2);
63  }
64 
65  $writer->startElementNS(
66  'ex',
67  'dom',
68  'http://ws.apache.org/xmlrpc/namespaces/extensions'
69  );
70  $writer->writeRaw($xml);
71  $writer->fullEndElement();
72  }
73 
75  protected static function parse($value, \DateTimeZone $timezone = null)
76  {
77  return simplexml_load_string($value, '\\SimpleXMLElement', LIBXML_NONET);
78  }
79 }
write(\XMLWriter $writer,\DateTimeZone $timezone, $stringTag)
Definition: Dom.php:54
$value
Current value associated with this object.
The XML-RPC "dom" type.
Definition: Dom.php:23
A class representing an abstract XML-RPC type.
static parse($value,\DateTimeZone $timezone=null)
Definition: Dom.php:75