Skip to content

Commit

Permalink
Add weborder post functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
stefandoorn committed May 10, 2017
1 parent ff70ce4 commit f7d2899
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,25 @@ $allArticles = $entity->getAll([
'internetPublished' => 'true',
]);
```

### POST items

* Use `post` on an entity to push a single item to Becosoft

## Examples

### POST Weborder

```
$gateway = GatewayFactory::get('apiKey', $debug = true);
$api = new Api($gateway);
$entity = new BecosoftApi\Entity\Weborder($this->api);
$weborder = new BecosoftApi\Model\Weborder;
$weborder->WeborderId = 1;
...
$result = $entity->post($weborder->toJson();
```

Currently you have to insert a JSON object yourself, which gives flexibility. The Weborder model is simply a helper model to make sure you insert fields with the correct name.
35 changes: 35 additions & 0 deletions src/Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BecosoftApi\Api;
use BecosoftApi\ApiInterface;
use Webmozart\Assert\Assert;

/**
* Class AbstractEntity
Expand All @@ -18,6 +19,11 @@ abstract class AbstractEntity implements EntityInterface
*/
protected static $endpoint;

/**
* @var string
*/
protected static $idField;

/**
* @var Api
*/
Expand All @@ -32,6 +38,35 @@ public function __construct(ApiInterface $api)
$this->api = $api;
}

/**
* {@inheritdoc}
*/
public function getById($id, array $options = [])
{
Assert::notNull(self::$idField);

return $this->get([self::$idField => $id], $options)->getBody()->getContents();
}

/**
* {@inheritdoc}
*/
public function post($data, array $options = [])
{
if (array_key_exists('body', $options) && !empty($data)) {
throw new \Exception('BODY key already exists in options array, supplied data is not empty');
}

$options['body'] = $data;

if (!array_key_exists('Content-Type', $options)) {
$options['Content-Type'] = 'application/json';
}

$response = $this->api->request('POST', static::$endpoint, $options);
return $response->getBody()->getContents();
}

/**
* {@inheritdoc}
*/
Expand Down
15 changes: 15 additions & 0 deletions src/Entity/EntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@
*/
interface EntityInterface
{
/**
* @param $id
* @param array $options
* @return string
*/
public function getById($id, array $options = []);

/**
* @param string $data (JSON e.g.)
* @param array $options
* @return string
* @throws \Exception
*/
public function post($data, array $options = []);

/**
* @param array $query
* @param array $options
Expand Down
13 changes: 13 additions & 0 deletions src/Entity/Weborder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace BecosoftApi\Entity;

/**
* Class Weborder
* @package BecosoftApi\Entity
*/
class Weborder extends AbstractEntity implements EntityInterface
{
protected static $endpoint = 'Weborder';
protected static $idField = 'orderID';
}
51 changes: 51 additions & 0 deletions src/Model/AbstractModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace BecosoftApi\Model;

use Illuminate\Support\Collection;

/**
* Class AbstractModel
* @package BecosoftApi\Model
*/
abstract class AbstractModel
{
/**
* @return array
* @todo Refactor this
*/
public function toArray()
{
$values = call_user_func('get_object_vars', $this);

foreach($values as $key => $value) {
if (!$value instanceof Collection) {
continue;
}

foreach($value as $subKey => $subValue) {
if (!is_array($subValue)) {
$subValue = $subValue->toArray();
}

$values[$key][$subKey] = $subValue;
}
}

return array_filter($values, function ($item) {
if ($item instanceof Collection) {
return !$item->isEmpty();
}

return !is_null($item);
});
}

/**
* @return string
*/
public function toJson()
{
return json_encode($this->toArray());
}
}
161 changes: 161 additions & 0 deletions src/Model/Weborder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

namespace BecosoftApi\Model;

use Illuminate\Support\Collection;

/**
* Class Weborder
* @package BecosoftApi\Model
*/
class Weborder extends AbstractModel
{
/**
* @var int
*/
public $WeborderId;

/**
* @var int
*/
public $Klantnummer;

/**
* @var \DateTime
*/
public $Datum;

/**
* @var string
*/
public $verkoper;

/**
* @var bool
*/
public $Afgehandeld;

/**
* @var string
*/
public $opmerkingorder;

/**
* @var int
*/
public $leveringsadres;

/**
* @var string
*/
public $ordertype;

/**
* @var float
*/
public $totaal;

/**
* @var int
*/
public $verzendtype;

/**
* @var string
*/
public $kortingcode;

/**
* @var string
*/
public $LeveradresNaam;

/**
* @var string
*/
public $LeveradresStraat;

/**
* @var string
*/
public $LeveradresExtraAdreslijn;

/**
* @var string
*/
public $LeveradresLand;

/**
* @var string
*/
public $LeveradresPostcode;

/**
* @var string
*/
public $LeveradresPlaats;

/**
* @var string
*/
public $LeveradresHuisnummer;

/**
* @var string
*/
public $LeveradresBus;

/**
* @var string
*/
public $Email;

/**
* @var int
*/
public $ecomklantid;

/**
* @var string
*/
public $ecomRemark;

/**
* @var string
*/
public $ecomCurrency;

/**
* @var string
*/
public $kialaPointID;

/**
* @var string
*/
public $kialaName;

/**
* @var bool
*/
public $avondlevering;

/**
* @var Collection|WeborderDetail[]
*/
public $Details;

/**
* @var Collection|WeborderPayment[]
*/
public $Payments;

/**
* Weborder constructor.
*/
public function __construct()
{
$this->Details = new Collection();
$this->Payments = new Collection();
}
}
40 changes: 40 additions & 0 deletions src/Model/WeborderDetail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace BecosoftApi\Model;

/**
* Class WeborderDetail
* @package BecosoftApi\Model
*/
class WeborderDetail extends AbstractModel
{
/**
* @var int
*/
public $aantal;

/**
* @var int
*/
public $artikelnummer;

/**
* @var float
*/
public $verkoopprijs;

/**
* @var string
*/
public $voorraadlocatie;

/**
* @var string
*/
public $retourcode;

/**
* @var string
*/
public $retourreden;
}

0 comments on commit f7d2899

Please sign in to comment.