From 657b2b75ede37e5cb6558cc2445ca8fba6b328ba Mon Sep 17 00:00:00 2001 From: Lasse Rafn Date: Wed, 30 Oct 2019 10:17:28 +0000 Subject: [PATCH] Option (enabled automatically for new users, disabled for old) to strip null values --- src/Builders/Builder.php | 2 ++ src/Economic.php | 30 +++++++++++++++++------------- src/Utils/Model.php | 4 +++- src/Utils/Request.php | 14 +++++++++++++- src/config/economic.php | 5 +++++ 5 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/Builders/Builder.php b/src/Builders/Builder.php index 372e6b9..ff165e1 100644 --- a/src/Builders/Builder.php +++ b/src/Builders/Builder.php @@ -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, diff --git a/src/Economic.php b/src/Economic.php index dc38dd4..20bd42b 100644 --- a/src/Economic.php +++ b/src/Economic.php @@ -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 @@ -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(); } @@ -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 @@ -259,7 +263,7 @@ public function archivedOrders() } /** - * @return CompanySelf + * @return CompanySelf|Model */ public function self() { return ( new SelfBuilder( $this->request ) )->find( '' ); @@ -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); } } diff --git a/src/Utils/Model.php b/src/Utils/Model.php index 5c85a84..7a78fc6 100644 --- a/src/Utils/Model.php +++ b/src/Utils/Model.php @@ -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, ]); diff --git a/src/Utils/Request.php b/src/Utils/Request.php index 038e569..3088d21 100644 --- a/src/Utils/Request.php +++ b/src/Utils/Request.php @@ -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'), @@ -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) diff --git a/src/config/economic.php b/src/config/economic.php index eb770cc..8dc519e 100644 --- a/src/config/economic.php +++ b/src/config/economic.php @@ -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, ];