XRL  2.0.0
Simple XML-RPC Library (both client and server)
Node.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;
13 
25 class Node
26 {
28  protected $properties;
29 
44  public function __construct(\XMLReader $reader, $validate, $subtrees)
45  {
46  do {
47  // We must silence read()/next() as old PHPs (5.3.x) emit warnings
48  // which get caught by PHPUnit and other custom error handlers
49  // when the methods fail and this is known to cause some issues.
50  if (($subtrees && !@$reader->read()) ||
51  (!$subtrees && !@$reader->next())) {
52  $error = libxml_get_last_error();
53  if (!$error) {
54  // We reached the end of the document.
55  // This is not an error but it causes
56  // read() to fail anyway.
57  // We throw a special error which gets caught
58  // and dealt with appropriately by the caller.
59  throw new \InvalidArgumentException('End of document');
60  }
61 
62  if ($error->code === 32) {
63  // 32 = XML_ERR_UNSUPPORTED_ENCODING
64  throw \fpoirotte\XRL\Faults::get(
65  \fpoirotte\XRL\Faults::UNSUPPORTED_ENCODING
66  );
67  } elseif ($error->code === 1 || $error->code === 2) {
68  // 1 = XML_ERR_INTERNAL_ERROR
69  // 2 = XML_ERR_NO_MEMORY
70  throw \fpoirotte\XRL\Faults::get(
71  \fpoirotte\XRL\Faults::INTERNAL_ERROR
72  );
73  } else {
74  // Generic error handling.
75  throw \fpoirotte\XRL\Faults::get(
76  \fpoirotte\XRL\Faults::NOT_WELL_FORMED
77  );
78  }
79  }
80  if ($validate && !$reader->isValid()) {
81  throw \fpoirotte\XRL\Faults::get(
82  \fpoirotte\XRL\Faults::INVALID_XML_RPC
83  );
84  }
85 
86  $subtrees = true;
87  } while ($reader->nodeType === \XMLReader::SIGNIFICANT_WHITESPACE);
88 
89  $fields = array(
90  'nodeType',
91  'value',
92  'isEmptyElement',
93  'localName',
94  'namespaceURI',
95  );
96 
97  $this->properties = array();
98  foreach ($fields as $field) {
99  $this->properties[$field] = $reader->$field;
100  }
101  $name = $reader->localName;
102  if ($reader->namespaceURI !== '') {
103  $name = '{' . $reader->namespaceURI . '}' . $name;
104  }
105  $this->properties['name'] = $name;
106  }
107 
125  public function __get($field)
126  {
127  if (!isset($this->properties[$field])) {
128  throw new \UnexpectedValueException("Unknown property '$field'");
129  }
130 
131  return $this->properties[$field];
132  }
133 
143  public function emptyNodeExpansionWorked()
144  {
145  if ($this->properties['nodeType'] == \XMLReader::ELEMENT &&
146  $this->properties['isEmptyElement'] == true) {
147  $this->properties['nodeType'] = \XMLReader::END_ELEMENT;
148  $this->properties['isEmptyElement'] = false;
149  return true;
150  }
151  return false;
152  }
153 }
An XML node as read from an XML reader.
Definition: Node.php:25
const UNSUPPORTED_ENCODING
Alias for the corresponding interoperability fault.
Definition: Faults.php:71
__get($field)
Definition: Node.php:125
__construct(\XMLReader $reader, $validate, $subtrees)
Definition: Node.php:44
const INVALID_XML_RPC
Alias for the corresponding interoperability fault.
Definition: Faults.php:77
emptyNodeExpansionWorked()
Definition: Node.php:143
const INTERNAL_ERROR
Alias for the corresponding interoperability fault.
Definition: Faults.php:86
const NOT_WELL_FORMED
Alias for the corresponding interoperability fault.
Definition: Faults.php:68
$properties
Fields that make up this node.
Definition: Node.php:28