Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results

Large diffs are not rendered by default.

67 changes: 66 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ This package was developed to give you a quick start to the Bexio API.
- [Contact Relations](#contact-relations)
- [Contacts](#contacts)
- [Contact Sectors](#contact-sectors)
- [Countries](#countries)
- [Currencies](#currencies)
- [Files](#files)
- [Iban Payments](#iban-payments)
Expand Down Expand Up @@ -681,9 +682,10 @@ We provide enums for the following values:
| ContactRelations: OrderByEnum | ID(), ID_ASC(), ID_DESC(), CONTACT_ID(), CONTACT_ID_ASC(), CONTACT_ID_DESC(), CONTACT_SUB_ID(), CONTACT_SUB_ID_ASC(), CONTACT_SUB_ID_DESC(), UPDATED_AT(), UPDATED_AT_ASC(), UPDATED_AT_DESC() |
| Contacts: OrderByEnum | ID(), ID_ASC(), ID_DESC(), NR(), NR_ASC(), NR_DESC(), NAME_1(), NAME_1_ASC(), NAME_1_DESC(), UPDATED_AT(), UPDATED_AT_ASC(), UPDATED_AT_DESC() |
| ContactSectors: OrderByEnum | ID(), ID_ASC(), ID_DESC(), NAME(), NAME_ASC(), NAME_DESC() |
| Countries: CountriesOrderByEnum | ID(), ID_ASC(), ID_DESC(), NAME(), NAME_ASC(), NAME_DESC(), NAME_SHORT(), NAME_SHORT_ASC(), NAME_SHORT_DESC() |
| IbanPayments: AllowanceTypeEnum | FEE_PAID_BY_SENDER(), FEE_PAID_BY_RECIPIENT(), FEE_SPLIT(), NO_FEE() |
| IbanPayments: StatusEnum | OPEN(), TRANSFERRED(), DOWNLOADED(), ERROR(), CANCELLED() |
| Items: OrderByEnum | ID(), ID_ASC(), ID_DESC(), INTERN_NAME(), INTERN_NAME_ASC(), INTERN_NAME_DESC() |
| Items: ItemsOrderByEnum | ID(), ID_ASC(), ID_DESC(), INTERN_NAME(), INTERN_NAME_ASC(), INTERN_NAME_DESC() |
| ManualEntries: TypeEnum | MANUAL_SINGLE_ENTRY(), MANUAL_GROUP_ENTRY(), MANUAL_COMPOUND_ENTRY() |
| QrPayments: AllowanceTypeEnum | FEE_PAID_BY_SENDER(), FEE_PAID_BY_RECIPIENT(), FEE_SPLIT(), NO_FEE() |
| QrPayments: StatusEnum | OPEN(), TRANSFERRED(), DOWNLOADED(), ERROR(), CANCELLED() |
Expand Down Expand Up @@ -2082,6 +2084,69 @@ $response = $connector->send(new DeleteAnItemRequest(
));
```

### Countries
```php
/**
* Fetch A List Of Countries
*/
$countries = $connector->send(new FetchAListOfCountriesRequest())->dto();
```

```php
/**
* Fetch A Country
*/
$country = $connector->send(new FetchACountryRequest(
country_id: 1
))->dto();
```

```php
/**
* Search Countries
*/
$countries = $connector->send(new SearchCountriesRequest(
searchField: 'name',
searchTerm: 'Switzerland'
))->dto();
```

```php
/**
* Create Country
*/
$country = $connector->send(new CreateCountryRequest(
data: new CreateEditCountryDTO(
name: 'Switzerland',
name_short: 'CH',
iso3166_alpha2: 'CH'
)
))->dto();
```

```php
/**
* Edit Country
*/
$country = $connector->send(new EditACountryRequest(
country_id: 1,
data: new CreateEditCountryDTO(
name: 'Switzerland',
name_short: 'CH',
iso3166_alpha2: 'CH'
)
))->dto();
```

```php
/**
* Delete Country
*/
$response = $connector->send(new DeleteACountryRequest(
country_id: 1
));
```

### VAT Periods
```php
/**
Expand Down
43 changes: 43 additions & 0 deletions src/Dto/Countries/CountryDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace CodebarAg\Bexio\Dto\Countries;

use Exception;
use Illuminate\Support\Arr;
use Saloon\Http\Response;
use Spatie\LaravelData\Data;

class CountryDTO extends Data
{
public function __construct(
public int $id,
public string $name,
public string $name_short,
public string $iso3166_alpha2,
) {}

public static function fromResponse(Response $response): self
{
if ($response->failed()) {
throw new \Exception('Failed to create DTO from Response');
}

$data = $response->json();

return self::fromArray($data);
}

public static function fromArray(array $data): self
{
if (! $data) {
throw new Exception('Unable to create DTO. Data missing from response.');
}

return new self(
id: Arr::get($data, 'id'),
name: Arr::get($data, 'name'),
name_short: Arr::get($data, 'name_short'),
iso3166_alpha2: Arr::get($data, 'iso_3166_alpha2'),
);
}
}
41 changes: 41 additions & 0 deletions src/Dto/Countries/CreateEditCountryDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace CodebarAg\Bexio\Dto\Countries;

use Exception;
use Illuminate\Support\Arr;
use Saloon\Http\Response;
use Spatie\LaravelData\Data;

class CreateEditCountryDTO extends Data
{
public function __construct(
public string $name,
public string $name_short,
public string $iso3166_alpha2,
) {}

public static function fromResponse(Response $response): self
{
if ($response->failed()) {
throw new \Exception('Failed to create DTO from Response');
}

$data = $response->json();

return self::fromArray($data);
}

public static function fromArray(array $data): self
{
if (! $data) {
throw new Exception('Unable to create DTO. Data missing from response.');
}

return new self(
name: Arr::get($data, 'name'),
name_short: Arr::get($data, 'name_short'),
iso3166_alpha2: Arr::get($data, 'iso3166_alpha2'),
);
}
}
49 changes: 49 additions & 0 deletions src/Enums/Countries/CountriesOrderByEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace CodebarAg\Bexio\Enums\Countries;

use Spatie\Enum\Laravel\Enum;

/**
* @method static self ID()
* @method static self ID_ASC()
* @method static self ID_DESC()
* @method static self NAME()
* @method static self NAME_ASC()
* @method static self NAME_DESC()
* @method static self NAME_SHORT()
* @method static self NAME_SHORT_ASC()
* @method static self NAME_SHORT_DESC()
*/
final class CountriesOrderByEnum extends Enum
{
protected static function values(): array
{
return [
'ID' => 'id',
'ID_ASC' => 'id_asc',
'ID_DESC' => 'id_desc',
'NAME' => 'name',
'NAME_ASC' => 'name_asc',
'NAME_DESC' => 'name_desc',
'NAME_SHORT' => 'name_short',
'NAME_SHORT_ASC' => 'name_short_asc',
'NAME_SHORT_DESC' => 'name_short_desc',
];
}

protected static function labels(): array
{
return [
'ID' => 'Id',
'ID_ASC' => 'Id Ascending',
'ID_DESC' => 'Id Descending',
'NAME' => 'Name',
'NAME_ASC' => 'Name Ascending',
'NAME_DESC' => 'Name Descending',
'NAME_SHORT' => 'Name Short',
'NAME_SHORT_ASC' => 'Name Short Ascending',
'NAME_SHORT_DESC' => 'Name Short Descending',
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @method static self INTERN_NAME_ASC()
* @method static self INTERN_NAME_DESC()
*/
final class OrderByEnum extends Enum
final class ItemsOrderByEnum extends Enum
{
protected static function values(): array
{
Expand Down
48 changes: 48 additions & 0 deletions src/Requests/Countries/CreateCountryRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace CodebarAg\Bexio\Requests\Countries;

use CodebarAg\Bexio\Dto\Countries\CountryDTO;
use CodebarAg\Bexio\Dto\Countries\CreateEditCountryDTO;
use Exception;
use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Body\HasJsonBody;

class CreateCountryRequest extends Request implements HasBody
{
use HasJsonBody;

protected Method $method = Method::POST;

public function __construct(
protected readonly array|CreateEditCountryDTO $data,
) {}

public function resolveEndpoint(): string
{
return '/2.0/country';
}

protected function defaultBody(): array
{
$body = $this->data;

if (! $body instanceof CreateEditCountryDTO) {
$body = CreateEditCountryDTO::fromArray($body);
}

return $body->toArray();
}

public function createDtoFromResponse(Response $response): CountryDTO
{
if (! $response->successful()) {
throw new Exception('Request was not successful. Unable to create DTO.');
}

return CountryDTO::fromArray($response->json());
}
}
31 changes: 31 additions & 0 deletions src/Requests/Countries/DeleteACountryRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace CodebarAg\Bexio\Requests\Countries;

use Exception;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;

class DeleteACountryRequest extends Request
{
protected Method $method = Method::DELETE;

public function __construct(
public readonly int $country_id,
) {}

public function resolveEndpoint(): string
{
return '/2.0/country/'.$this->country_id;
}

public function createDtoFromResponse(Response $response): mixed
{
if (! $response->successful()) {
throw new Exception('Request was not successful. Unable to create DTO.');
}

return $response->json();
}
}
49 changes: 49 additions & 0 deletions src/Requests/Countries/EditACountryRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace CodebarAg\Bexio\Requests\Countries;

use CodebarAg\Bexio\Dto\Countries\CountryDTO;
use CodebarAg\Bexio\Dto\Countries\CreateEditCountryDTO;
use Exception;
use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Body\HasJsonBody;

class EditACountryRequest extends Request implements HasBody
{
use HasJsonBody;

protected Method $method = Method::POST;

public function __construct(
public readonly int $country_id,
protected readonly array|CreateEditCountryDTO $data,
) {}

public function resolveEndpoint(): string
{
return '/2.0/country/'.$this->country_id;
}

protected function defaultBody(): array
{
$body = $this->data;

if (! $body instanceof CreateEditCountryDTO) {
$body = CreateEditCountryDTO::fromArray($body);
}

return $body->toArray();
}

public function createDtoFromResponse(Response $response): CountryDTO
{
if (! $response->successful()) {
throw new Exception('Request was not successful. Unable to create DTO.');
}

return CountryDTO::fromArray($response->json());
}
}
32 changes: 32 additions & 0 deletions src/Requests/Countries/FetchACountryRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace CodebarAg\Bexio\Requests\Countries;

use CodebarAg\Bexio\Dto\Countries\CountryDTO;
use Exception;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;

class FetchACountryRequest extends Request
{
protected Method $method = Method::GET;

public function __construct(
public readonly int $country_id,
) {}

public function resolveEndpoint(): string
{
return '/2.0/country/'.$this->country_id;
}

public function createDtoFromResponse(Response $response): CountryDTO
{
if (! $response->successful()) {
throw new Exception('Request was not successful. Unable to create DTO.');
}

return CountryDTO::fromResponse($response);
}
}
Loading