1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* ReportingCloud PHP Wrapper |
5
|
|
|
* |
6
|
|
|
* PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH. |
7
|
|
|
* |
8
|
|
|
* @link http://www.reporting.cloud to learn more about ReportingCloud |
9
|
|
|
* @link https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository |
10
|
|
|
* @license https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md |
11
|
|
|
* @copyright © 2018 Text Control GmbH |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace TxTextControl\ReportingCloud; |
15
|
|
|
|
16
|
|
|
use GuzzleHttp\Client; |
17
|
|
|
use GuzzleHttp\RequestOptions; |
18
|
|
|
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Abstract ReportingCloud |
22
|
|
|
* |
23
|
|
|
* @package TxTextControl\ReportingCloud |
24
|
|
|
* @author Jonathan Maron (@JonathanMaron) |
25
|
|
|
*/ |
26
|
|
|
abstract class AbstractReportingCloud |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Constants |
30
|
|
|
* ----------------------------------------------------------------------------------------------------------------- |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Default date/time format of backend is 'ISO 8601' |
35
|
|
|
* |
36
|
|
|
* Note, last letter is 'P' and not 'O': |
37
|
|
|
* |
38
|
|
|
* O - Difference to Greenwich time (GMT) in hours (e.g. +0200) |
39
|
|
|
* P - Difference to Greenwich time (GMT) with colon between hours and minutes (e.g. +02:00) |
40
|
|
|
* |
41
|
|
|
* Backend uses the 'P' variant |
42
|
|
|
* |
43
|
|
|
* @const DEFAULT_DATE_FORMAT |
44
|
|
|
*/ |
45
|
|
|
const DEFAULT_DATE_FORMAT = 'Y-m-d\TH:i:sP'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Default time zone of backend |
49
|
|
|
* |
50
|
|
|
* @const DEFAULT_TIME_ZONE |
51
|
|
|
*/ |
52
|
|
|
const DEFAULT_TIME_ZONE = 'UTC'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Default base URI of backend |
56
|
|
|
* |
57
|
|
|
* @const DEFAULT_BASE_URI |
58
|
|
|
*/ |
59
|
|
|
const DEFAULT_BASE_URI = 'https://api.reporting.cloud'; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Default version string of backend |
63
|
|
|
* |
64
|
|
|
* @const DEFAULT_VERSION |
65
|
|
|
*/ |
66
|
|
|
const DEFAULT_VERSION = 'v1'; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Default timeout of backend in seconds |
70
|
|
|
* |
71
|
|
|
* @const DEFAULT_TIMEOUT |
72
|
|
|
*/ |
73
|
|
|
const DEFAULT_TIMEOUT = 120; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Default test flag of backend |
77
|
|
|
* |
78
|
|
|
* @const DEFAULT_TEST |
79
|
|
|
*/ |
80
|
|
|
const DEFAULT_TEST = false; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Default debug flag of REST client |
84
|
|
|
* |
85
|
|
|
* @const DEFAULT_DEBUG |
86
|
|
|
*/ |
87
|
|
|
const DEFAULT_DEBUG = false; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Constants (ReportingCloud values) |
91
|
|
|
* ----------------------------------------------------------------------------------------------------------------- |
92
|
|
|
*/ |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Document divider - none |
96
|
|
|
*/ |
97
|
|
|
const DOCUMENT_DIVIDER_NONE = 1; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Document divider - new paragraph |
101
|
|
|
*/ |
102
|
|
|
const DOCUMENT_DIVIDER_NEW_PARAGRAPH = 2; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Document divider - new section |
106
|
|
|
*/ |
107
|
|
|
const DOCUMENT_DIVIDER_NEW_SECTION = 3; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Properties |
111
|
|
|
* ----------------------------------------------------------------------------------------------------------------- |
112
|
|
|
*/ |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Backend API key |
116
|
|
|
* |
117
|
|
|
* @var string|null |
118
|
|
|
*/ |
119
|
|
|
protected $apiKey; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Backend username |
123
|
|
|
* |
124
|
|
|
* @var string|null |
125
|
|
|
*/ |
126
|
|
|
protected $username; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Backend password |
130
|
|
|
* |
131
|
|
|
* @var string|null |
132
|
|
|
*/ |
133
|
|
|
protected $password; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* When true, API call does not count against quota |
137
|
|
|
* "TEST MODE" watermark is added to document |
138
|
|
|
* |
139
|
|
|
* @var bool|null |
140
|
|
|
*/ |
141
|
|
|
protected $test; |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Backend base URI |
145
|
|
|
* |
146
|
|
|
* @var string|null |
147
|
|
|
*/ |
148
|
|
|
protected $baseUri; |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Backend version string |
152
|
|
|
* |
153
|
|
|
* @var string|null |
154
|
|
|
*/ |
155
|
|
|
protected $version; |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Backend timeout in seconds |
159
|
|
|
* |
160
|
|
|
* @var int|null |
161
|
|
|
*/ |
162
|
|
|
protected $timeout; |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* REST client to backend |
166
|
|
|
* |
167
|
|
|
* @var Client|null |
168
|
|
|
*/ |
169
|
|
|
protected $client; |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Debug flag of REST client |
173
|
|
|
* |
174
|
|
|
* @var bool|null |
175
|
|
|
*/ |
176
|
|
|
protected $debug; |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Set and Get Methods |
180
|
|
|
* ----------------------------------------------------------------------------------------------------------------- |
181
|
|
|
*/ |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Return the API key |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
50 |
|
public function getApiKey() |
189
|
|
|
{ |
190
|
50 |
|
return $this->apiKey; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Set the API key |
195
|
|
|
* |
196
|
|
|
* @param string $apiKey API key |
197
|
|
|
* |
198
|
|
|
* @return ReportingCloud |
199
|
|
|
*/ |
200
|
134 |
|
public function setApiKey($apiKey) |
201
|
|
|
{ |
202
|
134 |
|
$this->apiKey = $apiKey; |
203
|
|
|
|
204
|
134 |
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Return the username |
209
|
|
|
* |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
8 |
|
public function getUsername() |
213
|
|
|
{ |
214
|
8 |
|
return $this->username; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Set the username |
219
|
|
|
* |
220
|
|
|
* @param string $username Username |
221
|
|
|
* |
222
|
|
|
* @return ReportingCloud |
223
|
|
|
*/ |
224
|
6 |
|
public function setUsername($username) |
225
|
|
|
{ |
226
|
6 |
|
$this->username = $username; |
227
|
|
|
|
228
|
6 |
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Return the password |
233
|
|
|
* |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
6 |
|
public function getPassword() |
237
|
|
|
{ |
238
|
6 |
|
return $this->password; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Set the password |
243
|
|
|
* |
244
|
|
|
* @param string $password Password |
245
|
|
|
* |
246
|
|
|
* @return ReportingCloud |
247
|
|
|
*/ |
248
|
6 |
|
public function setPassword($password) |
249
|
|
|
{ |
250
|
6 |
|
$this->password = $password; |
251
|
|
|
|
252
|
6 |
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Return the base URI of the backend web service |
257
|
|
|
* |
258
|
|
|
* @return string |
259
|
|
|
*/ |
260
|
58 |
|
public function getBaseUri() |
261
|
|
|
{ |
262
|
58 |
|
if (null === $this->baseUri) { |
263
|
54 |
|
$this->setBaseUri(self::DEFAULT_BASE_URI); |
264
|
27 |
|
} |
265
|
|
|
|
266
|
58 |
|
return $this->baseUri; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Set the base URI of the backend web service |
271
|
|
|
* |
272
|
|
|
* @param string $baseUri Base URI |
273
|
|
|
* |
274
|
|
|
* @return ReportingCloud |
275
|
|
|
*/ |
276
|
58 |
|
public function setBaseUri($baseUri) |
277
|
|
|
{ |
278
|
58 |
|
$this->baseUri = $baseUri; |
279
|
|
|
|
280
|
58 |
|
return $this; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Get the timeout (in seconds) of the backend web service |
285
|
|
|
* |
286
|
|
|
* @return int |
287
|
|
|
*/ |
288
|
56 |
|
public function getTimeout() |
289
|
|
|
{ |
290
|
56 |
|
if (null === $this->timeout) { |
291
|
52 |
|
$this->setTimeout(self::DEFAULT_TIMEOUT); |
292
|
26 |
|
} |
293
|
|
|
|
294
|
56 |
|
return $this->timeout; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Set the timeout (in seconds) of the backend web service |
299
|
|
|
* |
300
|
|
|
* @param int $timeout Timeout |
301
|
|
|
* |
302
|
|
|
* @return ReportingCloud |
303
|
|
|
*/ |
304
|
56 |
|
public function setTimeout($timeout) |
305
|
|
|
{ |
306
|
56 |
|
$this->timeout = (int) $timeout; |
307
|
|
|
|
308
|
56 |
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Return the debug flag |
313
|
|
|
* |
314
|
|
|
* @return mixed |
315
|
|
|
*/ |
316
|
56 |
|
public function getDebug() |
317
|
|
|
{ |
318
|
56 |
|
if (null === $this->debug) { |
319
|
52 |
|
$this->setDebug(self::DEFAULT_DEBUG); |
320
|
26 |
|
} |
321
|
|
|
|
322
|
56 |
|
return $this->debug; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Set the debug flag |
327
|
|
|
* |
328
|
|
|
* @param bool $debug Debug flag |
329
|
|
|
* |
330
|
|
|
* @return ReportingCloud |
331
|
|
|
*/ |
332
|
56 |
|
public function setDebug($debug) |
333
|
|
|
{ |
334
|
56 |
|
$this->debug = (bool) $debug; |
335
|
|
|
|
336
|
56 |
|
return $this; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Return the test flag |
341
|
|
|
* |
342
|
|
|
* @return mixed |
343
|
|
|
*/ |
344
|
50 |
|
public function getTest() |
345
|
|
|
{ |
346
|
50 |
|
if (null === $this->test) { |
347
|
46 |
|
$this->setTest(self::DEFAULT_TEST); |
348
|
23 |
|
} |
349
|
|
|
|
350
|
50 |
|
return $this->test; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Set the test flag |
355
|
|
|
* |
356
|
|
|
* @param bool $test Test flag |
357
|
|
|
* |
358
|
|
|
* @return ReportingCloud |
359
|
|
|
*/ |
360
|
50 |
|
public function setTest($test) |
361
|
|
|
{ |
362
|
50 |
|
$this->test = (bool) $test; |
363
|
|
|
|
364
|
50 |
|
return $this; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Get the version string of the backend web service |
369
|
|
|
* |
370
|
|
|
* @return string |
371
|
|
|
*/ |
372
|
54 |
|
public function getVersion() |
373
|
|
|
{ |
374
|
54 |
|
if (null === $this->version) { |
375
|
50 |
|
$this->version = self::DEFAULT_VERSION; |
376
|
25 |
|
} |
377
|
|
|
|
378
|
54 |
|
return $this->version; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Set the version string of the backend web service |
383
|
|
|
* |
384
|
|
|
* @param string $version Version string |
385
|
|
|
* |
386
|
|
|
* @return ReportingCloud |
387
|
|
|
*/ |
388
|
4 |
|
public function setVersion($version) |
389
|
|
|
{ |
390
|
4 |
|
$this->version = $version; |
391
|
|
|
|
392
|
4 |
|
return $this; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Return the REST client of the backend web service |
397
|
|
|
* |
398
|
|
|
* @return \GuzzleHttp\Client |
399
|
|
|
*/ |
400
|
50 |
|
public function getClient() |
401
|
|
|
{ |
402
|
50 |
|
if (null === $this->client) { |
403
|
|
|
|
404
|
50 |
|
$authorization = function () { |
405
|
|
|
|
406
|
50 |
|
if (!empty($this->getApiKey())) { |
407
|
48 |
|
return sprintf('ReportingCloud-APIKey %s', $this->getApiKey()); |
408
|
|
|
} |
409
|
|
|
|
410
|
2 |
|
if (!empty($this->getUsername()) && !empty($this->getPassword())) { |
411
|
|
|
$value = sprintf('%s:%s', $this->getUsername(), $this->getPassword()); |
412
|
|
|
return sprintf('Basic %s', base64_encode($value)); |
413
|
|
|
} |
414
|
|
|
|
415
|
2 |
|
$message = 'Either the API key, or username and password must be set for authorization'; |
416
|
2 |
|
throw new InvalidArgumentException($message); |
417
|
50 |
|
}; |
418
|
|
|
|
419
|
|
|
$options = [ |
420
|
50 |
|
'base_uri' => $this->getBaseUri(), |
421
|
50 |
|
RequestOptions::TIMEOUT => $this->getTimeout(), |
422
|
50 |
|
RequestOptions::DEBUG => $this->getDebug(), |
423
|
50 |
|
RequestOptions::HEADERS => [ |
424
|
50 |
|
'Authorization' => $authorization(), |
425
|
24 |
|
], |
426
|
24 |
|
]; |
427
|
|
|
|
428
|
48 |
|
$client = new Client($options); |
429
|
|
|
|
430
|
48 |
|
$this->setClient($client); |
431
|
24 |
|
} |
432
|
|
|
|
433
|
48 |
|
return $this->client; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Set the REST client of the backend web service |
438
|
|
|
* |
439
|
|
|
* @param Client $client REST client |
440
|
|
|
* |
441
|
|
|
* @return ReportingCloud |
442
|
|
|
*/ |
443
|
48 |
|
public function setClient(Client $client) |
444
|
|
|
{ |
445
|
48 |
|
$this->client = $client; |
446
|
|
|
|
447
|
48 |
|
return $this; |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
|