-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathYqlResponse.php
145 lines (131 loc) · 3.05 KB
/
YqlResponse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* This file contains a class to encapsulate the response returned from a call to YQL Web Service.
*
* @author Jefftulsa <[email protected]>
* @link http://www.yiihaw.com/
* @license MIT
*/
/**
* YqlResponse encapsulates a returned YQL response.
*/
class YqlResponse extends CComponent
{
private $_rawData;
private $_data;
private $_results;
private $_count;
private $_created;
private $_language;
private $_error;
private $_itemName;
/**
* Constructor.
* @param mixed $response the raw response returned from the query call
* @param string $itemName the name of the main item returned in the query results. This will depend on the query being made
* @param string $format the format of the returned response (can be either xml or json)
* @param boolean $requestedError whether or not there was an error with the request
*/
public function __construct($response, $itemName, $format='json', $requestError=false)
{
if($requestError)
{
$this->_error = new stdClass;
$this->_error->description = $requestError;
return;
}
$this->_rawData = $response;
$this->_itemName = $itemName;
if('json' == $format)
{
$this->_data = json_decode($this->_rawData);
}
if(is_object($this->_data))
{
//check for errors
if(isset($this->_data->error))
{
if(is_object($this->_data->error))
{
$this->_error = $this->_data->error;
}
else
{
$this->_error = new stdClass;
$this->_error->description = 'Unknown error returned from YQL call.';
}
}
else
{
if(is_object($this->_data->query))
{
$this->_count = $this->_data->query->count;
$this->_created = $this->_data->query->created;
$this->_language = $this->_data->query->lang;
$this->_results = $this->_data->query->results;
}
else
throw new CException('response not an object');
}
}
}
/**
* Returns the number of returned results.
* @return integer count of items
*/
public function getCount()
{
return $this->_count;
}
/**
* Returns the date of the returned response.
* @return string
*/
public function getDate()
{
return $this->_created;
}
/**
* Returns the language in which the returned results are written.
* @return string
*/
public function getLanguage()
{
return $this->_language;
}
/**
* Returns the results (rows) of the response.
* @return array of json objects representing the returned rows of data
*/
public function getResults()
{
$name = $this->_itemName;
return $this->_results->$name;
}
/*
* Returns the raw data from the response (i.e. in the raw json or xml format).
*/
public function getRawData()
{
return $this->_rawData;
}
/*
* Returns the full response (i.e. not just the data rows).
*/
public function getFullData()
{
return $this->_data;
}
public function hasErrors()
{
return isset($this->_error);
}
public function getHasErrors()
{
return $this->hasErrors();
}
public function getError()
{
return $this->_error;
}
}