PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/XmlRpc/Value.php

https://bitbucket.org/blackriver/openx
PHP | 401 lines | 189 code | 63 blank | 149 comment | 17 complexity | f11d80454daddda9518283fdbc32c222 MD5 | raw file
  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_XmlRpc
  17. * @subpackage Value
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Value.php 30068 2008-12-15 17:04:05Z pawel.gruszczynski $
  21. */
  22. /** Zend_XmlRpc_Value_Exception */
  23. require_once 'Zend/XmlRpc/Value/Exception.php';
  24. /** Zend_XmlRpc_Value_Scalar */
  25. require_once 'Zend/XmlRpc/Value/Scalar.php';
  26. /** Zend_XmlRpc_Value_Base64 */
  27. require_once 'Zend/XmlRpc/Value/Base64.php';
  28. /** Zend_XmlRpc_Value_Boolean */
  29. require_once 'Zend/XmlRpc/Value/Boolean.php';
  30. /** Zend_XmlRpc_Value_DateTime */
  31. require_once 'Zend/XmlRpc/Value/DateTime.php';
  32. /** Zend_XmlRpc_Value_Double */
  33. require_once 'Zend/XmlRpc/Value/Double.php';
  34. /** Zend_XmlRpc_Value_Integer */
  35. require_once 'Zend/XmlRpc/Value/Integer.php';
  36. /** Zend_XmlRpc_Value_String */
  37. require_once 'Zend/XmlRpc/Value/String.php';
  38. /** Zend_XmlRpc_Value_Nil */
  39. require_once 'Zend/XmlRpc/Value/Nil.php';
  40. /** Zend_XmlRpc_Value_Collection */
  41. require_once 'Zend/XmlRpc/Value/Collection.php';
  42. /** Zend_XmlRpc_Value_Array */
  43. require_once 'Zend/XmlRpc/Value/Array.php';
  44. /** Zend_XmlRpc_Value_Struct */
  45. require_once 'Zend/XmlRpc/Value/Struct.php';
  46. /**
  47. * Represent a native XML-RPC value entity, used as parameters for the methods
  48. * called by the Zend_XmlRpc_Client object and as the return value for those calls.
  49. *
  50. * This object as a very important static function Zend_XmlRpc_Value::getXmlRpcValue, this
  51. * function acts likes a factory for the Zend_XmlRpc_Value objects
  52. *
  53. * Using this function, users/Zend_XmlRpc_Client object can create the Zend_XmlRpc_Value objects
  54. * from PHP variables, XML string or by specifing the exact XML-RPC natvie type
  55. *
  56. * @package Zend_XmlRpc
  57. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  58. * @license http://framework.zend.com/license/new-bsd New BSD License
  59. */
  60. abstract class Zend_XmlRpc_Value
  61. {
  62. /**
  63. * The native XML-RPC representation of this object's value
  64. *
  65. * If the native type of this object is array or struct, this will be an array
  66. * of Zend_XmlRpc_Value objects
  67. */
  68. protected $_value;
  69. /**
  70. * The native XML-RPC type of this object
  71. * One of the XMLRPC_TYPE_* constants
  72. */
  73. protected $_type;
  74. /**
  75. * XML code representation of this object (will be calculated only once)
  76. */
  77. protected $_as_xml;
  78. /**
  79. * DOMElement representation of object (will be calculated only once)
  80. */
  81. protected $_as_dom;
  82. /**
  83. * Specify that the XML-RPC native type will be auto detected from a PHP variable type
  84. */
  85. const AUTO_DETECT_TYPE = 'auto_detect';
  86. /**
  87. * Specify that the XML-RPC value will be parsed out from a given XML code
  88. */
  89. const XML_STRING = 'xml';
  90. /**
  91. * All the XML-RPC native types
  92. */
  93. const XMLRPC_TYPE_I4 = 'i4';
  94. const XMLRPC_TYPE_INTEGER = 'int';
  95. const XMLRPC_TYPE_DOUBLE = 'double';
  96. const XMLRPC_TYPE_BOOLEAN = 'boolean';
  97. const XMLRPC_TYPE_STRING = 'string';
  98. const XMLRPC_TYPE_DATETIME = 'dateTime.iso8601';
  99. const XMLRPC_TYPE_BASE64 = 'base64';
  100. const XMLRPC_TYPE_ARRAY = 'array';
  101. const XMLRPC_TYPE_STRUCT = 'struct';
  102. const XMLRPC_TYPE_NIL = 'nil';
  103. /**
  104. * Get the native XML-RPC type (the type is one of the Zend_XmlRpc_Value::XMLRPC_TYPE_* constants)
  105. *
  106. * @return string
  107. */
  108. public function getType()
  109. {
  110. return $this->_type;
  111. }
  112. /**
  113. * Return the value of this object, convert the XML-RPC native value into a PHP variable
  114. *
  115. * @return mixed
  116. */
  117. abstract public function getValue();
  118. /**
  119. * Return the XML code that represent a native MXL-RPC value
  120. *
  121. * @return string
  122. */
  123. abstract public function saveXML();
  124. /**
  125. * Return DOMElement representation of object
  126. *
  127. * @return DOMElement
  128. */
  129. public function getAsDOM()
  130. {
  131. if (!$this->_as_dom) {
  132. $doc = new DOMDocument('1.0');
  133. $doc->loadXML($this->saveXML());
  134. $this->_as_dom = $doc->documentElement;
  135. }
  136. return $this->_as_dom;
  137. }
  138. protected function _stripXmlDeclaration(DOMDocument $dom)
  139. {
  140. return preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $dom->saveXML());
  141. }
  142. /**
  143. * Creates a Zend_XmlRpc_Value* object, representing a native XML-RPC value
  144. * A XmlRpcValue object can be created in 3 ways:
  145. * 1. Autodetecting the native type out of a PHP variable
  146. * (if $type is not set or equal to Zend_XmlRpc_Value::AUTO_DETECT_TYPE)
  147. * 2. By specifing the native type ($type is one of the Zend_XmlRpc_Value::XMLRPC_TYPE_* constants)
  148. * 3. From a XML string ($type is set to Zend_XmlRpc_Value::XML_STRING)
  149. *
  150. * By default the value type is autodetected according to it's PHP type
  151. *
  152. * @param mixed $value
  153. * @param Zend_XmlRpc_Value::constant $type
  154. *
  155. * @return Zend_XmlRpc_Value
  156. * @static
  157. */
  158. public static function getXmlRpcValue($value, $type = self::AUTO_DETECT_TYPE)
  159. {
  160. switch ($type) {
  161. case self::AUTO_DETECT_TYPE:
  162. // Auto detect the XML-RPC native type from the PHP type of $value
  163. return self::_phpVarToNativeXmlRpc($value);
  164. case self::XML_STRING:
  165. // Parse the XML string given in $value and get the XML-RPC value in it
  166. return self::_xmlStringToNativeXmlRpc($value);
  167. case self::XMLRPC_TYPE_I4:
  168. // fall through to the next case
  169. case self::XMLRPC_TYPE_INTEGER:
  170. return new Zend_XmlRpc_Value_Integer($value);
  171. case self::XMLRPC_TYPE_DOUBLE:
  172. return new Zend_XmlRpc_Value_Double($value);
  173. case self::XMLRPC_TYPE_BOOLEAN:
  174. return new Zend_XmlRpc_Value_Boolean($value);
  175. case self::XMLRPC_TYPE_STRING:
  176. return new Zend_XmlRpc_Value_String($value);
  177. case self::XMLRPC_TYPE_BASE64:
  178. return new Zend_XmlRpc_Value_Base64($value);
  179. case self::XMLRPC_TYPE_NIL:
  180. return new Zend_XmlRpc_Value_Nil();
  181. case self::XMLRPC_TYPE_DATETIME:
  182. return new Zend_XmlRpc_Value_DateTime($value);
  183. case self::XMLRPC_TYPE_ARRAY:
  184. return new Zend_XmlRpc_Value_Array($value);
  185. case self::XMLRPC_TYPE_STRUCT:
  186. return new Zend_XmlRpc_Value_Struct($value);
  187. default:
  188. throw new Zend_XmlRpc_Value_Exception('Given type is not a '. __CLASS__ .' constant');
  189. }
  190. }
  191. /**
  192. * Transform a PHP native variable into a XML-RPC native value
  193. *
  194. * @param mixed $value The PHP variable for convertion
  195. *
  196. * @return Zend_XmlRpc_Value
  197. * @static
  198. */
  199. private static function _phpVarToNativeXmlRpc($value)
  200. {
  201. switch (gettype($value)) {
  202. case 'object':
  203. // Check to see if it's an XmlRpc value
  204. if ($value instanceof Zend_XmlRpc_Value) {
  205. return $value;
  206. }
  207. // Otherwise, we convert the object into a struct
  208. $value = get_object_vars($value);
  209. // Break intentionally omitted
  210. case 'array':
  211. // Default native type for a PHP array (a simple numeric array) is 'array'
  212. $obj = 'Zend_XmlRpc_Value_Array';
  213. // Determine if this is an associative array
  214. if (!empty($value) && is_array($value) && (array_keys($value) !== range(0, count($value) - 1))) {
  215. $obj = 'Zend_XmlRpc_Value_Struct';
  216. }
  217. return new $obj($value);
  218. case 'integer':
  219. return new Zend_XmlRpc_Value_Integer($value);
  220. case 'double':
  221. return new Zend_XmlRpc_Value_Double($value);
  222. case 'boolean':
  223. return new Zend_XmlRpc_Value_Boolean($value);
  224. case 'NULL':
  225. case 'null':
  226. return new Zend_XmlRpc_Value_Nil();
  227. case 'string':
  228. // Fall through to the next case
  229. default:
  230. // If type isn't identified (or identified as string), it treated as string
  231. return new Zend_XmlRpc_Value_String($value);
  232. }
  233. }
  234. /**
  235. * Transform an XML string into a XML-RPC native value
  236. *
  237. * @param string|SimpleXMLElement $simple_xml A SimpleXMLElement object represent the XML string
  238. * It can be also a valid XML string for convertion
  239. *
  240. * @return Zend_XmlRpc_Value
  241. * @static
  242. */
  243. private static function _xmlStringToNativeXmlRpc($simple_xml)
  244. {
  245. if (!$simple_xml instanceof SimpleXMLElement) {
  246. try {
  247. $simple_xml = @new SimpleXMLElement($simple_xml);
  248. } catch (Exception $e) {
  249. // The given string is not a valid XML
  250. throw new Zend_XmlRpc_Value_Exception('Failed to create XML-RPC value from XML string: '.$e->getMessage(),$e->getCode());
  251. }
  252. }
  253. // Get the key (tag name) and value from the simple xml object and convert the value to an XML-RPC native value
  254. list($type, $value) = each($simple_xml);
  255. if (!$type) { // If no type was specified, the default is string
  256. $type = self::XMLRPC_TYPE_STRING;
  257. }
  258. switch ($type) {
  259. // All valid and known XML-RPC native values
  260. case self::XMLRPC_TYPE_I4:
  261. // Fall through to the next case
  262. case self::XMLRPC_TYPE_INTEGER:
  263. $xmlrpc_val = new Zend_XmlRpc_Value_Integer($value);
  264. break;
  265. case self::XMLRPC_TYPE_DOUBLE:
  266. $xmlrpc_val = new Zend_XmlRpc_Value_Double($value);
  267. break;
  268. case self::XMLRPC_TYPE_BOOLEAN:
  269. $xmlrpc_val = new Zend_XmlRpc_Value_Boolean($value);
  270. break;
  271. case self::XMLRPC_TYPE_STRING:
  272. $xmlrpc_val = new Zend_XmlRpc_Value_String($value);
  273. break;
  274. case self::XMLRPC_TYPE_DATETIME: // The value should already be in a iso8601 format
  275. $xmlrpc_val = new Zend_XmlRpc_Value_DateTime($value);
  276. break;
  277. case self::XMLRPC_TYPE_BASE64: // The value should already be base64 encoded
  278. $xmlrpc_val = new Zend_XmlRpc_Value_Base64($value ,true);
  279. break;
  280. case self::XMLRPC_TYPE_NIL: // The value should always be NULL
  281. $xmlrpc_val = new Zend_XmlRpc_Value_Nil();
  282. break;
  283. case self::XMLRPC_TYPE_ARRAY:
  284. // If the XML is valid, $value must be an SimpleXML element and contain the <data> tag
  285. if (!$value instanceof SimpleXMLElement) {
  286. throw new Zend_XmlRpc_Value_Exception('XML string is invalid for XML-RPC native '. self::XMLRPC_TYPE_ARRAY .' type');
  287. }
  288. // PHP 5.2.4 introduced a regression in how empty($xml->value)
  289. // returns; need to look for the item specifically
  290. $data = null;
  291. foreach ($value->children() as $key => $value) {
  292. if ('data' == $key) {
  293. $data = $value;
  294. break;
  295. }
  296. }
  297. if (null === $data) {
  298. throw new Zend_XmlRpc_Value_Exception('Invalid XML for XML-RPC native '. self::XMLRPC_TYPE_ARRAY .' type: ARRAY tag must contain DATA tag');
  299. }
  300. $values = array();
  301. // Parse all the elements of the array from the XML string
  302. // (simple xml element) to Zend_XmlRpc_Value objects
  303. foreach ($data->value as $element) {
  304. $values[] = self::_xmlStringToNativeXmlRpc($element);
  305. }
  306. $xmlrpc_val = new Zend_XmlRpc_Value_Array($values);
  307. break;
  308. case self::XMLRPC_TYPE_STRUCT:
  309. // If the XML is valid, $value must be an SimpleXML
  310. if ((!$value instanceof SimpleXMLElement)) {
  311. throw new Zend_XmlRpc_Value_Exception('XML string is invalid for XML-RPC native '. self::XMLRPC_TYPE_STRUCT .' type');
  312. }
  313. $values = array();
  314. // Parse all the memebers of the struct from the XML string
  315. // (simple xml element) to Zend_XmlRpc_Value objects
  316. foreach ($value->member as $member) {
  317. // @todo? If a member doesn't have a <value> tag, we don't add it to the struct
  318. // Maybe we want to throw an exception here ?
  319. if ((!$member->value instanceof SimpleXMLElement) || empty($member->value)) {
  320. continue;
  321. //throw new Zend_XmlRpc_Value_Exception('Member of the '. self::XMLRPC_TYPE_STRUCT .' XML-RPC native type must contain a VALUE tag');
  322. }
  323. $values[(string)$member->name] = self::_xmlStringToNativeXmlRpc($member->value);
  324. }
  325. $xmlrpc_val = new Zend_XmlRpc_Value_Struct($values);
  326. break;
  327. default:
  328. throw new Zend_XmlRpc_Value_Exception('Value type \''. $type .'\' parsed from the XML string is not a known XML-RPC native type');
  329. break;
  330. }
  331. $xmlrpc_val->_setXML($simple_xml->asXML());
  332. return $xmlrpc_val;
  333. }
  334. private function _setXML($xml)
  335. {
  336. $this->_as_xml = $xml;
  337. }
  338. }