-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRequest.php
231 lines (211 loc) · 6.12 KB
/
Request.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
namespace TpReport;
/**
* Fetching reports ( or collections of entities using TargetProcess's API
*
* @author Marek Ziółkowski
*/
class Request
{
protected $params = array();
protected $collection;
private $api_base_url;
private $username;
private $password;
/**
*
* @param string $api_base_url i.e. http://localhost/TargetProcess/api/v1
* @param string $username
* @param string $password
* @throws \Exception
*/
public function __construct($api_base_url, $username = null, $password = null)
{
if ($api_base_url == '') {
throw new Exception('Undefined API base URL');
} else {
$this->api_base_url = $api_base_url;
}
$this->username = $username;
$this->password = $password;
}
/**
* Generates the condition string that will be put in the WHERE part.
* Based on Yii query builder.
*
* @return string|array the condition string to put in the WHERE part
* @throws \Exception
*/
private function processConditions($conditions)
{
if (!is_array($conditions)) {
return $conditions;
} elseif ($conditions === array()) {
return '';
}
$n = count($conditions);
$operator = strtolower($conditions[0]);
if ($operator === 'or' || $operator === 'and') {
$parts = array();
for ($i = 1; $i < $n; ++$i) {
$condition = $this->processConditions($conditions[$i]);
if ($condition !== '') {
$parts[] = '(' . $condition . ')';
}
}
return $parts === array() ? '' : implode(' ' . $operator . ' ', $parts);
}
if (!isset($conditions[1], $conditions[2])) {
return '';
}
$column = $conditions[1];
$values = $conditions[2];
if (!is_array($values)) {
$values = array($values);
}
if ($operator === 'in') {
if ($values === array()) {
return $operator === 'in' ? '0=1' : '';
} foreach ($values as $i => $value) {
if (is_string($value)) {
$values[$i] = "'" . $value . "'";
} else {
$values[$i] = (string) $value;
}
}
return $column . ' ' . $operator . ' (' . implode(',', $values) . ')';
}
throw new \Exception('Unknown operator ' . $operator);
}
/**
* Set collection name.
*
* @param string $name Collection name
* @return \TpReport
*/
public function collection($name)
{
$this->collection = $name;
return $this;
}
/**
* Builds where clause upon given parameters in form of Polish prefix notation (operators to the lefto of their operands).
*
* Always overwrites previews where conditions.
*
* @param string|array $conditions the conditions that should be put in the WHERE part. String or array in form of Polish prefix notation.
* @return \TpReport
*/
public function where($conditions)
{
$this->addParam('where', $this->processConditions($conditions));
return $this;
}
/**
* Adds any parameters as an array.
*
* @param string $name Name of the parameter
* @param mixed $values
*/
private function addArrayParams($name, $values)
{
if (!is_array($values)) {
$values = array($values);
}
if ($name != '') {
$this->addParam($name, $values);
}
}
/**
* Adds or overwrite single parameter.
*
* @param string $name
* @param mixed $value
*/
private function addParam($name, $value)
{
if ($name != '') {
$this->params[$name] = $value;
}
}
/**
* You can get items with specified fields only.
*
* @param array $fields Fields to be included
* @return \TpReport
*/
public function inc($fields)
{
$this->addArrayParams('include', $fields);
return $this;
}
/**
* Sets acid parameter (Context wfor requests)
* @param string $acid
* @return \TpReport\Request
*/
public function setAcid($acid)
{
$this->addParam('acid', $acid);
return $this;
}
/**
* Set the amount of items to be taken.
* @see http://dev.targetprocess.com/rest/response_format#paging
*
* @param int $limit
* @return \TpReport
*/
public function take($limit)
{
if (is_int($limit)) {
$this->addParam('take', $limit);
}
return $this;
}
/**
* Final URL buliding.
* @see http://dev.targetprocess.com/rest/response_format#filtering
*
* @return string
*/
public function getUrl()
{
if (!isset($this->collection) || $this->collection == '') {
throw new \Exception("Can't build URL: undefined collection");
}
$q = $this->api_base_url . '/' . $this->collection;
if (!empty($this->params)) {
$first = true;
foreach ($this->params as $name => $value) {
if (is_array($value)) {
$value_arr = $value;
$value = '[' . implode(',', $value_arr) . ']';
}
if ($value != '') {
$q.=($first ? '?' : '&') . $name . '=' . $value;
$first = false;
}
}
}
return $q;
}
/**
* Do the request.
*
* @return array
* @throws \TpReport\HttpErrorException
*/
public function query()
{
$response = \Httpful\Request::get(str_replace(" ", "%20", $this->getUrl()))
->addHeader('Accept', 'application/json')
->authenticateWith($this->username, $this->password)
->send();
$error_code = (int) $response->code;
if ((int) $error_code != 200) {
throw new \TpReport\HttpErrorException($error_code);
}
return $response->body;
}
}