Skip to content

Commit 1bf30d1

Browse files
authored
Merge pull request #54 from codebar-ag/feature-countries
Feature Countries
2 parents 5cec431 + d3fd800 commit 1bf30d1

35 files changed

+897
-48
lines changed

.phpunit.cache/test-results

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ This package was developed to give you a quick start to the Bexio API.
4646
- [Contact Relations](#contact-relations)
4747
- [Contacts](#contacts)
4848
- [Contact Sectors](#contact-sectors)
49+
- [Countries](#countries)
4950
- [Currencies](#currencies)
5051
- [Files](#files)
5152
- [Iban Payments](#iban-payments)
@@ -681,9 +682,10 @@ We provide enums for the following values:
681682
| 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() |
682683
| 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() |
683684
| ContactSectors: OrderByEnum | ID(), ID_ASC(), ID_DESC(), NAME(), NAME_ASC(), NAME_DESC() |
685+
| Countries: CountriesOrderByEnum | ID(), ID_ASC(), ID_DESC(), NAME(), NAME_ASC(), NAME_DESC(), NAME_SHORT(), NAME_SHORT_ASC(), NAME_SHORT_DESC() |
684686
| IbanPayments: AllowanceTypeEnum | FEE_PAID_BY_SENDER(), FEE_PAID_BY_RECIPIENT(), FEE_SPLIT(), NO_FEE() |
685687
| IbanPayments: StatusEnum | OPEN(), TRANSFERRED(), DOWNLOADED(), ERROR(), CANCELLED() |
686-
| Items: OrderByEnum | ID(), ID_ASC(), ID_DESC(), INTERN_NAME(), INTERN_NAME_ASC(), INTERN_NAME_DESC() |
688+
| Items: ItemsOrderByEnum | ID(), ID_ASC(), ID_DESC(), INTERN_NAME(), INTERN_NAME_ASC(), INTERN_NAME_DESC() |
687689
| ManualEntries: TypeEnum | MANUAL_SINGLE_ENTRY(), MANUAL_GROUP_ENTRY(), MANUAL_COMPOUND_ENTRY() |
688690
| QrPayments: AllowanceTypeEnum | FEE_PAID_BY_SENDER(), FEE_PAID_BY_RECIPIENT(), FEE_SPLIT(), NO_FEE() |
689691
| QrPayments: StatusEnum | OPEN(), TRANSFERRED(), DOWNLOADED(), ERROR(), CANCELLED() |
@@ -2082,6 +2084,69 @@ $response = $connector->send(new DeleteAnItemRequest(
20822084
));
20832085
```
20842086

2087+
### Countries
2088+
```php
2089+
/**
2090+
* Fetch A List Of Countries
2091+
*/
2092+
$countries = $connector->send(new FetchAListOfCountriesRequest())->dto();
2093+
```
2094+
2095+
```php
2096+
/**
2097+
* Fetch A Country
2098+
*/
2099+
$country = $connector->send(new FetchACountryRequest(
2100+
country_id: 1
2101+
))->dto();
2102+
```
2103+
2104+
```php
2105+
/**
2106+
* Search Countries
2107+
*/
2108+
$countries = $connector->send(new SearchCountriesRequest(
2109+
searchField: 'name',
2110+
searchTerm: 'Switzerland'
2111+
))->dto();
2112+
```
2113+
2114+
```php
2115+
/**
2116+
* Create Country
2117+
*/
2118+
$country = $connector->send(new CreateCountryRequest(
2119+
data: new CreateEditCountryDTO(
2120+
name: 'Switzerland',
2121+
name_short: 'CH',
2122+
iso3166_alpha2: 'CH'
2123+
)
2124+
))->dto();
2125+
```
2126+
2127+
```php
2128+
/**
2129+
* Edit Country
2130+
*/
2131+
$country = $connector->send(new EditACountryRequest(
2132+
country_id: 1,
2133+
data: new CreateEditCountryDTO(
2134+
name: 'Switzerland',
2135+
name_short: 'CH',
2136+
iso3166_alpha2: 'CH'
2137+
)
2138+
))->dto();
2139+
```
2140+
2141+
```php
2142+
/**
2143+
* Delete Country
2144+
*/
2145+
$response = $connector->send(new DeleteACountryRequest(
2146+
country_id: 1
2147+
));
2148+
```
2149+
20852150
### VAT Periods
20862151
```php
20872152
/**

src/Dto/Countries/CountryDTO.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Dto\Countries;
4+
5+
use Exception;
6+
use Illuminate\Support\Arr;
7+
use Saloon\Http\Response;
8+
use Spatie\LaravelData\Data;
9+
10+
class CountryDTO extends Data
11+
{
12+
public function __construct(
13+
public int $id,
14+
public string $name,
15+
public string $name_short,
16+
public string $iso3166_alpha2,
17+
) {}
18+
19+
public static function fromResponse(Response $response): self
20+
{
21+
if ($response->failed()) {
22+
throw new \Exception('Failed to create DTO from Response');
23+
}
24+
25+
$data = $response->json();
26+
27+
return self::fromArray($data);
28+
}
29+
30+
public static function fromArray(array $data): self
31+
{
32+
if (! $data) {
33+
throw new Exception('Unable to create DTO. Data missing from response.');
34+
}
35+
36+
return new self(
37+
id: Arr::get($data, 'id'),
38+
name: Arr::get($data, 'name'),
39+
name_short: Arr::get($data, 'name_short'),
40+
iso3166_alpha2: Arr::get($data, 'iso_3166_alpha2'),
41+
);
42+
}
43+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Dto\Countries;
4+
5+
use Exception;
6+
use Illuminate\Support\Arr;
7+
use Saloon\Http\Response;
8+
use Spatie\LaravelData\Data;
9+
10+
class CreateEditCountryDTO extends Data
11+
{
12+
public function __construct(
13+
public string $name,
14+
public string $name_short,
15+
public string $iso3166_alpha2,
16+
) {}
17+
18+
public static function fromResponse(Response $response): self
19+
{
20+
if ($response->failed()) {
21+
throw new \Exception('Failed to create DTO from Response');
22+
}
23+
24+
$data = $response->json();
25+
26+
return self::fromArray($data);
27+
}
28+
29+
public static function fromArray(array $data): self
30+
{
31+
if (! $data) {
32+
throw new Exception('Unable to create DTO. Data missing from response.');
33+
}
34+
35+
return new self(
36+
name: Arr::get($data, 'name'),
37+
name_short: Arr::get($data, 'name_short'),
38+
iso3166_alpha2: Arr::get($data, 'iso3166_alpha2'),
39+
);
40+
}
41+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Enums\Countries;
4+
5+
use Spatie\Enum\Laravel\Enum;
6+
7+
/**
8+
* @method static self ID()
9+
* @method static self ID_ASC()
10+
* @method static self ID_DESC()
11+
* @method static self NAME()
12+
* @method static self NAME_ASC()
13+
* @method static self NAME_DESC()
14+
* @method static self NAME_SHORT()
15+
* @method static self NAME_SHORT_ASC()
16+
* @method static self NAME_SHORT_DESC()
17+
*/
18+
final class CountriesOrderByEnum extends Enum
19+
{
20+
protected static function values(): array
21+
{
22+
return [
23+
'ID' => 'id',
24+
'ID_ASC' => 'id_asc',
25+
'ID_DESC' => 'id_desc',
26+
'NAME' => 'name',
27+
'NAME_ASC' => 'name_asc',
28+
'NAME_DESC' => 'name_desc',
29+
'NAME_SHORT' => 'name_short',
30+
'NAME_SHORT_ASC' => 'name_short_asc',
31+
'NAME_SHORT_DESC' => 'name_short_desc',
32+
];
33+
}
34+
35+
protected static function labels(): array
36+
{
37+
return [
38+
'ID' => 'Id',
39+
'ID_ASC' => 'Id Ascending',
40+
'ID_DESC' => 'Id Descending',
41+
'NAME' => 'Name',
42+
'NAME_ASC' => 'Name Ascending',
43+
'NAME_DESC' => 'Name Descending',
44+
'NAME_SHORT' => 'Name Short',
45+
'NAME_SHORT_ASC' => 'Name Short Ascending',
46+
'NAME_SHORT_DESC' => 'Name Short Descending',
47+
];
48+
}
49+
}

src/Enums/Items/OrderByEnum.php renamed to src/Enums/Items/ItemsOrderByEnum.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* @method static self INTERN_NAME_ASC()
1313
* @method static self INTERN_NAME_DESC()
1414
*/
15-
final class OrderByEnum extends Enum
15+
final class ItemsOrderByEnum extends Enum
1616
{
1717
protected static function values(): array
1818
{
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\Countries;
4+
5+
use CodebarAg\Bexio\Dto\Countries\CountryDTO;
6+
use CodebarAg\Bexio\Dto\Countries\CreateEditCountryDTO;
7+
use Exception;
8+
use Saloon\Contracts\Body\HasBody;
9+
use Saloon\Enums\Method;
10+
use Saloon\Http\Request;
11+
use Saloon\Http\Response;
12+
use Saloon\Traits\Body\HasJsonBody;
13+
14+
class CreateCountryRequest extends Request implements HasBody
15+
{
16+
use HasJsonBody;
17+
18+
protected Method $method = Method::POST;
19+
20+
public function __construct(
21+
protected readonly array|CreateEditCountryDTO $data,
22+
) {}
23+
24+
public function resolveEndpoint(): string
25+
{
26+
return '/2.0/country';
27+
}
28+
29+
protected function defaultBody(): array
30+
{
31+
$body = $this->data;
32+
33+
if (! $body instanceof CreateEditCountryDTO) {
34+
$body = CreateEditCountryDTO::fromArray($body);
35+
}
36+
37+
return $body->toArray();
38+
}
39+
40+
public function createDtoFromResponse(Response $response): CountryDTO
41+
{
42+
if (! $response->successful()) {
43+
throw new Exception('Request was not successful. Unable to create DTO.');
44+
}
45+
46+
return CountryDTO::fromArray($response->json());
47+
}
48+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\Countries;
4+
5+
use Exception;
6+
use Saloon\Enums\Method;
7+
use Saloon\Http\Request;
8+
use Saloon\Http\Response;
9+
10+
class DeleteACountryRequest extends Request
11+
{
12+
protected Method $method = Method::DELETE;
13+
14+
public function __construct(
15+
public readonly int $country_id,
16+
) {}
17+
18+
public function resolveEndpoint(): string
19+
{
20+
return '/2.0/country/'.$this->country_id;
21+
}
22+
23+
public function createDtoFromResponse(Response $response): mixed
24+
{
25+
if (! $response->successful()) {
26+
throw new Exception('Request was not successful. Unable to create DTO.');
27+
}
28+
29+
return $response->json();
30+
}
31+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\Countries;
4+
5+
use CodebarAg\Bexio\Dto\Countries\CountryDTO;
6+
use CodebarAg\Bexio\Dto\Countries\CreateEditCountryDTO;
7+
use Exception;
8+
use Saloon\Contracts\Body\HasBody;
9+
use Saloon\Enums\Method;
10+
use Saloon\Http\Request;
11+
use Saloon\Http\Response;
12+
use Saloon\Traits\Body\HasJsonBody;
13+
14+
class EditACountryRequest extends Request implements HasBody
15+
{
16+
use HasJsonBody;
17+
18+
protected Method $method = Method::POST;
19+
20+
public function __construct(
21+
public readonly int $country_id,
22+
protected readonly array|CreateEditCountryDTO $data,
23+
) {}
24+
25+
public function resolveEndpoint(): string
26+
{
27+
return '/2.0/country/'.$this->country_id;
28+
}
29+
30+
protected function defaultBody(): array
31+
{
32+
$body = $this->data;
33+
34+
if (! $body instanceof CreateEditCountryDTO) {
35+
$body = CreateEditCountryDTO::fromArray($body);
36+
}
37+
38+
return $body->toArray();
39+
}
40+
41+
public function createDtoFromResponse(Response $response): CountryDTO
42+
{
43+
if (! $response->successful()) {
44+
throw new Exception('Request was not successful. Unable to create DTO.');
45+
}
46+
47+
return CountryDTO::fromArray($response->json());
48+
}
49+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\Countries;
4+
5+
use CodebarAg\Bexio\Dto\Countries\CountryDTO;
6+
use Exception;
7+
use Saloon\Enums\Method;
8+
use Saloon\Http\Request;
9+
use Saloon\Http\Response;
10+
11+
class FetchACountryRequest extends Request
12+
{
13+
protected Method $method = Method::GET;
14+
15+
public function __construct(
16+
public readonly int $country_id,
17+
) {}
18+
19+
public function resolveEndpoint(): string
20+
{
21+
return '/2.0/country/'.$this->country_id;
22+
}
23+
24+
public function createDtoFromResponse(Response $response): CountryDTO
25+
{
26+
if (! $response->successful()) {
27+
throw new Exception('Request was not successful. Unable to create DTO.');
28+
}
29+
30+
return CountryDTO::fromResponse($response);
31+
}
32+
}

0 commit comments

Comments
 (0)