Skip to content
This repository was archived by the owner on Oct 20, 2021. It is now read-only.

Commit e6e5a6f

Browse files
committed
PHP library updates 1.01 - Fixes for inline documentation to work better with IDEs, reseller API functionality
1 parent 37a132a commit e6e5a6f

8 files changed

+73
-35
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.DS_Store
2+
.idea

Billogram/Api.php

+49-31
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,21 @@
5252
class Api
5353
{
5454
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";
5656

5757
private $authUser;
5858
private $authKey;
5959
private $apiBase;
6060
private $userAgent;
61+
private $extraHeaders;
6162

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;
6870

6971
/**
7072
* Create a Billogram API connection object
@@ -76,7 +78,8 @@ public function __construct(
7678
$authUser,
7779
$authKey,
7880
$userAgent = self::USER_AGENT,
79-
$apiBase = self::API_URL_BASE
81+
$apiBase = self::API_URL_BASE,
82+
$extraHeaders = array()
8083
) {
8184
$this->authUser = $authUser;
8285
$this->authKey = $authKey;
@@ -85,11 +88,15 @@ public function __construct(
8588
$this->userAgent = $userAgent;
8689
else
8790
$this->userAgent = self::USER_AGENT;
91+
if (!$extraHeaders)
92+
$this->extraHeaders = array();
93+
else
94+
$this->extraHeaders = $extraHeaders;
8895
}
8996

9097
/**
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
93100
* de-encoded data.
94101
*
95102
**/
@@ -100,11 +107,12 @@ private function checkApiResponse($response, $expectContentType = null)
100107

101108
if ($response->statusCode >= 500 && $response->statusCode <= 600) {
102109
if ($response->headers['content-type'] == $expectContentType &&
103-
$expectContentType == 'application/json')
110+
$expectContentType == 'application/json') {
104111
$data = json_decode($response->content);
105112
throw new ServiceMalfunctioningError('Billogram API reported ' .
106113
'a server error: ' . $data->status . ' - ' .
107114
$data->data->message);
115+
}
108116
throw new ServiceMalfunctioningError('Billogram API reported a ' .
109117
'server error');
110118
}
@@ -184,8 +192,6 @@ private function checkApiResponse($response, $expectContentType = null)
184192
throw new InvalidObjectStateError($message);
185193
else
186194
throw new RequestDataError($message);
187-
188-
return $data;
189195
}
190196

191197
/**
@@ -211,6 +217,9 @@ private function httpRequest(
211217
foreach ($sendHeaders as $header => $value) {
212218
$streamParams['http']['header'] .= $header . ': ' . $value . "\r\n";
213219
}
220+
foreach ($this->extraHeaders as $header => $value) {
221+
$streamParams['http']['header'] .= $header . ': ' . $value . "\r\n";
222+
}
214223

215224
if (is_array($data)) {
216225
if (in_array($request, array('POST', 'PUT')))
@@ -350,43 +359,52 @@ public function __get($key)
350359
{
351360
switch ($key) {
352361
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');
355364

356-
return $this->items;
365+
return $this->itemsConnector;
357366
case 'customers':
358-
if (!$this->customers)
359-
$this->customers = new SimpleClass(
367+
if (!$this->customersConnector)
368+
$this->customersConnector = new SimpleClass(
360369
$this,
361370
'customer',
362371
'customer_no'
363372
);
364373

365-
return $this->customers;
374+
return $this->customersConnector;
366375
case 'billogram':
367-
if (!$this->billogram)
368-
$this->billogram = new BillogramClass($this);
376+
if (!$this->billogramConnector)
377+
$this->billogramConnector = new BillogramClass($this);
369378

370-
return $this->billogram;
379+
return $this->billogramConnector;
371380
case 'settings':
372-
if (!$this->settings)
373-
$this->settings = new SingletonObject($this, 'settings');
381+
if (!$this->settingsConnector)
382+
$this->settingsConnector = new SingletonObject($this, 'settings');
374383

375-
return $this->settings;
384+
return $this->settingsConnector;
376385
case 'logotype':
377-
if (!$this->logotype)
378-
$this->logotype = new SingletonObject($this, 'logotype');
386+
if (!$this->logotypeConnector)
387+
$this->logotypeConnector = new SingletonObject($this, 'logotype');
379388

380-
return $this->logotype;
389+
return $this->logotypeConnector;
381390
case 'reports':
382-
if (!$this->reports)
383-
$this->reports = new SimpleClass(
391+
if (!$this->reportsConnector)
392+
$this->reportsConnector = new SimpleClass(
384393
$this,
385394
'report',
386395
'filename'
387396
);
388397

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;
390408
default:
391409
throw new UnknownFieldError("Invalid parameter: " . $key);
392410
}

Billogram/Api/Models/BillogramClass.php

+12
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,20 @@ public function __construct($api)
5353
$this->objectIdField = 'id';
5454
}
5555

56+
/**
57+
* Makes a POST request to the API and creates a new object.
58+
*
59+
* @return \Billogram\Api\Objects\BillogramObject
60+
**/
61+
public function create($data)
62+
{
63+
return parent::create($data);
64+
}
65+
5666
/**
5767
* Creates and sends a billogram using the $data and $method supplied.
5868
*
69+
* @return \Billogram\Api\Objects\BillogramObject
5970
**/
6071
public function createAndSend($data, $method)
6172
{
@@ -75,6 +86,7 @@ public function createAndSend($data, $method)
7586
/**
7687
* Creates and sells a billogram.
7788
*
89+
* @return \Billogram\Api\Objects\BillogramObject
7890
**/
7991
public function createAndSell($data, $method)
8092
{

Billogram/Api/Models/SimpleClass.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
*
3737
* See the online documentation for the actual structure of remote objects.
3838
*
39+
* @property \Billogram\Api $api
3940
**/
4041
class SimpleClass
4142
{
@@ -58,6 +59,7 @@ public function __construct($api, $urlName, $objectIdField)
5859
/**
5960
* Create a query for objects of this type.
6061
*
62+
* @return \Billogram\Api\Query
6163
**/
6264
public function query()
6365
{
@@ -67,7 +69,7 @@ public function query()
6769
/**
6870
* Finds an object by id $objectId and returns an object.
6971
*
70-
* @return void
72+
* @return \Billogram\Api\Objects\SimpleObject
7173
**/
7274
public function get($objectId)
7375
{
@@ -80,7 +82,7 @@ public function get($objectId)
8082
/**
8183
* Makes a POST request to the API and creates a new object.
8284
*
83-
* @return void
85+
* @return \Billogram\Api\Objects\SimpleObject
8486
**/
8587
public function create($data)
8688
{
@@ -93,6 +95,7 @@ public function create($data)
9395
/**
9496
* Formats and returns a URL to an object or object id.
9597
*
98+
* @return string
9699
**/
97100
public function url($object = null)
98101
{

Billogram/Api/Objects/BillogramObject.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public function creditFull()
9494
/**
9595
* Creates a credit invoice for the remaining amount of the billogram.
9696
*
97-
* @return void
9897
**/
9998
public function creditRemaining()
10099
{
@@ -181,7 +180,7 @@ public function resend($method = null)
181180
* or letter document. Will throw a ObjectNotFoundError with message
182181
* 'Object not available yet' if the PDF has not yet been generated.
183182
*
184-
* @throws Billogram\Api\Exceptions\ObjectNotFoundError
183+
* @throws \Billogram\Api\Exceptions\ObjectNotFoundError
185184
**/
186185
public function getInvoicePdf($letterId = null, $invoiceNo = null)
187186
{

Billogram/Api/Objects/SingletonObject.php

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
*
4444
* See the online documentation for the actual structure of remote objects.
4545
*
46+
* @property \Billogram\Api $api
47+
* @property \Billogram\Api\Models\SimpleClass $objectClass
4648
**/
4749
class SingletonObject
4850
{

Billogram/Api/Query.php

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
* The exact fields and special queries available for each object type varies,
4040
* see the online documentation for details.
4141
*
42+
* @property \Billogram\Api $api
43+
* @property \Billogram\Api\Models\SimpleClass $typeClass
4244
**/
4345
class Query
4446
{

examples.php

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ function autoload($className)
9999
already be sent out to the customer (via Letter, or Email). */
100100
echo 'Waiting for PDF to be generated, this may take a few seconds.' . "\n";
101101
do {
102+
$pdfContent = '';
102103
try {
103104
$pdfContent = $billogramObject->getInvoicePdf();
104105
break;

0 commit comments

Comments
 (0)