Value.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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-2009 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. /**
  23. * Represent a native XML-RPC value entity, used as parameters for the methods
  24. * called by the Zend_XmlRpc_Client object and as the return value for those calls.
  25. *
  26. * This object as a very important static function Zend_XmlRpc_Value::getXmlRpcValue, this
  27. * function acts likes a factory for the Zend_XmlRpc_Value objects
  28. *
  29. * Using this function, users/Zend_XmlRpc_Client object can create the Zend_XmlRpc_Value objects
  30. * from PHP variables, XML string or by specifing the exact XML-RPC natvie type
  31. *
  32. * @package Zend_XmlRpc
  33. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. abstract class Zend_XmlRpc_Value
  37. {
  38. /**
  39. * The native XML-RPC representation of this object's value
  40. *
  41. * If the native type of this object is array or struct, this will be an array
  42. * of Zend_XmlRpc_Value objects
  43. */
  44. protected $_value;
  45. /**
  46. * The native XML-RPC type of this object
  47. * One of the XMLRPC_TYPE_* constants
  48. */
  49. protected $_type;
  50. /**
  51. * XML code representation of this object (will be calculated only once)
  52. */
  53. protected $_xml;
  54. /**
  55. * @var Zend_XmlRpc_Generator_Abstract
  56. */
  57. protected static $_generator;
  58. /**
  59. * Specify that the XML-RPC native type will be auto detected from a PHP variable type
  60. */
  61. const AUTO_DETECT_TYPE = 'auto_detect';
  62. /**
  63. * Specify that the XML-RPC value will be parsed out from a given XML code
  64. */
  65. const XML_STRING = 'xml';
  66. /**
  67. * All the XML-RPC native types
  68. */
  69. const XMLRPC_TYPE_I4 = 'i4';
  70. const XMLRPC_TYPE_INTEGER = 'int';
  71. const XMLRPC_TYPE_I8 = 'i8';
  72. const XMLRPC_TYPE_APACHEI8 = 'ex:i8';
  73. const XMLRPC_TYPE_DOUBLE = 'double';
  74. const XMLRPC_TYPE_BOOLEAN = 'boolean';
  75. const XMLRPC_TYPE_STRING = 'string';
  76. const XMLRPC_TYPE_DATETIME = 'dateTime.iso8601';
  77. const XMLRPC_TYPE_BASE64 = 'base64';
  78. const XMLRPC_TYPE_ARRAY = 'array';
  79. const XMLRPC_TYPE_STRUCT = 'struct';
  80. const XMLRPC_TYPE_NIL = 'nil';
  81. const XMLRPC_TYPE_APACHENIL = 'ex:nil';
  82. /**
  83. * Get the native XML-RPC type (the type is one of the Zend_XmlRpc_Value::XMLRPC_TYPE_* constants)
  84. *
  85. * @return string
  86. */
  87. public function getType()
  88. {
  89. return $this->_type;
  90. }
  91. /**
  92. * Get XML generator instance
  93. *
  94. * @return Zend_XmlRpc_Generator_Abstract
  95. */
  96. public static function getGenerator()
  97. {
  98. if (!self::$_generator) {
  99. require_once 'Zend/XmlRpc/Generator/DOMDocument.php';
  100. self::$_generator = new Zend_XmlRpc_Generator_DOMDocument();
  101. }
  102. return self::$_generator;
  103. }
  104. /**
  105. * Sets XML generator instance
  106. *
  107. * @param Zend_XmlRpc_Generator_Abstract $generator
  108. * @return void
  109. */
  110. public static function setGenerator(Zend_XmlRpc_Generator_Abstract $generator)
  111. {
  112. self::$_generator = $generator;
  113. }
  114. /**
  115. * Changes the encoding of the generator
  116. *
  117. * @param string $encoding
  118. * @return void
  119. */
  120. public static function setEncoding($encoding)
  121. {
  122. $generator = self::getGenerator();
  123. $newGenerator = new $generator($encoding);
  124. self::setGenerator($newGenerator);
  125. }
  126. /**
  127. * Return the value of this object, convert the XML-RPC native value into a PHP variable
  128. *
  129. * @return mixed
  130. */
  131. abstract public function getValue();
  132. /**
  133. * Return the XML code that represent a native MXL-RPC value
  134. *
  135. * @return string
  136. */
  137. public function saveXml()
  138. {
  139. if (!$this->_xml) {
  140. $this->generateXml();
  141. }
  142. return $this->_xml;
  143. }
  144. /**
  145. * Generate XML code that represent a native XML/RPC value
  146. *
  147. * @return void
  148. */
  149. public function generateXml()
  150. {
  151. if (!$this->_xml) {
  152. $this->_generateXml();
  153. }
  154. }
  155. /**
  156. * Creates a Zend_XmlRpc_Value* object, representing a native XML-RPC value
  157. * A XmlRpcValue object can be created in 3 ways:
  158. * 1. Autodetecting the native type out of a PHP variable
  159. * (if $type is not set or equal to Zend_XmlRpc_Value::AUTO_DETECT_TYPE)
  160. * 2. By specifing the native type ($type is one of the Zend_XmlRpc_Value::XMLRPC_TYPE_* constants)
  161. * 3. From a XML string ($type is set to Zend_XmlRpc_Value::XML_STRING)
  162. *
  163. * By default the value type is autodetected according to it's PHP type
  164. *
  165. * @param mixed $value
  166. * @param Zend_XmlRpc_Value::constant $type
  167. *
  168. * @return Zend_XmlRpc_Value
  169. * @static
  170. */
  171. public static function getXmlRpcValue($value, $type = self::AUTO_DETECT_TYPE)
  172. {
  173. switch ($type) {
  174. case self::AUTO_DETECT_TYPE:
  175. // Auto detect the XML-RPC native type from the PHP type of $value
  176. return self::_phpVarToNativeXmlRpc($value);
  177. case self::XML_STRING:
  178. // Parse the XML string given in $value and get the XML-RPC value in it
  179. return self::_xmlStringToNativeXmlRpc($value);
  180. case self::XMLRPC_TYPE_I4:
  181. // fall through to the next case
  182. case self::XMLRPC_TYPE_INTEGER:
  183. require_once 'Zend/XmlRpc/Value/Integer.php';
  184. return new Zend_XmlRpc_Value_Integer($value);
  185. case self::XMLRPC_TYPE_I8:
  186. // fall through to the next case
  187. case self::XMLRPC_TYPE_APACHEI8:
  188. require_once 'Zend/XmlRpc/Value/BigInteger.php';
  189. return new Zend_XmlRpc_Value_BigInteger($value);
  190. case self::XMLRPC_TYPE_DOUBLE:
  191. require_once 'Zend/XmlRpc/Value/Double.php';
  192. return new Zend_XmlRpc_Value_Double($value);
  193. case self::XMLRPC_TYPE_BOOLEAN:
  194. require_once 'Zend/XmlRpc/Value/Boolean.php';
  195. return new Zend_XmlRpc_Value_Boolean($value);
  196. case self::XMLRPC_TYPE_STRING:
  197. require_once 'Zend/XmlRpc/Value/String.php';
  198. return new Zend_XmlRpc_Value_String($value);
  199. case self::XMLRPC_TYPE_BASE64:
  200. require_once 'Zend/XmlRpc/Value/Base64.php';
  201. return new Zend_XmlRpc_Value_Base64($value);
  202. case self::XMLRPC_TYPE_NIL:
  203. // fall through to the next case
  204. case self::XMLRPC_TYPE_APACHENIL:
  205. require_once 'Zend/XmlRpc/Value/Nil.php';
  206. return new Zend_XmlRpc_Value_Nil();
  207. case self::XMLRPC_TYPE_DATETIME:
  208. require_once 'Zend/XmlRpc/Value/DateTime.php';
  209. return new Zend_XmlRpc_Value_DateTime($value);
  210. case self::XMLRPC_TYPE_ARRAY:
  211. require_once 'Zend/XmlRpc/Value/Array.php';
  212. return new Zend_XmlRpc_Value_Array($value);
  213. case self::XMLRPC_TYPE_STRUCT:
  214. require_once 'Zend/XmlRpc/Value/Struct.php';
  215. return new Zend_XmlRpc_Value_Struct($value);
  216. default:
  217. require_once 'Zend/XmlRpc/Value/Exception.php';
  218. throw new Zend_XmlRpc_Value_Exception('Given type is not a '. __CLASS__ .' constant');
  219. }
  220. }
  221. /**
  222. * Transform a PHP native variable into a XML-RPC native value
  223. *
  224. * @param mixed $value The PHP variable for convertion
  225. *
  226. * @return Zend_XmlRpc_Value
  227. * @static
  228. */
  229. protected static function _phpVarToNativeXmlRpc($value)
  230. {
  231. switch (gettype($value)) {
  232. case 'object':
  233. // Check to see if it's an XmlRpc value
  234. if ($value instanceof Zend_XmlRpc_Value) {
  235. return $value;
  236. }
  237. if ($value instanceof Zend_Crypt_Math_BigInteger) {
  238. require_once 'Zend/XmlRpc/Value/BigInteger.php';
  239. return new Zend_XmlRpc_Value_BigInteger($value);
  240. }
  241. if ($value instanceof Zend_Date or $value instanceof DateTime) {
  242. require_once 'Zend/XmlRpc/Value/DateTime.php';
  243. return new Zend_XmlRpc_Value_DateTime($value);
  244. }
  245. // Otherwise, we convert the object into a struct
  246. $value = get_object_vars($value);
  247. // Break intentionally omitted
  248. case 'array':
  249. // Default native type for a PHP array (a simple numeric array) is 'array'
  250. require_once 'Zend/XmlRpc/Value/Array.php';
  251. $obj = 'Zend_XmlRpc_Value_Array';
  252. // Determine if this is an associative array
  253. if (!empty($value) && is_array($value) && (array_keys($value) !== range(0, count($value) - 1))) {
  254. require_once 'Zend/XmlRpc/Value/Struct.php';
  255. $obj = 'Zend_XmlRpc_Value_Struct';
  256. }
  257. return new $obj($value);
  258. case 'integer':
  259. require_once 'Zend/XmlRpc/Value/Integer.php';
  260. return new Zend_XmlRpc_Value_Integer($value);
  261. case 'double':
  262. require_once 'Zend/XmlRpc/Value/Double.php';
  263. return new Zend_XmlRpc_Value_Double($value);
  264. case 'boolean':
  265. require_once 'Zend/XmlRpc/Value/Boolean.php';
  266. return new Zend_XmlRpc_Value_Boolean($value);
  267. case 'NULL':
  268. case 'null':
  269. require_once 'Zend/XmlRpc/Value/Nil.php';
  270. return new Zend_XmlRpc_Value_Nil();
  271. case 'string':
  272. // Fall through to the next case
  273. default:
  274. // If type isn't identified (or identified as string), it treated as string
  275. require_once 'Zend/XmlRpc/Value/String.php';
  276. return new Zend_XmlRpc_Value_String($value);
  277. }
  278. }
  279. /**
  280. * Transform an XML string into a XML-RPC native value
  281. *
  282. * @param string|SimpleXMLElement $xml A SimpleXMLElement object represent the XML string
  283. * It can be also a valid XML string for convertion
  284. *
  285. * @return Zend_XmlRpc_Value
  286. * @static
  287. */
  288. protected static function _xmlStringToNativeXmlRpc($xml)
  289. {
  290. self::_createSimpleXMLElement($xml);
  291. self::_extractTypeAndValue($xml, $type, $value);
  292. switch ($type) {
  293. // All valid and known XML-RPC native values
  294. case self::XMLRPC_TYPE_I4:
  295. // Fall through to the next case
  296. case self::XMLRPC_TYPE_INTEGER:
  297. require_once 'Zend/XmlRpc/Value/Integer.php';
  298. $xmlrpcValue = new Zend_XmlRpc_Value_Integer($value);
  299. break;
  300. case self::XMLRPC_TYPE_APACHEI8:
  301. // Fall through to the next case
  302. case self::XMLRPC_TYPE_I8:
  303. require_once 'Zend/XmlRpc/Value/BigInteger.php';
  304. $xmlrpcValue = new Zend_XmlRpc_Value_BigInteger($value);
  305. break;
  306. case self::XMLRPC_TYPE_DOUBLE:
  307. require_once 'Zend/XmlRpc/Value/Double.php';
  308. $xmlrpcValue = new Zend_XmlRpc_Value_Double($value);
  309. break;
  310. case self::XMLRPC_TYPE_BOOLEAN:
  311. require_once 'Zend/XmlRpc/Value/Boolean.php';
  312. $xmlrpcValue = new Zend_XmlRpc_Value_Boolean($value);
  313. break;
  314. case self::XMLRPC_TYPE_STRING:
  315. require_once 'Zend/XmlRpc/Value/String.php';
  316. $xmlrpcValue = new Zend_XmlRpc_Value_String($value);
  317. break;
  318. case self::XMLRPC_TYPE_DATETIME: // The value should already be in a iso8601 format
  319. require_once 'Zend/XmlRpc/Value/DateTime.php';
  320. $xmlrpcValue = new Zend_XmlRpc_Value_DateTime($value);
  321. break;
  322. case self::XMLRPC_TYPE_BASE64: // The value should already be base64 encoded
  323. require_once 'Zend/XmlRpc/Value/Base64.php';
  324. $xmlrpcValue = new Zend_XmlRpc_Value_Base64($value, true);
  325. break;
  326. case self::XMLRPC_TYPE_NIL:
  327. // Fall through to the next case
  328. case self::XMLRPC_TYPE_APACHENIL:
  329. // The value should always be NULL
  330. require_once 'Zend/XmlRpc/Value/Nil.php';
  331. $xmlrpcValue = new Zend_XmlRpc_Value_Nil();
  332. break;
  333. case self::XMLRPC_TYPE_ARRAY:
  334. // PHP 5.2.4 introduced a regression in how empty($xml->value)
  335. // returns; need to look for the item specifically
  336. $data = null;
  337. foreach ($value->children() as $key => $value) {
  338. if ('data' == $key) {
  339. $data = $value;
  340. break;
  341. }
  342. }
  343. if (null === $data) {
  344. require_once 'Zend/XmlRpc/Value/Exception.php';
  345. throw new Zend_XmlRpc_Value_Exception('Invalid XML for XML-RPC native '. self::XMLRPC_TYPE_ARRAY .' type: ARRAY tag must contain DATA tag');
  346. }
  347. $values = array();
  348. // Parse all the elements of the array from the XML string
  349. // (simple xml element) to Zend_XmlRpc_Value objects
  350. foreach ($data->value as $element) {
  351. $values[] = self::_xmlStringToNativeXmlRpc($element);
  352. }
  353. require_once 'Zend/XmlRpc/Value/Array.php';
  354. $xmlrpcValue = new Zend_XmlRpc_Value_Array($values);
  355. break;
  356. case self::XMLRPC_TYPE_STRUCT:
  357. $values = array();
  358. // Parse all the memebers of the struct from the XML string
  359. // (simple xml element) to Zend_XmlRpc_Value objects
  360. foreach ($value->member as $member) {
  361. // @todo? If a member doesn't have a <value> tag, we don't add it to the struct
  362. // Maybe we want to throw an exception here ?
  363. if (!isset($member->value) or !isset($member->name)) {
  364. continue;
  365. //throw new Zend_XmlRpc_Value_Exception('Member of the '. self::XMLRPC_TYPE_STRUCT .' XML-RPC native type must contain a VALUE tag');
  366. }
  367. $values[(string)$member->name] = self::_xmlStringToNativeXmlRpc($member->value);
  368. }
  369. require_once 'Zend/XmlRpc/Value/Struct.php';
  370. $xmlrpcValue = new Zend_XmlRpc_Value_Struct($values);
  371. break;
  372. default:
  373. require_once 'Zend/XmlRpc/Value/Exception.php';
  374. throw new Zend_XmlRpc_Value_Exception('Value type \''. $type .'\' parsed from the XML string is not a known XML-RPC native type');
  375. break;
  376. }
  377. $xmlrpcValue->_setXML($xml->asXML());
  378. return $xmlrpcValue;
  379. }
  380. protected static function _createSimpleXMLElement(&$xml)
  381. {
  382. if ($xml instanceof SimpleXMLElement) {
  383. return;
  384. }
  385. try {
  386. $xml = new SimpleXMLElement($xml);
  387. } catch (Exception $e) {
  388. // The given string is not a valid XML
  389. require_once 'Zend/XmlRpc/Value/Exception.php';
  390. throw new Zend_XmlRpc_Value_Exception('Failed to create XML-RPC value from XML string: ' . $e->getMessage(), $e->getCode(), $e);
  391. }
  392. }
  393. /**
  394. * Extract XML/RPC type and value from SimpleXMLElement object
  395. *
  396. * @param SimpleXMLElement $xml
  397. * @param string &$type Type bind variable
  398. * @param string &$value Value bind variable
  399. * @return void
  400. */
  401. protected static function _extractTypeAndValue(SimpleXMLElement $xml, &$type, &$value)
  402. {
  403. list($type, $value) = each($xml);
  404. if (!$type and $value === null) {
  405. $namespaces = array('ex' => 'http://ws.apache.org/xmlrpc/namespaces/extensions');
  406. foreach ($namespaces as $namespaceName => $namespaceUri) {
  407. $namespaceXml = $xml->children($namespaceUri);
  408. list($type, $value) = each($namespaceXml);
  409. if ($type !== null) {
  410. $type = $namespaceName . ':' . $type;
  411. break;
  412. }
  413. }
  414. }
  415. // If no type was specified, the default is string
  416. if (!$type) {
  417. $type = self::XMLRPC_TYPE_STRING;
  418. }
  419. }
  420. /**
  421. * @param $xml
  422. * @return void
  423. */
  424. protected function _setXML($xml)
  425. {
  426. $this->_xml = $this->getGenerator()->stripDeclaration($xml);
  427. }
  428. }