Skip to content

Commit

Permalink
Initial version of the project
Browse files Browse the repository at this point in the history
  • Loading branch information
dantepiazza committed Mar 31, 2024
0 parents commit ace7c25
Show file tree
Hide file tree
Showing 22 changed files with 570 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor
/.fleet
/.idea
/.vscode
76 changes: 76 additions & 0 deletions README.md
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.
18 changes: 18 additions & 0 deletions composer.json
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": "dante.7h@gmail.com"
}
],
"require": {
"php": ">=7.4"
}
}
18 changes: 18 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/Adapter/Response.php
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;
}
109 changes: 109 additions & 0 deletions src/Dolibarr.php
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;
}
}
11 changes: 11 additions & 0 deletions src/Endpoints/BankAccounts.php
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;
}
}

11 changes: 11 additions & 0 deletions src/Endpoints/Contacts.php
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;
}
}

11 changes: 11 additions & 0 deletions src/Endpoints/Documents.php
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;
}
}

11 changes: 11 additions & 0 deletions src/Endpoints/ExpenseReports.php
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;
}
}

Loading

0 comments on commit ace7c25

Please sign in to comment.