-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ace7c25
Showing
22 changed files
with
570 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/vendor | ||
/.fleet | ||
/.idea | ||
/.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Dolibarr API Rest SDK | ||
|
||
## Installation | ||
|
||
The recommended way to install this package is via the Packagist Dependency Manager ([dantepiazza/dolibarr-sdk](https://packagist.org/packages/dantepiazza/dolibarr-sdk)). | ||
|
||
## Getting Started | ||
|
||
```php | ||
$dolibarr = new Dolibarr([ | ||
'url' => 'YOUR_DOLIBAR_API_URL', | ||
'token' => 'YOUR_DOLIBAR_API_TOKEN', | ||
]); | ||
|
||
$invoice = $dolibarr -> Invoices -> Get(1); | ||
|
||
if($invoice -> status){ | ||
print_r($invoice -> data); | ||
} | ||
``` | ||
|
||
The call to the different Dolibarr endpoints is made through the defined instance invoking the class and the corresponding method: | ||
|
||
|
||
```php | ||
$call = $dolibarr -> endpoint_class_name -> method_name(array $options); | ||
``` | ||
|
||
You can see a detail of the different endpoints by exploring your API from the URL `YOUR_DOLIBAR_API_URL/api/index.php/explorer` | ||
|
||
## Availables endpoints | ||
|
||
The names of the classes correspond to the different endpoints of the Dolibarr Rest API. Below is a list of endpoints available in this version: | ||
|
||
- [ ] BankAccounts | ||
- [ ] Contacts | ||
- [ ] Documents | ||
- [ ] ExpenseReports | ||
- [x] Invoices | ||
- [ ] Login | ||
- [ ] Projects | ||
- [ ] Proposals | ||
- [ ] Setup | ||
- [ ] Status | ||
- [x] SupplierInvoices | ||
- [ ] SupplierOrders | ||
- [ ] SupplierProposals | ||
- [ ] Tasks | ||
- [x] Thirdparties | ||
- [ ] Users | ||
|
||
Note that this repository is currently under development, additional classes and endpoints being actively added. | ||
|
||
## Responses | ||
|
||
All requests return an instance of the Response class | ||
|
||
```php | ||
class Response{ | ||
var bool $status = false; | ||
var mixed $data = []; | ||
var int $code = 0; | ||
} | ||
|
||
``` | ||
|
||
## Contributions | ||
|
||
First off, thanks for taking the time to contribute! 🎉👍 To help add functionality or address issues, please take the following steps: | ||
|
||
* Fork the repository from the master branch. | ||
* Create a new branch for your `features` or `fixes`. | ||
* Make the changes you wish to see. | ||
* Create a pull request with details of what changes have been made and explanation of new behaviour. | ||
* Ensure documentation contains the correct information. | ||
* Pull requests will be reviewed and hopefully merged into a release. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "dantepiazza/dolibarr-sdk", | ||
"type": "library", | ||
"autoload": { | ||
"psr-4": { | ||
"Dantepiazza\\DolibarrSdk\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Dante Piazza Quiroga", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.4" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Dantepiazza\DolibarrSdk\Adapter; | ||
|
||
class Response{ | ||
var bool $status = false; | ||
var mixed $data = []; | ||
var int $code = 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace Dantepiazza\DolibarrSdk; | ||
|
||
use Dantepiazza\DolibarrSdk\Adapter\Response; | ||
|
||
#[AllowDynamicProperties] | ||
class Dolibarr{ | ||
private $url = ''; | ||
private $token = ''; | ||
|
||
public $payload = []; | ||
public Response $response; | ||
|
||
/** | ||
* Implemented endpoints | ||
* | ||
* @var array[string] | ||
**/ | ||
var $endpoints = [ | ||
'BankAccounts', | ||
'Contacts', | ||
'Documents', | ||
'ExpenseReports', | ||
'Invoices', | ||
'Login', | ||
'Projects', | ||
'Proposals', | ||
'Setup', | ||
'Status', | ||
'SupplierInvoices', | ||
'SupplierOrders', | ||
'SupplierProposals', | ||
'Tasks', | ||
'Thirdparties', | ||
'Users', | ||
]; | ||
|
||
function __construct(array $options){ | ||
if(!isset($options['url']) || empty($options['url'])){ | ||
throw new Exception('Dolibarr endpoint url is required', 1); | ||
} | ||
|
||
if(!isset($options['token']) || empty($options['token'])){ | ||
throw new Exception('The Dolibarr token is required', 1); | ||
} | ||
|
||
$this -> url = $options['url']; | ||
$this -> token = $options['token']; | ||
$this -> response = new Response(); | ||
} | ||
|
||
public function __get($property){ | ||
if (!isset($this -> {$property}) && in_array($property, $this -> endpoints)) { | ||
$file = __DIR__.'/Endpoints/'.$property.'.php'; | ||
|
||
if (!file_exists($file)) { | ||
throw new Exception('Failed to open '.$file, 1); | ||
} | ||
|
||
include_once $file; | ||
|
||
return ($this -> {$property} = new $property($this)); | ||
} else { | ||
return $this -> {$property}; | ||
} | ||
} | ||
|
||
public function exec(string $method, string $path, array $query = []){ | ||
$headers = ['DOLAPIKEY: '.$this -> token]; | ||
$method = strtoupper($method); | ||
$query = count($query) > 0 ? sprintf("%s?%s", $url, http_build_query($query)) : ''; | ||
|
||
$curl = curl_init(); | ||
|
||
if(in_array($method, ['POST', 'PUT'])){ | ||
$headers[] = 'Content-Type:application/json'; | ||
|
||
curl_setopt($curl, CURLOPT_POST, true); | ||
|
||
if($method === 'PUT'){ | ||
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); | ||
} | ||
|
||
if(count($this -> payload) > 0){ | ||
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($this -> payload)); | ||
} | ||
} | ||
|
||
// Optional Authentication: | ||
// curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | ||
// curl_setopt($curl, CURLOPT_USERPWD, "username:password"); | ||
|
||
curl_setopt($curl, CURLOPT_URL, $this -> url.$path); | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); | ||
|
||
$response = json_decode(curl_exec($curl), true); | ||
|
||
$this -> response = new Response(); | ||
$this -> response -> status = isset($response['error']) ? false : true; | ||
$this -> response -> data = isset($response['error']) ? array_merge($response['error'], ['path' => $path, 'payload' => $this -> payload]) : $response; | ||
$this -> response -> code = curl_getinfo($curl, CURLINFO_HTTP_CODE); | ||
|
||
curl_close($curl); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
class BankAccounts { | ||
|
||
private $adapter = null; | ||
|
||
function __construct($adapter){ | ||
$this -> adapter = $adapter; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
class Contacts { | ||
|
||
private $adapter = null; | ||
|
||
function __construct($adapter){ | ||
$this -> adapter = $adapter; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
class Documents { | ||
|
||
private $adapter = null; | ||
|
||
function __construct($adapter){ | ||
$this -> adapter = $adapter; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
class ExpenseReports { | ||
|
||
private $adapter = null; | ||
|
||
function __construct($adapter){ | ||
$this -> adapter = $adapter; | ||
} | ||
} | ||
|
Oops, something went wrong.