Skip to content

Commit

Permalink
Option (enabled automatically for new users, disabled for old) to str…
Browse files Browse the repository at this point in the history
…ip null values
  • Loading branch information
LasseRafn committed Oct 30, 2019
1 parent 8df8ee6 commit 657b2b7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/Builders/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ public function all($filters = [], $pageSize = 500)
*/
public function create($data)
{
$data = $this->request->formatData($data);

return $this->request->handleWithExceptions(function () use ($data) {
$response = $this->request->curl->post("/{$this->entity}", [
'json' => $data,
Expand Down
30 changes: 17 additions & 13 deletions src/Economic.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use LasseRafn\Economic\Builders\VatZoneBuilder;
use LasseRafn\Economic\Builders\VoucherBuilder;
use LasseRafn\Economic\Models\CompanySelf;
use LasseRafn\Economic\Utils\Model;
use LasseRafn\Economic\Utils\Request;

class Economic
Expand All @@ -40,11 +41,14 @@ class Economic

protected $apiPublic;

public function __construct($agreement = '', $apiSecret = null, $apiPublic = null)
protected $stripNullValues;

public function __construct($agreement = '', $apiSecret = null, $apiPublic = null, $stripNull = null)
{
$this->agreement = $agreement ?? config('economic.agreement');
$this->apiSecret = $apiSecret ?? config('economic.secret_token');
$this->apiPublic = $apiPublic ?? config('economic.public_token');
$this->stripNullValues = $stripNull ?? config('economic.strip_null', false);

$this->initRequest();
}
Expand Down Expand Up @@ -157,16 +161,16 @@ public function layouts()
{
return new LayoutBuilder($this->request);
}

/**
* @param integer $customerNumber
*
* @return ContactBuilder()|Builder
*/
public function customerContacts( $customerNumber ) {
return new ContactBuilder( $this->request, $customerNumber );
}

/**
* @param integer $customerNumber
*
* @return ContactBuilder()|Builder
*/
public function customerContacts( $customerNumber )
{
return new ContactBuilder( $this->request, $customerNumber );
}

/**
* @return VatZoneBuilder|Builder
Expand Down Expand Up @@ -259,7 +263,7 @@ public function archivedOrders()
}

/**
* @return CompanySelf
* @return CompanySelf|Model
*/
public function self() {
return ( new SelfBuilder( $this->request ) )->find( '' );
Expand Down Expand Up @@ -315,6 +319,6 @@ public function downloadInvoice($directUrl)

protected function initRequest()
{
$this->request = new Request($this->agreement, $this->apiSecret);
$this->request = new Request($this->agreement, $this->apiSecret, $this->stripNullValues);
}
}
4 changes: 3 additions & 1 deletion src/Utils/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ public function update($data = [])

public function updateRaw($data = [])
{
return $this->request->handleWithExceptions(function () use ($data) {
$data = $this->request->formatData($data);

return $this->request->handleWithExceptions(function () use ($data) {
$response = $this->request->curl->put($this->getUpdateEndpoint(), [
'json' => $data,
]);
Expand Down
14 changes: 13 additions & 1 deletion src/Utils/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class Request
{
public $curl;

public function __construct($agreementToken = '', $apiSecret = '')
protected $stripNull;

public function __construct($agreementToken = '', $apiSecret = '', $stripNull = false)
{
$this->curl = new Client([
'base_uri' => config('economic.request_endpoint'),
Expand All @@ -21,6 +23,16 @@ public function __construct($agreementToken = '', $apiSecret = '')
'Content-Type' => 'application/json',
],
]);

$this->stripNull = $stripNull;
}

public function formatData($data) {
if($this->stripNull) {
return array_filter($data, static function($item) { return $item !== null; });
}

return $data;
}

public function handleWithExceptions($callback)
Expand Down
5 changes: 5 additions & 0 deletions src/config/economic.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
'public_token' => env('ECONOMIC_APP_PUBLIC'),
'secret_token' => env('ECONOMIC_APP_SECRET'),
'agreement' => env('ECONOMIC_AGREEMENT'),

/* --------------------------------------------------------
* Automatically strip null values.
* ----------------------------------------------------- */
'strip_null' => true,
];

0 comments on commit 657b2b7

Please sign in to comment.