52
52
class Api
53
53
{
54
54
const API_URL_BASE = "https://billogram.com/api/v2 " ;
55
- const USER_AGENT = "Billogram API PHP Library/1.00 " ;
55
+ const USER_AGENT = "Billogram API PHP Library/1.01 " ;
56
56
57
57
private $ authUser ;
58
58
private $ authKey ;
59
59
private $ apiBase ;
60
60
private $ userAgent ;
61
+ private $ extraHeaders ;
61
62
62
- private $ items ;
63
- private $ customers ;
64
- private $ billogram ;
65
- private $ settings ;
66
- private $ logotype ;
67
- private $ reports ;
63
+ private $ itemsConnector ;
64
+ private $ customersConnector ;
65
+ private $ billogramConnector ;
66
+ private $ settingsConnector ;
67
+ private $ logotypeConnector ;
68
+ private $ reportsConnector ;
69
+ private $ creditorsConnector ;
68
70
69
71
/**
70
72
* Create a Billogram API connection object
@@ -76,7 +78,8 @@ public function __construct(
76
78
$ authUser ,
77
79
$ authKey ,
78
80
$ userAgent = self ::USER_AGENT ,
79
- $ apiBase = self ::API_URL_BASE
81
+ $ apiBase = self ::API_URL_BASE ,
82
+ $ extraHeaders = array ()
80
83
) {
81
84
$ this ->authUser = $ authUser ;
82
85
$ this ->authKey = $ authKey ;
@@ -85,11 +88,15 @@ public function __construct(
85
88
$ this ->userAgent = $ userAgent ;
86
89
else
87
90
$ this ->userAgent = self ::USER_AGENT ;
91
+ if (!$ extraHeaders )
92
+ $ this ->extraHeaders = array ();
93
+ else
94
+ $ this ->extraHeaders = $ extraHeaders ;
88
95
}
89
96
90
97
/**
91
- * Checks the response ($response as a response-objcet from httpRequest)
92
- * from the API and throws the apprioate exceptions or returns the
98
+ * Checks the response ($response as a response-object from httpRequest)
99
+ * from the API and throws the appropriate exceptions or returns the
93
100
* de-encoded data.
94
101
*
95
102
**/
@@ -100,11 +107,12 @@ private function checkApiResponse($response, $expectContentType = null)
100
107
101
108
if ($ response ->statusCode >= 500 && $ response ->statusCode <= 600 ) {
102
109
if ($ response ->headers ['content-type ' ] == $ expectContentType &&
103
- $ expectContentType == 'application/json ' )
110
+ $ expectContentType == 'application/json ' ) {
104
111
$ data = json_decode ($ response ->content );
105
112
throw new ServiceMalfunctioningError ('Billogram API reported ' .
106
113
'a server error: ' . $ data ->status . ' - ' .
107
114
$ data ->data ->message );
115
+ }
108
116
throw new ServiceMalfunctioningError ('Billogram API reported a ' .
109
117
'server error ' );
110
118
}
@@ -184,8 +192,6 @@ private function checkApiResponse($response, $expectContentType = null)
184
192
throw new InvalidObjectStateError ($ message );
185
193
else
186
194
throw new RequestDataError ($ message );
187
-
188
- return $ data ;
189
195
}
190
196
191
197
/**
@@ -211,6 +217,9 @@ private function httpRequest(
211
217
foreach ($ sendHeaders as $ header => $ value ) {
212
218
$ streamParams ['http ' ]['header ' ] .= $ header . ': ' . $ value . "\r\n" ;
213
219
}
220
+ foreach ($ this ->extraHeaders as $ header => $ value ) {
221
+ $ streamParams ['http ' ]['header ' ] .= $ header . ': ' . $ value . "\r\n" ;
222
+ }
214
223
215
224
if (is_array ($ data )) {
216
225
if (in_array ($ request , array ('POST ' , 'PUT ' )))
@@ -350,43 +359,52 @@ public function __get($key)
350
359
{
351
360
switch ($ key ) {
352
361
case 'items ' :
353
- if (!$ this ->items )
354
- $ this ->items = new SimpleClass ($ this , 'item ' , 'item_no ' );
362
+ if (!$ this ->itemsConnector )
363
+ $ this ->itemsConnector = new SimpleClass ($ this , 'item ' , 'item_no ' );
355
364
356
- return $ this ->items ;
365
+ return $ this ->itemsConnector ;
357
366
case 'customers ' :
358
- if (!$ this ->customers )
359
- $ this ->customers = new SimpleClass (
367
+ if (!$ this ->customersConnector )
368
+ $ this ->customersConnector = new SimpleClass (
360
369
$ this ,
361
370
'customer ' ,
362
371
'customer_no '
363
372
);
364
373
365
- return $ this ->customers ;
374
+ return $ this ->customersConnector ;
366
375
case 'billogram ' :
367
- if (!$ this ->billogram )
368
- $ this ->billogram = new BillogramClass ($ this );
376
+ if (!$ this ->billogramConnector )
377
+ $ this ->billogramConnector = new BillogramClass ($ this );
369
378
370
- return $ this ->billogram ;
379
+ return $ this ->billogramConnector ;
371
380
case 'settings ' :
372
- if (!$ this ->settings )
373
- $ this ->settings = new SingletonObject ($ this , 'settings ' );
381
+ if (!$ this ->settingsConnector )
382
+ $ this ->settingsConnector = new SingletonObject ($ this , 'settings ' );
374
383
375
- return $ this ->settings ;
384
+ return $ this ->settingsConnector ;
376
385
case 'logotype ' :
377
- if (!$ this ->logotype )
378
- $ this ->logotype = new SingletonObject ($ this , 'logotype ' );
386
+ if (!$ this ->logotypeConnector )
387
+ $ this ->logotypeConnector = new SingletonObject ($ this , 'logotype ' );
379
388
380
- return $ this ->logotype ;
389
+ return $ this ->logotypeConnector ;
381
390
case 'reports ' :
382
- if (!$ this ->reports )
383
- $ this ->reports = new SimpleClass (
391
+ if (!$ this ->reportsConnector )
392
+ $ this ->reportsConnector = new SimpleClass (
384
393
$ this ,
385
394
'report ' ,
386
395
'filename '
387
396
);
388
397
389
- return $ this ->reports ;
398
+ return $ this ->reportsConnector ;
399
+ case 'creditors ' :
400
+ if (!$ this ->creditorsConnector )
401
+ $ this ->creditorsConnector = new SimpleClass (
402
+ $ this ,
403
+ 'creditor ' ,
404
+ 'id '
405
+ );
406
+
407
+ return $ this ->creditorsConnector ;
390
408
default :
391
409
throw new UnknownFieldError ("Invalid parameter: " . $ key );
392
410
}
0 commit comments