|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Rest |
|
17 * @subpackage Client |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: Result.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @category Zend |
|
25 * @package Zend_Rest |
|
26 * @subpackage Client |
|
27 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
28 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
29 */ |
|
30 class Zend_Rest_Client_Result implements IteratorAggregate { |
|
31 /** |
|
32 * @var SimpleXMLElement |
|
33 */ |
|
34 protected $_sxml; |
|
35 |
|
36 /** |
|
37 * error information |
|
38 * @var string |
|
39 */ |
|
40 protected $_errstr; |
|
41 |
|
42 /** |
|
43 * Constructor |
|
44 * |
|
45 * @param string $data XML Result |
|
46 * @return void |
|
47 */ |
|
48 public function __construct($data) |
|
49 { |
|
50 set_error_handler(array($this, 'handleXmlErrors')); |
|
51 $this->_sxml = simplexml_load_string($data); |
|
52 restore_error_handler(); |
|
53 if($this->_sxml === false) { |
|
54 if ($this->_errstr === null) { |
|
55 $message = "An error occured while parsing the REST response with simplexml."; |
|
56 } else { |
|
57 $message = "REST Response Error: " . $this->_errstr; |
|
58 $this->_errstr = null; |
|
59 } |
|
60 require_once "Zend/Rest/Client/Result/Exception.php"; |
|
61 throw new Zend_Rest_Client_Result_Exception($message); |
|
62 } |
|
63 } |
|
64 |
|
65 /** |
|
66 * Temporary error handler for parsing REST responses. |
|
67 * |
|
68 * @param int $errno |
|
69 * @param string $errstr |
|
70 * @param string $errfile |
|
71 * @param string $errline |
|
72 * @param array $errcontext |
|
73 * @return true |
|
74 */ |
|
75 public function handleXmlErrors($errno, $errstr, $errfile = null, $errline = null, array $errcontext = null) |
|
76 { |
|
77 $this->_errstr = $errstr; |
|
78 return true; |
|
79 } |
|
80 |
|
81 /** |
|
82 * Casts a SimpleXMLElement to its appropriate PHP value |
|
83 * |
|
84 * @param SimpleXMLElement $value |
|
85 * @return mixed |
|
86 */ |
|
87 public function toValue(SimpleXMLElement $value) |
|
88 { |
|
89 $node = dom_import_simplexml($value); |
|
90 return $node->nodeValue; |
|
91 } |
|
92 |
|
93 /** |
|
94 * Get Property Overload |
|
95 * |
|
96 * @param string $name |
|
97 * @return null|SimpleXMLElement|array Null if not found, SimpleXMLElement if only one value found, array of Zend_Rest_Client_Result objects otherwise |
|
98 */ |
|
99 public function __get($name) |
|
100 { |
|
101 if (isset($this->_sxml->{$name})) { |
|
102 return $this->_sxml->{$name}; |
|
103 } |
|
104 |
|
105 $result = $this->_sxml->xpath("//$name"); |
|
106 $count = count($result); |
|
107 |
|
108 if ($count == 0) { |
|
109 return null; |
|
110 } elseif ($count == 1) { |
|
111 return $result[0]; |
|
112 } else { |
|
113 return $result; |
|
114 } |
|
115 } |
|
116 |
|
117 /** |
|
118 * Cast properties to PHP values |
|
119 * |
|
120 * For arrays, loops through each element and casts to a value as well. |
|
121 * |
|
122 * @param string $method |
|
123 * @param array $args |
|
124 * @return mixed |
|
125 */ |
|
126 public function __call($method, $args) |
|
127 { |
|
128 if (null !== ($value = $this->__get($method))) { |
|
129 if (!is_array($value)) { |
|
130 return $this->toValue($value); |
|
131 } else { |
|
132 $return = array(); |
|
133 foreach ($value as $element) { |
|
134 $return[] = $this->toValue($element); |
|
135 } |
|
136 return $return; |
|
137 } |
|
138 } |
|
139 |
|
140 return null; |
|
141 } |
|
142 |
|
143 |
|
144 /** |
|
145 * Isset Overload |
|
146 * |
|
147 * @param string $name |
|
148 * @return boolean |
|
149 */ |
|
150 public function __isset($name) |
|
151 { |
|
152 if (isset($this->_sxml->{$name})) { |
|
153 return true; |
|
154 } |
|
155 |
|
156 $result = $this->_sxml->xpath("//$name"); |
|
157 |
|
158 if (sizeof($result) > 0) { |
|
159 return true; |
|
160 } |
|
161 |
|
162 return false; |
|
163 } |
|
164 |
|
165 /** |
|
166 * Implement IteratorAggregate::getIterator() |
|
167 * |
|
168 * @return SimpleXMLIterator |
|
169 */ |
|
170 public function getIterator() |
|
171 { |
|
172 return $this->_sxml; |
|
173 } |
|
174 |
|
175 /** |
|
176 * Get Request Status |
|
177 * |
|
178 * @return boolean |
|
179 */ |
|
180 public function getStatus() |
|
181 { |
|
182 $status = $this->_sxml->xpath('//status/text()'); |
|
183 |
|
184 $status = strtolower($status[0]); |
|
185 |
|
186 if (ctype_alpha($status) && $status == 'success') { |
|
187 return true; |
|
188 } elseif (ctype_alpha($status) && $status != 'success') { |
|
189 return false; |
|
190 } else { |
|
191 return (bool) $status; |
|
192 } |
|
193 } |
|
194 |
|
195 public function isError() |
|
196 { |
|
197 $status = $this->getStatus(); |
|
198 if ($status) { |
|
199 return false; |
|
200 } else { |
|
201 return true; |
|
202 } |
|
203 } |
|
204 |
|
205 public function isSuccess() |
|
206 { |
|
207 $status = $this->getStatus(); |
|
208 if ($status) { |
|
209 return true; |
|
210 } else { |
|
211 return false; |
|
212 } |
|
213 } |
|
214 |
|
215 /** |
|
216 * toString overload |
|
217 * |
|
218 * Be sure to only call this when the result is a single value! |
|
219 * |
|
220 * @return string |
|
221 */ |
|
222 public function __toString() |
|
223 { |
|
224 if (!$this->getStatus()) { |
|
225 $message = $this->_sxml->xpath('//message'); |
|
226 return (string) $message[0]; |
|
227 } else { |
|
228 $result = $this->_sxml->xpath('//response'); |
|
229 if (sizeof($result) > 1) { |
|
230 return (string) "An error occured."; |
|
231 } else { |
|
232 return (string) $result[0]; |
|
233 } |
|
234 } |
|
235 } |
|
236 } |