|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00030 class Zend_Rest_Client_Result implements IteratorAggregate { 00034 protected $_sxml; 00035 00040 protected $_errstr; 00041 00048 public function __construct($data) 00049 { 00050 set_error_handler(array($this, 'handleXmlErrors')); 00051 $this->_sxml = simplexml_load_string($data); 00052 restore_error_handler(); 00053 if($this->_sxml === false) { 00054 if ($this->_errstr === null) { 00055 $message = "An error occured while parsing the REST response with simplexml."; 00056 } else { 00057 $message = "REST Response Error: " . $this->_errstr; 00058 $this->_errstr = null; 00059 } 00060 require_once "Zend/Rest/Client/Result/Exception.php"; 00061 throw new Zend_Rest_Client_Result_Exception($message); 00062 } 00063 } 00064 00075 public function handleXmlErrors($errno, $errstr, $errfile = null, $errline = null, array $errcontext = null) 00076 { 00077 $this->_errstr = $errstr; 00078 return true; 00079 } 00080 00087 public function toValue(SimpleXMLElement $value) 00088 { 00089 $node = dom_import_simplexml($value); 00090 return $node->nodeValue; 00091 } 00092 00099 public function __get($name) 00100 { 00101 if (isset($this->_sxml->{$name})) { 00102 return $this->_sxml->{$name}; 00103 } 00104 00105 $result = $this->_sxml->xpath("//$name"); 00106 $count = count($result); 00107 00108 if ($count == 0) { 00109 return null; 00110 } elseif ($count == 1) { 00111 return $result[0]; 00112 } else { 00113 return $result; 00114 } 00115 } 00116 00126 public function __call($method, $args) 00127 { 00128 if (null !== ($value = $this->__get($method))) { 00129 if (!is_array($value)) { 00130 return $this->toValue($value); 00131 } else { 00132 $return = array(); 00133 foreach ($value as $element) { 00134 $return[] = $this->toValue($element); 00135 } 00136 return $return; 00137 } 00138 } 00139 00140 return null; 00141 } 00142 00143 00150 public function __isset($name) 00151 { 00152 if (isset($this->_sxml->{$name})) { 00153 return true; 00154 } 00155 00156 $result = $this->_sxml->xpath("//$name"); 00157 00158 if (sizeof($result) > 0) { 00159 return true; 00160 } 00161 00162 return false; 00163 } 00164 00170 public function getIterator() 00171 { 00172 return $this->_sxml; 00173 } 00174 00180 public function getStatus() 00181 { 00182 $status = $this->_sxml->xpath('//status/text()'); 00183 00184 $status = strtolower($status[0]); 00185 00186 if (ctype_alpha($status) && $status == 'success') { 00187 return true; 00188 } elseif (ctype_alpha($status) && $status != 'success') { 00189 return false; 00190 } else { 00191 return (bool) $status; 00192 } 00193 } 00194 00195 public function isError() 00196 { 00197 $status = $this->getStatus(); 00198 if ($status) { 00199 return false; 00200 } else { 00201 return true; 00202 } 00203 } 00204 00205 public function isSuccess() 00206 { 00207 $status = $this->getStatus(); 00208 if ($status) { 00209 return true; 00210 } else { 00211 return false; 00212 } 00213 } 00214 00222 public function __toString() 00223 { 00224 if (!$this->getStatus()) { 00225 $message = $this->_sxml->xpath('//message'); 00226 return (string) $message[0]; 00227 } else { 00228 $result = $this->_sxml->xpath('//response'); 00229 if (sizeof($result) > 1) { 00230 return (string) "An error occured."; 00231 } else { 00232 return (string) $result[0]; 00233 } 00234 } 00235 } 00236 }