/library/Zend/Service/Nirvanix/Response.php

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