Magento file: lib/Zend/XmlRpc/Value.php

Please LogIn to Reply!

Post By: admin | 26-01-2012 06:35 Reply

Magento folder: lib/Zend/XmlRpc/
Magento file: lib/Zend/XmlRpc/Value.php

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

Post By: Magento Support Team | 04-07-2012 14:22

http://magentosupportteam.com

`Reply Or Comment!
Please LogIn to Reply!