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