Skip to content

Commit

Permalink
Merge pull request #7 from kg-bot/master
Browse files Browse the repository at this point in the history
Where not able to change fields externally, this has fixed it now
  • Loading branch information
LasseRafn authored Jun 30, 2019
2 parents 804e2a0 + 086a70e commit bdd1ba2
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Builders/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class Builder
{
private $request;
protected $request;
protected $entity;
protected $responseClass = PaginatedResponse::class;

Expand Down
37 changes: 37 additions & 0 deletions src/Builders/DepositAccountBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace LasseRafn\Dinero\Builders;

use LasseRafn\Dinero\Exceptions\DineroRequestException;
use LasseRafn\Dinero\Exceptions\DineroServerException;
use LasseRafn\Dinero\Models\DepositAccount;

class DepositAccountBuilder extends Builder
{
protected $entity = 'accounts/deposit';
protected $model = DepositAccount::class;

/**
* @inheritDoc
*/
public function get($parameters = '')
{
try {
$dineroApiResponse = $this->request->curl->get("{$this->entity}{$parameters}");

$jsonResponse = json_decode($dineroApiResponse->getBody()->getContents());
} catch (ClientException $exception) {
throw new DineroRequestException($exception);
} catch (ServerException $exception) {
throw new DineroServerException($exception);
}

$request = $this->request;

$items = array_map(function ($item) use ($request) {
return new $this->model($request, $item);
}, $jsonResponse);

return (object)['items' => $items];
}
}
37 changes: 37 additions & 0 deletions src/Builders/EntryAccountBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace LasseRafn\Dinero\Builders;

use LasseRafn\Dinero\Exceptions\DineroRequestException;
use LasseRafn\Dinero\Exceptions\DineroServerException;
use LasseRafn\Dinero\Models\EntryAccount;

class EntryAccountBuilder extends Builder
{
protected $entity = 'accounts/entry';
protected $model = EntryAccount::class;

/**
* @inheritDoc
*/
public function get($parameters = '')
{
try {
$dineroApiResponse = $this->request->curl->get("{$this->entity}{$parameters}");

$jsonResponse = json_decode($dineroApiResponse->getBody()->getContents());
} catch (ClientException $exception) {
throw new DineroRequestException($exception);
} catch (ServerException $exception) {
throw new DineroServerException($exception);
}

$request = $this->request;

$items = array_map(function ($item) use ($request) {
return new $this->model($request, $item);
}, $jsonResponse);

return (object)['items' => $items];
}
}
11 changes: 11 additions & 0 deletions src/Builders/PurchaseVoucherBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace LasseRafn\Dinero\Builders;

use LasseRafn\Dinero\Models\PurchaseVoucher;

class PurchaseVoucherBuilder extends Builder
{
protected $entity = 'vouchers/purchase';
protected $model = PurchaseVoucher::class;
}
22 changes: 22 additions & 0 deletions src/Dinero.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@
use GuzzleHttp\Exception\ServerException;
use LasseRafn\Dinero\Builders\ContactBuilder;
use LasseRafn\Dinero\Builders\CreditnoteBuilder;
use LasseRafn\Dinero\Builders\DepositAccountBuilder;
use LasseRafn\Dinero\Builders\EntryAccountBuilder;
use LasseRafn\Dinero\Builders\InvoiceBuilder;
use LasseRafn\Dinero\Builders\PaymentBuilder;
use LasseRafn\Dinero\Builders\ProductBuilder;
use LasseRafn\Dinero\Builders\PurchaseVoucherBuilder;
use LasseRafn\Dinero\Exceptions\DineroRequestException;
use LasseRafn\Dinero\Exceptions\DineroServerException;
use LasseRafn\Dinero\Requests\ContactRequestBuilder;
use LasseRafn\Dinero\Requests\CreditnoteRequestBuilder;
use LasseRafn\Dinero\Requests\DepositAccountRequestBuilder;
use LasseRafn\Dinero\Requests\EntryAccountRequestBuilder;
use LasseRafn\Dinero\Requests\InvoiceRequestBuilder;
use LasseRafn\Dinero\Requests\PaymentRequestBuilder;
use LasseRafn\Dinero\Requests\ProductRequestBuilder;
use LasseRafn\Dinero\Requests\PurchaseVoucherRequestBuilder;
use LasseRafn\Dinero\Utils\Request;

class Dinero
Expand Down Expand Up @@ -107,4 +113,20 @@ public function creditnotes()
{
return new CreditnoteRequestBuilder(new CreditnoteBuilder($this->request));
}

public function entryAccounts() {

return new EntryAccountRequestBuilder(new EntryAccountBuilder($this->request));
}

public function depositAccounts() {

return new DepositAccountRequestBuilder(new DepositAccountBuilder($this->request));
}

public function purchaseVouchers()
{

return new PurchaseVoucherRequestBuilder(new PurchaseVoucherBuilder($this->request));
}
}
14 changes: 14 additions & 0 deletions src/Models/DepositAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace LasseRafn\Dinero\Models;

use LasseRafn\Dinero\Utils\Model;

class DepositAccount extends Model
{
protected $entity = 'accounts/deposit';
protected $primaryKey = 'AccountNumber';

public $AccountNumber;
public $Name;
}
18 changes: 18 additions & 0 deletions src/Models/EntryAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace LasseRafn\Dinero\Models;

use LasseRafn\Dinero\Utils\Model;

class EntryAccount extends Model
{
protected $entity = 'accounts/entry';
protected $primaryKey = 'AccountNumber';

public $AccountNumber;
public $Name;
public $VatCode;
public $Category;
public $IsHidden;
public $IsDefaultSalesAccount;
}
26 changes: 26 additions & 0 deletions src/Models/PurchaseVoucher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace LasseRafn\Dinero\Models;

use LasseRafn\Dinero\Utils\Model;

class PurchaseVoucher extends Model
{
protected $entity = 'vouchers/purchase';
protected $primaryKey = 'Guid';

public $PaymentDate;
public $VoucherDate;
public $Status;
public $ContactGuid;
public $Guid;
public $TimeStamp;
public $VoucherNumber;
public $FileGuid;
public $RegionKey;
public $DepositAccountNumber;
public $ExternalReference;

/** @var array */
public $Lines;
}
15 changes: 15 additions & 0 deletions src/Requests/DepositAccountRequestBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace LasseRafn\Dinero\Requests;

use LasseRafn\Dinero\Builders\Builder;
use LasseRafn\Dinero\Utils\RequestBuilder;

class DepositAccountRequestBuilder extends RequestBuilder
{
public function __construct( Builder $builder ) {
$this->parameters['fields'] = 'Name,AccountNumber';

parent::__construct( $builder );
}
}
15 changes: 15 additions & 0 deletions src/Requests/EntryAccountRequestBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace LasseRafn\Dinero\Requests;

use LasseRafn\Dinero\Builders\Builder;
use LasseRafn\Dinero\Utils\RequestBuilder;

class EntryAccountRequestBuilder extends RequestBuilder
{
public function __construct( Builder $builder ) {
$this->parameters['fields'] = 'Name,AccountNumber,VatCode,Category,IsHidden,IsDefaultSalesAccount';

parent::__construct( $builder );
}
}
16 changes: 16 additions & 0 deletions src/Requests/PurchaseVoucherRequestBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace LasseRafn\Dinero\Requests;

use LasseRafn\Dinero\Builders\Builder;
use LasseRafn\Dinero\Utils\RequestBuilder;

class PurchaseVoucherRequestBuilder extends RequestBuilder
{
public function __construct(Builder $builder)
{
$this->parameters['fields'] = 'Number,Guid,ContactGuid,VoucherDate,PaymentDate,Status,Timestamp,VoucherNumber,FileGuid,RegionKey,$DepositAccountNumber,ExternalReference';

parent::__construct($builder);
}
}
7 changes: 4 additions & 3 deletions src/Utils/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected function setAttribute($attribute, $value)
public function delete()
{
// todo test
return $this->request->curl->delete("/{$this->entity}/{$this->{$this->primaryKey}}");
return $this->request->curl->delete("{$this->entity}/{$this->{$this->primaryKey}}");
}

/**
Expand All @@ -81,8 +81,9 @@ public function delete()
*/
public function update($data = [])
{
// todo test
$response = $this->request->curl->put("/{$this->entity}/{$this->{$this->primaryKey}}", [
$data = array_merge($this->toArray(), $data);

$response = $this->request->curl->put("{$this->entity}/{$this->{$this->primaryKey}}", [
'json' => $data,
]);

Expand Down
15 changes: 5 additions & 10 deletions src/Utils/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,12 @@ public function __construct( Builder $builder ) {
* @return $this
*/
public function select( $fields ) {
if ( ! isset( $this->parameters['fields'] ) ) {
$this->parameters['fields'] = [];
}

if ( is_array( $fields ) ) {
foreach ( $fields as $field ) {
$this->parameters['fields'][] = $field;
}
} elseif ( is_string( $fields ) || is_int( $fields ) ) {
$this->parameters['fields'][] = $fields;
}
if (is_array($fields)) {
$this->parameters['fields'] = implode(',', $fields);
} elseif (is_string($fields) || is_int($fields)) {
$this->parameters['fields'] = $fields;
}

return $this;
}
Expand Down

0 comments on commit bdd1ba2

Please sign in to comment.