/libraries/Zend/GData/GApps/ServiceException.php

https://github.com/kiranatama/sagalaya · PHP · 194 lines · 72 code · 17 blank · 105 comment · 10 complexity · e1806510e2df230f932a2154c31c9768 MD5 · raw file

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