File indexing completed on 2024-05-26 06:03:04

0001 <?php
0002 
0003 /**
0004  * Zend Framework
0005  *
0006  * LICENSE
0007  *
0008  * This source file is subject to the new BSD license that is bundled
0009  * with this package in the file LICENSE.txt.
0010  * It is also available through the world-wide-web at this URL:
0011  * http://framework.zend.com/license/new-bsd
0012  * If you did not receive a copy of the license and are unable to
0013  * obtain it through the world-wide-web, please send an email
0014  * to license@zend.com so we can send you a copy immediately.
0015  *
0016  * @category   Zend
0017  * @package    Zend_Gdata
0018  * @subpackage Gapps
0019  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0020  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0021  * @version    $Id$
0022  */
0023 
0024 
0025 /**
0026  * Zend_Exception
0027  */
0028 // require_once 'Zend/Exception.php';
0029 
0030 /**
0031  * Zend_Gdata_Gapps_Error
0032  */
0033 // require_once 'Zend/Gdata/Gapps/Error.php';
0034 
0035 /** @see Zend_Xml_Security */
0036 // require_once 'Zend/Xml/Security.php';
0037 
0038 /**
0039  * Gdata Gapps Exception class. This is thrown when an
0040  * AppsForYourDomainErrors message is received from the Google Apps
0041  * servers.
0042  *
0043  * Several different errors may be represented by this exception. For a list
0044  * of error codes available, see getErrorCode.
0045  *
0046  * @category   Zend
0047  * @package    Zend_Gdata
0048  * @subpackage Gapps
0049  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0050  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0051  */
0052 class Zend_Gdata_Gapps_ServiceException extends Zend_Exception
0053 {
0054 
0055     protected $_rootElement = "AppsForYourDomainErrors";
0056 
0057     /**
0058      * Array of Zend_Gdata_Error objects indexed by error code.
0059      *
0060      * @var array
0061      */
0062     protected $_errors = array();
0063 
0064     /**
0065      * Create a new ServiceException.
0066      *
0067      * @return array An array containing a collection of
0068      *          Zend_Gdata_Gapps_Error objects.
0069      */
0070     public function __construct($errors = null) {
0071         parent::__construct("Server errors encountered");
0072         if ($errors !== null) {
0073             $this->setErrors($errors);
0074         }
0075     }
0076 
0077     /**
0078      * Add a single Error object to the list of errors received by the
0079      * server.
0080      *
0081      * @param Zend_Gdata_Gapps_Error $error An instance of an error returned
0082      *          by the server. The error's errorCode must be set.
0083      * @throws Zend_Gdata_App_Exception
0084      */
0085     public function addError($error) {
0086         // Make sure that we don't try to index an error that doesn't
0087         // contain an index value.
0088         if ($error->getErrorCode() == null) {
0089             // require_once 'Zend/Gdata/App/Exception.php';
0090             throw new Zend_Gdata_App_Exception("Error encountered without corresponding error code.");
0091         }
0092 
0093         $this->_errors[$error->getErrorCode()] = $error;
0094     }
0095 
0096     /**
0097      * Set the list of errors as sent by the server inside of an
0098      * AppsForYourDomainErrors tag.
0099      *
0100      * @param array $array An associative array containing a collection of
0101      *          Zend_Gdata_Gapps_Error objects. All errors must have their
0102      *          errorCode value set.
0103      * @throws Zend_Gdata_App_Exception
0104      */
0105     public function setErrors($array) {
0106         $this->_errors = array();
0107         foreach ($array as $error) {
0108             $this->addError($error);
0109         }
0110     }
0111 
0112     /**
0113      * Get the list of errors as sent by the server inside of an
0114      * AppsForYourDomainErrors tag.
0115      *
0116      * @return array An associative array containing a collection of
0117      *          Zend_Gdata_Gapps_Error objects, indexed by error code.
0118      */
0119     public function getErrors() {
0120         return $this->_errors;
0121     }
0122 
0123     /**
0124      * Return the Error object associated with a specific error code.
0125      *
0126      * @return Zend_Gdata_Gapps_Error The Error object requested, or null
0127      *              if not found.
0128      */
0129     public function getError($errorCode) {
0130         if (array_key_exists($errorCode, $this->_errors)) {
0131             $result = $this->_errors[$errorCode];
0132             return $result;
0133         } else {
0134             return null;
0135         }
0136     }
0137 
0138     /**
0139      * Check whether or not a particular error code was returned by the
0140      * server.
0141      *
0142      * @param integer $errorCode The error code to check against.
0143      * @return boolean Whether or not the supplied error code was returned
0144      *          by the server.
0145      */
0146     public function hasError($errorCode) {
0147         return array_key_exists($errorCode, $this->_errors);
0148     }
0149 
0150     /**
0151      * Import an AppsForYourDomain error from XML.
0152      *
0153      * @param string $string The XML data to be imported
0154      * @return Zend_Gdata_Gapps_ServiceException Provides a fluent interface.
0155      * @throws Zend_Gdata_App_Exception
0156      */
0157     public function importFromString($string) {
0158         if ($string) {
0159             // Check to see if an AppsForYourDomainError exists
0160             //
0161             // track_errors is temporarily enabled so that if an error
0162             // occurs while parsing the XML we can append it to an
0163             // exception by referencing $php_errormsg
0164             @ini_set('track_errors', 1);
0165             $doc = new DOMDocument();
0166             $doc = @Zend_Xml_Security::scan($string, $doc);
0167             @ini_restore('track_errors');
0168 
0169             if (!$doc) {
0170                 // require_once 'Zend/Gdata/App/Exception.php';
0171                 // $php_errormsg is automatically generated by PHP if
0172                 // an error occurs while calling loadXML(), above.
0173                 throw new Zend_Gdata_App_Exception("DOMDocument cannot parse XML: $php_errormsg");
0174             }
0175 
0176             // Ensure that the outermost node is an AppsForYourDomain error.
0177             // If it isn't, something has gone horribly wrong.
0178             $rootElement = $doc->getElementsByTagName($this->_rootElement)->item(0);
0179             if (!$rootElement) {
0180                 // require_once 'Zend/Gdata/App/Exception.php';
0181                 throw new Zend_Gdata_App_Exception('No root <' . $this->_rootElement . '> element found, cannot parse feed.');
0182             }
0183 
0184             foreach ($rootElement->childNodes as $errorNode) {
0185                 if (!($errorNode instanceof DOMText)) {
0186                     $error = new Zend_Gdata_Gapps_Error();
0187                     $error->transferFromDom($errorNode);
0188                     $this->addError($error);
0189                 }
0190             }
0191             return $this;
0192         } else {
0193             // require_once 'Zend/Gdata/App/Exception.php';
0194             throw new Zend_Gdata_App_Exception('XML passed to transferFromXML cannot be null');
0195         }
0196 
0197     }
0198 
0199     /**
0200      * Get a human readable version of this exception.
0201      *
0202      * @return string
0203      */
0204     public function __toString() {
0205         $result = "The server encountered the following errors processing the request:";
0206         foreach ($this->_errors as $error) {
0207             $result .= "\n" . $error->__toString();
0208         }
0209         return $result;
0210     }
0211 }