Arbit - project tracking

PHPillow - PHP CouchDB connector

Browse source code

File: / src/ classes/ response.php

Type
text/plain text/plain
Last Author
kore
Version
159
Line Rev. Author Source
1 1 kore <?php
2 kore /**
3 2 kore * phpillow CouchDB backend
4 1 kore *
5 2 kore * This file is part of phpillow.
6 1 kore *
7 3 kore * phpillow is free software; you can redistribute it and/or modify it under
8 kore * the terms of the GNU Lesser General Public License as published by the Free
9 kore * Software Foundation; version 3 of the License.
10 1 kore *
11 3 kore * phpillow is distributed in the hope that it will be useful, but WITHOUT ANY
12 kore * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 kore * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14 kore * more details.
15 1 kore *
16 3 kore * You should have received a copy of the GNU Lesser General Public License
17 kore * along with phpillow; if not, write to the Free Software Foundation, Inc., 51
18 kore * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 1 kore *
20 kore * @package Core
21 4 kore * @version $Revision: 159 $
22 3 kore * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
23 1 kore */
24 kore
25 kore /**
26 kore * Response factory to create response objects from JSON results
27 kore *
28 kore * @package Core
29 4 kore * @version $Revision: 159 $
30 3 kore * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
31 1 kore */
32 3 kore class phpillowResponseFactory
33 1 kore {
34 kore /**
35 kore * Parse a server response
36 kore *
37 kore * Parses a server response depending on the response body and the HTTP
38 kore * status code.
39 kore *
40 159 kore * The method will either return a plain phpillowResponse object, when the
41 1 kore * server returned a single document. If the server returned a set of
42 3 kore * documents you will receive a phpillowResultSetResponse object, with a row
43 1 kore * property to iterate over all documents returned by the server.
44 kore *
45 kore * For put and delete requests the server will just return a status,
46 159 kore * whether the request was successful, which is represented by a
47 3 kore * phpillowStatusResponse object.
48 1 kore *
49 159 kore * For all other cases most probably some error occurred, which is
50 3 kore * transformed into a phpillowResponseErrorException, which will be thrown
51 1 kore * by the parse method.
52 25 kore *
53 kore * If the third parameter raw is set to true, the body will not expected to
54 159 kore * be some JSON structure, but just preserved as a raw string.
55 kore *
56 33 kore * @param array $headers
57 159 kore * @param string $body
58 97 kore * @param bool $raw
59 3 kore * @return phpillowResponse
60 1 kore */
61 33 kore public static function parse( array $headers, $body, $raw = false )
62 1 kore {
63 30 kore $response = $raw === true ? $body : json_decode( $body, true );
64 1 kore
65 kore // To detect the type of the response from the couch DB server we use
66 kore // the response status which indicates the return type.
67 33 kore switch ( $headers['status'] )
68 1 kore {
69 kore case 200:
70 kore // The HTTP status code 200 - OK indicates, that we got a document
71 kore // or a set of documents as return value.
72 kore //
73 159 kore // To check whether we received a set of documents or a single
74 1 kore // document we can check for the document properties _id or
75 kore // _rev, which are always available for documents and are only
76 kore // available for documents.
77 25 kore if ( $raw === true )
78 1 kore {
79 33 kore return new phpillowDataResponse( $headers['content-type'], $response );
80 25 kore }
81 44 kore elseif ( $body[0] === '[' )
82 kore {
83 kore return new phpillowArrayResponse( $response );
84 kore }
85 156 kore elseif ( isset( $response['_id'] ) )
86 25 kore {
87 3 kore return new phpillowResponse( $response );
88 1 kore }
89 156 kore elseif ( isset( $response['rows'] ) )
90 1 kore {
91 3 kore return new phpillowResultSetResponse( $response );
92 1 kore }
93 kore
94 25 kore // Otherwise fall back to a plain status response. No break.
95 kore
96 1 kore case 201:
97 kore case 202:
98 kore // The following status codes are given for status responses
99 kore // depending on the request type - which does not matter here any
100 kore // more.
101 3 kore return new phpillowStatusResponse( $response );
102 1 kore
103 kore case 404:
104 kore // The 404 and 409 (412) errors are using custom exceptions
105 kore // extending the base error exception, because they are often
106 25 kore // required to be handled in a special way by the application.
107 1 kore //
108 kore // Feel free to extend this for other errors as well.
109 3 kore throw new phpillowResponseNotFoundErrorException( $response );
110 25 kore
111 1 kore case 409: // Conflict
112 kore case 412: // Precondition Failed - we just consider this as a conflict.
113 3 kore throw new phpillowResponseConflictErrorException( $response );
114 1 kore
115 kore default:
116 kore // All other unhandled HTTP codes are for now handled as an error.
117 kore // This may not be true, as lots of other status code may be used
118 159 kore // for valid responses.
119 33 kore throw new phpillowResponseErrorException( $headers['status'], $response );
120 1 kore }
121 kore }
122 kore }
123 kore