Skip to content

Commit 49124cf

Browse files
committed
WIP
1 parent ec17523 commit 49124cf

28 files changed

+783
-12
lines changed

src/Dto/ManualEntries/AddFileDTO.php

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\Dto\ManualEntries;
4+
5+
use Exception;
6+
use GuzzleHttp\Psr7\Stream;
7+
use Illuminate\Support\Arr;
8+
use Spatie\LaravelData\Data;
9+
10+
class AddFileDTO extends Data
11+
{
12+
public function __construct(
13+
public string $name,
14+
public mixed $absolute_file_path_or_stream,
15+
public string $filename,
16+
) {
17+
}
18+
19+
public static function fromArray(array $data): self
20+
{
21+
if (! $data) {
22+
throw new Exception('Unable to create DTO. Data missing from response.');
23+
}
24+
25+
return new self(
26+
name: Arr::get($data, 'name'),
27+
absolute_file_path_or_stream: Arr::get($data, 'absolute_file_path_or_stream'),
28+
filename: Arr::get($data, 'filename'),
29+
);
30+
}
31+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Dto\ManualEntries;
4+
5+
use Exception;
6+
use Illuminate\Support\Arr;
7+
use Spatie\LaravelData\Data;
8+
9+
class CreateEntryDTO extends Data
10+
{
11+
public function __construct(
12+
public int $debit_account_id,
13+
public int $credit_account_id,
14+
public int $tax_id,
15+
public int $tax_account_id,
16+
public string $description,
17+
public float $amount,
18+
public int $currency_id,
19+
public int $currency_factor,
20+
) {
21+
}
22+
23+
public static function fromArray(array $data): self
24+
{
25+
if (! $data) {
26+
throw new Exception('Unable to create DTO. Data missing from response.');
27+
}
28+
29+
return new self(
30+
debit_account_id: Arr::get($data, 'debit_account_id'),
31+
credit_account_id: Arr::get($data, 'credit_account_id'),
32+
tax_id: Arr::get($data, 'tax_id'),
33+
tax_account_id: Arr::get($data, 'tax_account_id'),
34+
description: Arr::get($data, 'description'),
35+
amount: Arr::get($data, 'amount'),
36+
currency_id: Arr::get($data, 'currency_id'),
37+
currency_factor: Arr::get($data, 'currency_factor'),
38+
);
39+
}
40+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Dto\ManualEntries;
4+
5+
use Exception;
6+
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Collection;
8+
use Saloon\Http\Response;
9+
use Spatie\LaravelData\Data;
10+
11+
class CreateManualEntryDTO extends Data
12+
{
13+
public function __construct(
14+
public string $type,
15+
public string $date,
16+
public string $reference_nr,
17+
public Collection $entries,
18+
) {
19+
}
20+
21+
public static function fromResponse(Response $response): self
22+
{
23+
if ($response->failed()) {
24+
throw new \Exception('Failed to create DTO from Response');
25+
}
26+
27+
$data = $response->json();
28+
29+
return self::fromArray($data);
30+
}
31+
32+
public static function fromArray(array $data): self
33+
{
34+
if (! $data) {
35+
throw new Exception('Unable to create DTO. Data missing from response.');
36+
}
37+
38+
return new self(
39+
type: Arr::get($data, 'type'),
40+
date: Arr::get($data, 'date'),
41+
reference_nr: Arr::get($data, 'reference_nr'),
42+
entries: collect(Arr::get($data, 'entries'))->map(fn (array $entry) => EntryDTO::fromArray($entry)),
43+
);
44+
}
45+
}

src/Dto/ManualEntries/EntryDTO.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Dto\ManualEntries;
4+
5+
use Exception;
6+
use Illuminate\Support\Arr;
7+
use Saloon\Http\Response;
8+
use Spatie\LaravelData\Data;
9+
10+
class EntryDTO extends Data
11+
{
12+
public function __construct(
13+
public int $id,
14+
public string $date,
15+
public int $debit_account_id,
16+
public int $credit_account_id,
17+
public int $tax_id,
18+
public int $tax_account_id,
19+
public string $description,
20+
public float $amount,
21+
public int $currency_id,
22+
public int $base_currency_id,
23+
public int $currency_factor,
24+
public float $base_currency_amount,
25+
public int $created_by_user_id,
26+
public int $edited_by_user_id,
27+
) {
28+
}
29+
30+
public static function fromResponse(Response $response): self
31+
{
32+
if ($response->failed()) {
33+
throw new \Exception('Failed to create DTO from Response');
34+
}
35+
36+
$data = $response->json();
37+
38+
return self::fromArray($data);
39+
}
40+
41+
public static function fromArray(array $data): self
42+
{
43+
if (! $data) {
44+
throw new Exception('Unable to create DTO. Data missing from response.');
45+
}
46+
47+
return new self(
48+
id: Arr::get($data, 'id'),
49+
date: Arr::get($data, 'date'),
50+
debit_account_id: Arr::get($data, 'debit_account_id'),
51+
credit_account_id: Arr::get($data, 'credit_account_id'),
52+
tax_id: Arr::get($data, 'tax_id'),
53+
tax_account_id: Arr::get($data, 'tax_account_id'),
54+
description: Arr::get($data, 'description'),
55+
amount: Arr::get($data, 'amount'),
56+
currency_id: Arr::get($data, 'currency_id'),
57+
base_currency_id: Arr::get($data, 'base_currency_id'),
58+
currency_factor: Arr::get($data, 'currency_factor'),
59+
base_currency_amount: Arr::get($data, 'base_currency_amount'),
60+
created_by_user_id: Arr::get($data, 'created_by_user_id'),
61+
edited_by_user_id: Arr::get($data, 'edited_by_user_id'),
62+
);
63+
}
64+
}

src/Dto/ManualEntries/FileDTO.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Dto\ManualEntries;
4+
5+
use Exception;
6+
use Illuminate\Support\Arr;
7+
use Saloon\Http\Response;
8+
use Spatie\LaravelData\Data;
9+
10+
class FileDTO extends Data
11+
{
12+
public function __construct(
13+
public int $id,
14+
public string $name,
15+
public int $size_in_bytes,
16+
public string $extension,
17+
public string $mime_type,
18+
public int $user_id,
19+
public string $created_at,
20+
public ?bool $is_archived = null,
21+
public ?int $source_id = null,
22+
public ?bool $is_referenced = null,
23+
public ?string $source_type = null,
24+
public ?string $uuid = null,
25+
public ?string $uploader_email = null,
26+
public ?string $processing_source = null,
27+
public ?string $processing_status = null
28+
) {
29+
}
30+
31+
public static function fromResponse(Response $response): self
32+
{
33+
if ($response->failed()) {
34+
throw new \Exception('Failed to create DTO from Response');
35+
}
36+
37+
$data = $response->json();
38+
39+
return self::fromArray($data);
40+
}
41+
42+
public static function fromArray(array $data): self
43+
{
44+
if (! $data) {
45+
throw new Exception('Unable to create DTO. Data missing from response.');
46+
}
47+
48+
return new self(
49+
id: Arr::get($data, 'id'),
50+
name: Arr::get($data, 'name'),
51+
size_in_bytes: Arr::get($data, 'size_in_bytes'),
52+
extension: Arr::get($data, 'extension'),
53+
mime_type: Arr::get($data, 'mime_type'),
54+
user_id: Arr::get($data, 'user_id'),
55+
created_at: Arr::get($data, 'created_at'),
56+
is_archived: Arr::get($data, 'is_archived'),
57+
source_id: Arr::get($data, 'source_id'),
58+
is_referenced: Arr::get($data, 'is_referenced'),
59+
source_type: Arr::get($data, 'source_type'),
60+
uuid: Arr::get($data, 'uuid'),
61+
uploader_email: Arr::get($data, 'uploader_email'),
62+
processing_source: Arr::get($data, 'processing_source'),
63+
processing_status: Arr::get($data, 'processing_status'),
64+
);
65+
}
66+
}

src/Dto/ManualEntries/ManualEntryDTO.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Collection;
78
use Saloon\Http\Response;
89
use Spatie\LaravelData\Data;
910

@@ -13,13 +14,12 @@ public function __construct(
1314
public int $id,
1415
public string $type,
1516
public string $date,
16-
public string $name,
17-
public string $reference_number,
17+
public string $reference_nr,
1818
public int $created_by_user_id,
1919
public int $edited_by_user_id,
20-
public array $entries,
20+
public Collection $entries,
2121
public bool $is_locked,
22-
public string $locked_info,
22+
public ?string $locked_info = null,
2323
) {
2424
}
2525

@@ -44,11 +44,10 @@ public static function fromArray(array $data): self
4444
id: Arr::get($data, 'id'),
4545
type: Arr::get($data, 'type'),
4646
date: Arr::get($data, 'date'),
47-
name: Arr::get($data, 'name'),
48-
reference_number: Arr::get($data, 'reference_number'),
47+
reference_nr: Arr::get($data, 'reference_nr'),
4948
created_by_user_id: Arr::get($data, 'created_by_user_id'),
5049
edited_by_user_id: Arr::get($data, 'edited_by_user_id'),
51-
entries: Arr::get($data, 'entries'),
50+
entries: collect(Arr::get($data, 'entries'))->map(fn (array $entry) => EntryDTO::fromArray($entry)),
5251
is_locked: Arr::get($data, 'is_locked'),
5352
locked_info: Arr::get($data, 'locked_info'),
5453
);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace CodebarAg\Bexio\Requests\ManualEntries;
4+
5+
use CodebarAg\Bexio\Dto\ManualEntries\AddFileDTO;
6+
use CodebarAg\Bexio\Dto\ManualEntries\ManualEntryDTO;
7+
use Exception;
8+
use Illuminate\Support\Facades\File;
9+
use Saloon\Contracts\Body\HasBody;
10+
use Saloon\Data\MultipartValue;
11+
use Saloon\Enums\Method;
12+
use Saloon\Http\Request;
13+
use Saloon\Http\Response;
14+
use Saloon\Traits\Body\HasMultipartBody;
15+
16+
class AddFileToAccountingEntryLineRequest extends Request implements HasBody
17+
{
18+
use HasMultipartBody;
19+
20+
protected Method $method = Method::POST;
21+
22+
public function __construct(
23+
readonly int $manual_entry_id,
24+
readonly int $entry_id,
25+
readonly protected array|AddFileDTO $data,
26+
) {
27+
}
28+
29+
public function resolveEndpoint(): string
30+
{
31+
return '/3.0/accounting/manual_entries/'.$this->manual_entry_id.'/entries/'.$this->entry_id.'/files';
32+
}
33+
34+
protected function defaultBody(): array
35+
{
36+
$body = collect();
37+
38+
if ($this->data instanceof AddFileDTO) {
39+
$body->push(
40+
new MultipartValue(
41+
name: $this->data->name,
42+
value: $this->data->absolute_file_path_or_stream,
43+
filename: $this->data->filename,
44+
)
45+
);
46+
47+
ray($body->toArray());
48+
49+
return $body->toArray();
50+
}
51+
52+
foreach ($this->data as $key => &$value) {
53+
if (! $value instanceof AddFileDTO) {
54+
$value = AddFileDTO::fromArray($value);
55+
}
56+
57+
$body->push(
58+
new MultipartValue(
59+
name: $value->name,
60+
value: $value->absolute_file_path_or_stream,
61+
filename: $value->filename,
62+
)
63+
);
64+
}
65+
66+
return $body->toArray();
67+
}
68+
69+
public function createDtoFromResponse(Response $response): mixed
70+
{
71+
if (! $response->successful()) {
72+
throw new Exception('Request was not successful. Unable to create DTO.');
73+
}
74+
75+
return ManualEntryDTO::fromArray($response->json());
76+
}
77+
}

0 commit comments

Comments
 (0)