source: extensions/Google2Piwigo/include/Zend/Gdata/Gapps/ServiceException.php @ 17475

Last change on this file since 17475 was 17475, checked in by mistic100, 12 years ago

new extension: Google2Piwigo

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