2
0

Response.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_Service
  17. * @subpackage Nirvanix
  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. */
  21. /**
  22. * This class decorates a SimpleXMLElement parsed from a Nirvanix web service
  23. * response. It is primarily exists to provide a convenience feature that
  24. * throws an exception when <ResponseCode> contains an error.
  25. *
  26. * @category Zend
  27. * @package Zend_Service
  28. * @subpackage Nirvanix
  29. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Service_Nirvanix_Response
  33. {
  34. /**
  35. * SimpleXMLElement parsed from Nirvanix web service response.
  36. *
  37. * @var SimpleXMLElement
  38. */
  39. protected $_sxml;
  40. /**
  41. * Class constructor. Parse the XML response from a Nirvanix method
  42. * call into a decorated SimpleXMLElement element.
  43. *
  44. * @param string $xml XML response string from Nirvanix
  45. * @throws Zend_Service_Nirvanix_Exception
  46. */
  47. public function __construct($xml)
  48. {
  49. $this->_sxml = @simplexml_load_string($xml);
  50. if (! $this->_sxml instanceof SimpleXMLElement) {
  51. $this->_throwException("XML could not be parsed from response: $xml");
  52. }
  53. $name = $this->_sxml->getName();
  54. if ($name != 'Response') {
  55. $this->_throwException("Expected XML element Response, got $name");
  56. }
  57. $code = (int)$this->_sxml->ResponseCode;
  58. if ($code != 0) {
  59. $msg = (string)$this->_sxml->ErrorMessage;
  60. $this->_throwException($msg, $code);
  61. }
  62. }
  63. /**
  64. * Return the SimpleXMLElement representing this response
  65. * for direct access.
  66. *
  67. * @return SimpleXMLElement
  68. */
  69. public function getSxml()
  70. {
  71. return $this->_sxml;
  72. }
  73. /**
  74. * Delegate undefined properties to the decorated SimpleXMLElement.
  75. *
  76. * @param string $offset Undefined property name
  77. * @return mixed
  78. */
  79. public function __get($offset)
  80. {
  81. return $this->_sxml->$offset;
  82. }
  83. /**
  84. * Delegate undefined methods to the decorated SimpleXMLElement.
  85. *
  86. * @param string $offset Underfined method name
  87. * @param array $args Method arguments
  88. * @return mixed
  89. */
  90. public function __call($method, $args)
  91. {
  92. return call_user_func_array(array($this->_sxml, $method), $args);
  93. }
  94. /**
  95. * Throw an exception. This method exists to only contain the
  96. * lazy-require() of the exception class.
  97. *
  98. * @param string $message Error message
  99. * @param integer $code Error code
  100. * @throws Zend_Service_Nirvanix_Exception
  101. * @return void
  102. */
  103. protected function _throwException($message, $code = null)
  104. {
  105. /**
  106. * @see Zend_Service_Nirvanix_Exception
  107. */
  108. require_once 'Zend/Service/Nirvanix/Exception.php';
  109. throw new Zend_Service_Nirvanix_Exception($message, $code);
  110. }
  111. }