2
0

Value.php 14 KB

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