ServiceException.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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_Gdata
  17. * @subpackage Gapps
  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$
  21. */
  22. /**
  23. * Zend_Exception
  24. */
  25. require_once 'Zend/Exception.php';
  26. /**
  27. * Zend_Gdata_Gapps_Error
  28. */
  29. require_once 'Zend/Gdata/Gapps/Error.php';
  30. /**
  31. * Gdata Gapps Exception class. This is thrown when an
  32. * AppsForYourDomainErrors message is received from the Google Apps
  33. * servers.
  34. *
  35. * Several different errors may be represented by this exception. For a list
  36. * of error codes available, see getErrorCode.
  37. *
  38. * @category Zend
  39. * @package Zend_Gdata
  40. * @subpackage Gapps
  41. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. */
  44. class Zend_Gdata_Gapps_ServiceException extends Zend_Exception
  45. {
  46. protected $_rootElement = "AppsForYourDomainErrors";
  47. /**
  48. * Array of Zend_Gdata_Error objects indexed by error code.
  49. *
  50. * @var array
  51. */
  52. protected $_errors = array();
  53. /**
  54. * Create a new ServiceException.
  55. *
  56. * @return array An array containing a collection of
  57. * Zend_Gdata_Gapps_Error objects.
  58. */
  59. public function __construct($errors = null) {
  60. parent::__construct("Server errors encountered");
  61. if ($errors !== null) {
  62. $this->setErrors($errors);
  63. }
  64. }
  65. /**
  66. * Add a single Error object to the list of errors received by the
  67. * server.
  68. *
  69. * @param Zend_Gdata_Gapps_Error $error An instance of an error returned
  70. * by the server. The error's errorCode must be set.
  71. * @throws Zend_Gdata_App_Exception
  72. */
  73. public function addError($error) {
  74. // Make sure that we don't try to index an error that doesn't
  75. // contain an index value.
  76. if ($error->getErrorCode() == null) {
  77. require_once 'Zend/Gdata/App/Exception.php';
  78. throw new Zend_Gdata_App_Exception("Error encountered without corresponding error code.");
  79. }
  80. $this->_errors[$error->getErrorCode()] = $error;
  81. }
  82. /**
  83. * Set the list of errors as sent by the server inside of an
  84. * AppsForYourDomainErrors tag.
  85. *
  86. * @param array $array An associative array containing a collection of
  87. * Zend_Gdata_Gapps_Error objects. All errors must have their
  88. * errorCode value set.
  89. * @throws Zend_Gdata_App_Exception
  90. */
  91. public function setErrors($array) {
  92. $this->_errors = array();
  93. foreach ($array as $error) {
  94. $this->addError($error);
  95. }
  96. }
  97. /**
  98. * Get the list of errors as sent by the server inside of an
  99. * AppsForYourDomainErrors tag.
  100. *
  101. * @return array An associative array containing a collection of
  102. * Zend_Gdata_Gapps_Error objects, indexed by error code.
  103. */
  104. public function getErrors() {
  105. return $this->_errors;
  106. }
  107. /**
  108. * Return the Error object associated with a specific error code.
  109. *
  110. * @return Zend_Gdata_Gapps_Error The Error object requested, or null
  111. * if not found.
  112. */
  113. public function getError($errorCode) {
  114. if (array_key_exists($errorCode, $this->_errors)) {
  115. $result = $this->_errors[$errorCode];
  116. return $result;
  117. } else {
  118. return null;
  119. }
  120. }
  121. /**
  122. * Check whether or not a particular error code was returned by the
  123. * server.
  124. *
  125. * @param integer $errorCode The error code to check against.
  126. * @return boolean Whether or not the supplied error code was returned
  127. * by the server.
  128. */
  129. public function hasError($errorCode) {
  130. return array_key_exists($errorCode, $this->_errors);
  131. }
  132. /**
  133. * Import an AppsForYourDomain error from XML.
  134. *
  135. * @param string $string The XML data to be imported
  136. * @return Zend_Gdata_Gapps_ServiceException Provides a fluent interface.
  137. * @throws Zend_Gdata_App_Exception
  138. */
  139. public function importFromString($string) {
  140. if ($string) {
  141. // Check to see if an AppsForYourDomainError exists
  142. //
  143. // track_errors is temporarily enabled so that if an error
  144. // occurs while parsing the XML we can append it to an
  145. // exception by referencing $php_errormsg
  146. @ini_set('track_errors', 1);
  147. $doc = new DOMDocument();
  148. $success = @$doc->loadXML($string);
  149. @ini_restore('track_errors');
  150. if (!$success) {
  151. require_once 'Zend/Gdata/App/Exception.php';
  152. // $php_errormsg is automatically generated by PHP if
  153. // an error occurs while calling loadXML(), above.
  154. throw new Zend_Gdata_App_Exception("DOMDocument cannot parse XML: $php_errormsg");
  155. }
  156. // Ensure that the outermost node is an AppsForYourDomain error.
  157. // If it isn't, something has gone horribly wrong.
  158. $rootElement = $doc->getElementsByTagName($this->_rootElement)->item(0);
  159. if (!$rootElement) {
  160. require_once 'Zend/Gdata/App/Exception.php';
  161. throw new Zend_Gdata_App_Exception('No root <' . $this->_rootElement . '> element found, cannot parse feed.');
  162. }
  163. foreach ($rootElement->childNodes as $errorNode) {
  164. if (!($errorNode instanceof DOMText)) {
  165. $error = new Zend_Gdata_Gapps_Error();
  166. $error->transferFromDom($errorNode);
  167. $this->addError($error);
  168. }
  169. }
  170. return $this;
  171. } else {
  172. require_once 'Zend/Gdata/App/Exception.php';
  173. throw new Zend_Gdata_App_Exception('XML passed to transferFromXML cannot be null');
  174. }
  175. }
  176. /**
  177. * Get a human readable version of this exception.
  178. *
  179. * @return string
  180. */
  181. public function __toString() {
  182. $result = "The server encountered the following errors processing the request:";
  183. foreach ($this->_errors as $error) {
  184. $result .= "\n" . $error->__toString();
  185. }
  186. return $result;
  187. }
  188. }