1: <?php
2:
3: /**
4: * A RESTian_Response represents the return values from an HTTP request
5: */
6: class RESTian_Response {
7:
8: /**
9: * @var int
10: */
11: var $status_code = 200;
12:
13: /**
14: * @var bool
15: */
16: var $message = false;
17:
18: /**
19: * @var bool|array False if no error, of code (string) and message that external clients can use to branch
20: * appropriatly after a call and/or to provide error messages for their users.
21: *
22: * Provides a dependable format error code for clients to take action on when using RESTian.
23: *
24: * 'code' => High level error codes for the caller.
25: *
26: * 'NO_AUTH' - No username and password passed
27: * 'BAD_AUTH' - Username and password combination rejected by Revostock
28: * 'API_FAIL' - Problem communicating with the API
29: * 'NO_BODY' - No response body returned when one was expected.
30: * 'BAD_SYNTAX' - The response body contains malformed XML, JSON, etc.
31: * 'UNKNOWN' - Unexpected HTTP response code
32: *
33: * 'message' => Human readable to explain the $error_code.
34: */
35: private $_error = false;
36:
37: /**
38: * @var object|array A structured version of the body, is applicable.
39: */
40: var $data;
41: /**
42: * @var bool
43: */
44: var $body = false;
45: /**
46: * @var object
47: */
48: /**
49: * @var RESTian_Http_Agent_Base Encapsules the specifics for the HTTP agent: PHP's curl ('php_curl') or WordPress' ('wordpress').
50: * Contains raw returned data (data) and error results (error_num and error_msg) when an error occurs.
51: */
52: var $http_error = false;
53: /**
54: * @var mixed
55: */
56: var $result;
57: /**
58: * @var RESTian_Request
59: */
60: var $request;
61: /**
62: * @var bool
63: */
64: var $authenticated = false;
65: /**
66: * @var array
67: */
68: var $grant = array();
69:
70: /**
71: * @param array $args
72: */
73: function __construct( $args = array() ) {
74: /**
75: * Copy properties in from $args, if they exist.
76: */
77: foreach( $args as $property => $value )
78: if ( property_exists( $this, $property ) )
79: $this->$property = $value;
80:
81: }
82:
83: /**
84: * @param $number
85: * @param $message
86: */
87: function set_http_error( $number, $message ) {
88: $this->http_error = array( $number, $message );
89: }
90:
91: /**
92: * @return bool|RESTian_Http_Agent_Base
93: */
94: function is_http_error() {
95: return $this->http_error;
96: }
97:
98: /**
99: *
100: * @return bool|object
101: */
102: function get_error() {
103: return $this->_error;
104: }
105:
106: /**
107: *
108: * @return bool|object
109: */
110: function has_error() {
111: return is_object( $this->_error );
112: }
113:
114: /**
115: *
116: * @return bool|object
117: */
118: function is_error() {
119: return is_object( $this->_error );
120: }
121:
122: /**
123: * @param $code
124: * @param bool|string|RESTian_Service $message
125: */
126: function set_error( $code, $message = false ) {
127: if ( false === $message )
128: $message = $code;
129: $this->_error = (object)array(
130: 'code' => $code,
131: 'message' => ! is_object( $message ) ? $message : $message->get_error_message( $code ),
132: );
133: }
134:
135: }
136: