File indexing completed on 2024-12-22 05:36:58

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Rest
0017  * @subpackage Client
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 // require_once 'Zend/Xml/Security.php';
0024 
0025 /**
0026  * @category   Zend
0027  * @package    Zend_Rest
0028  * @subpackage Client
0029  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0030  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0031  */
0032 class Zend_Rest_Client_Result implements IteratorAggregate {
0033     /**
0034      * @var SimpleXMLElement
0035      */
0036     protected $_sxml;
0037 
0038     /**
0039      * error information
0040      * @var string
0041      */
0042     protected $_errstr;
0043 
0044     /**
0045      * Constructor
0046      *
0047      * @param string $data XML Result
0048      * @return void
0049      */
0050     public function __construct($data)
0051     {
0052         set_error_handler(array($this, 'handleXmlErrors'));
0053         $this->_sxml = Zend_Xml_Security::scan($data); 
0054         restore_error_handler();
0055         if($this->_sxml === false) {
0056             if ($this->_errstr === null) {
0057                 $message = "An error occured while parsing the REST response with simplexml.";
0058             } else {
0059                 $message = "REST Response Error: " . $this->_errstr;
0060                 $this->_errstr = null;
0061             }
0062             // require_once "Zend/Rest/Client/Result/Exception.php";
0063             throw new Zend_Rest_Client_Result_Exception($message);
0064         }
0065     }
0066 
0067     /**
0068      * Temporary error handler for parsing REST responses.
0069      *
0070      * @param int    $errno
0071      * @param string $errstr
0072      * @param string $errfile
0073      * @param string $errline
0074      * @param array  $errcontext
0075      * @return true
0076      */
0077     public function handleXmlErrors($errno, $errstr, $errfile = null, $errline = null, array $errcontext = null)
0078     {
0079         $this->_errstr = $errstr;
0080         return true;
0081     }
0082 
0083     /**
0084      * Casts a SimpleXMLElement to its appropriate PHP value
0085      *
0086      * @param SimpleXMLElement $value
0087      * @return mixed
0088      */
0089     public function toValue(SimpleXMLElement $value)
0090     {
0091         $node = dom_import_simplexml($value);
0092         return $node->nodeValue;
0093     }
0094 
0095     /**
0096      * Get Property Overload
0097      *
0098      * @param string $name
0099      * @return null|SimpleXMLElement|array Null if not found, SimpleXMLElement if only one value found, array of Zend_Rest_Client_Result objects otherwise
0100      */
0101     public function __get($name)
0102     {
0103         if (isset($this->_sxml->{$name})) {
0104             return $this->_sxml->{$name};
0105         }
0106 
0107         $result = $this->_sxml->xpath("//$name");
0108         $count  = count($result);
0109 
0110         if ($count == 0) {
0111             return null;
0112         } elseif ($count == 1) {
0113             return $result[0];
0114         } else {
0115             return $result;
0116         }
0117     }
0118 
0119     /**
0120      * Cast properties to PHP values
0121      *
0122      * For arrays, loops through each element and casts to a value as well.
0123      *
0124      * @param string $method
0125      * @param array $args
0126      * @return mixed
0127      */
0128     public function __call($method, $args)
0129     {
0130         if (null !== ($value = $this->__get($method))) {
0131             if (!is_array($value)) {
0132                 return $this->toValue($value);
0133             } else {
0134                 $return = array();
0135                 foreach ($value as $element) {
0136                     $return[] = $this->toValue($element);
0137                 }
0138                 return $return;
0139             }
0140         }
0141 
0142         return null;
0143     }
0144 
0145 
0146     /**
0147      * Isset Overload
0148      *
0149      * @param string $name
0150      * @return boolean
0151      */
0152     public function __isset($name)
0153     {
0154         if (isset($this->_sxml->{$name})) {
0155             return true;
0156         }
0157 
0158         $result = $this->_sxml->xpath("//$name");
0159 
0160         if (sizeof($result) > 0) {
0161             return true;
0162         }
0163 
0164         return false;
0165     }
0166 
0167     /**
0168      * Implement IteratorAggregate::getIterator()
0169      *
0170      * @return SimpleXMLIterator
0171      */
0172     public function getIterator()
0173     {
0174         return $this->_sxml;
0175     }
0176 
0177     /**
0178      * Get Request Status
0179      *
0180      * @return boolean
0181      */
0182     public function getStatus()
0183     {
0184         $status = $this->_sxml->xpath('//status/text()');
0185         if ( !isset($status[0]) ) return false;
0186         
0187         $status = strtolower($status[0]);
0188 
0189         if (ctype_alpha($status) && $status == 'success') {
0190             return true;
0191         } elseif (ctype_alpha($status) && $status != 'success') {
0192             return false;
0193         } else {
0194             return (bool) $status;
0195         }
0196     }
0197 
0198     public function isError()
0199     {
0200         $status = $this->getStatus();
0201         if ($status) {
0202             return false;
0203         } else {
0204             return true;
0205         }
0206     }
0207 
0208     public function isSuccess()
0209     {
0210         $status = $this->getStatus();
0211         if ($status) {
0212             return true;
0213         } else {
0214             return false;
0215         }
0216     }
0217 
0218     /**
0219      * toString overload
0220      *
0221      * Be sure to only call this when the result is a single value!
0222      *
0223      * @return string
0224      */
0225     public function __toString()
0226     {
0227         if (!$this->getStatus()) {
0228             $message = $this->_sxml->xpath('//message');
0229             return (string) $message[0];
0230         } else {
0231             $result = $this->_sxml->xpath('//response');
0232             if (sizeof($result) > 1) {
0233                 return (string) "An error occured.";
0234             } else {
0235                 return (string) $result[0];
0236             }
0237         }
0238     }
0239 }