2
0

ServiceException.php 6.8 KB

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