source: trunk/www.guidonia.net/wp/wp-content/plugins/webtv/Drivers/Zend/Gdata/Gapps/ServiceException.php@ 44

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