Skip to content
This repository was archived by the owner on Nov 4, 2025. It is now read-only.

Commit 163212d

Browse files
author
Niek Brekelmans
committed
wip on casting to DTO
1 parent c9e9768 commit 163212d

File tree

10 files changed

+115
-15
lines changed

10 files changed

+115
-15
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"bensampo/laravel-enum": "^5.3",
2121
"illuminate/contracts": "^8.0|^9.0",
2222
"sammyjo20/saloon-laravel": "^1.4",
23+
"spatie/data-transfer-object": "^3.8",
2324
"spatie/laravel-package-tools": "^1.9.2"
2425
},
2526
"require-dev": {

src/HetznerDnsClient.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
use Sammyjo20\Saloon\Http\SaloonConnector;
66
use Sammyjo20\Saloon\Traits\Plugins\AcceptsJson;
77

8+
/**
9+
* @method RequestCollections\ZoneCollection zones()
10+
* @method RequestCollections\RecordCollection records()
11+
*/
812
class HetznerDnsClient extends SaloonConnector
913
{
1014
use AcceptsJson;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace DutchCodingCompany\HetznerDnsClient\Objects\Casters;
4+
5+
use Carbon\Carbon;
6+
use Spatie\DataTransferObject\Caster;
7+
8+
class CarbonCaster implements Caster
9+
{
10+
public function cast(mixed $value): ?Carbon
11+
{
12+
return empty($value) ? null : (new Carbon($value));
13+
}
14+
}

src/Objects/Zone.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace DutchCodingCompany\HetznerDnsClient\Objects;
4+
5+
use Carbon\Carbon;
6+
use Spatie\DataTransferObject\Attributes\DefaultCast;
7+
use Spatie\DataTransferObject\DataTransferObject;
8+
9+
#[
10+
DefaultCast(Carbon::class, Casters\CarbonCaster::class),
11+
]
12+
class Zone extends DataTransferObject
13+
{
14+
public string $id;
15+
public string $name;
16+
public int $ttl;
17+
public ?string $registrar;
18+
public ?string $legacy_dns_host;
19+
public ?array $legacy_ns;
20+
public array $ns;
21+
public Carbon $created;
22+
public ?Carbon $verified;
23+
public Carbon $modified;
24+
public string $owner;
25+
public string $permission;
26+
public array $zone_type;
27+
public string $status;
28+
public bool $paused;
29+
public bool $is_secondary_dns;
30+
public array $txt_verification;
31+
public int $records_count;
32+
}

src/Objects/ZoneCollection.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace DutchCodingCompany\HetznerDnsClient\Objects;
4+
5+
use Carbon\Carbon;
6+
use Spatie\DataTransferObject\Attributes\CastWith;
7+
use Spatie\DataTransferObject\Attributes\DefaultCast;
8+
use Spatie\DataTransferObject\Casters\ArrayCaster;
9+
use Spatie\DataTransferObject\DataTransferObject;
10+
11+
#[
12+
DefaultCast(Carbon::class, Casters\CarbonCaster::class),
13+
]
14+
class ZoneCollection extends DataTransferObject
15+
{
16+
/** @var Zone[] */
17+
#[CastWith(ArrayCaster::class, itemType: Zone::class)]
18+
public array $zones;
19+
}

src/RequestCollections/RecordCollection.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,36 @@ class RecordCollection extends RequestCollection
1515
{
1616
public function all(...$arguments): array
1717
{
18-
return $this->connector->request(new ListRecords(...$arguments))->send()->json();
18+
return $this->connector->request(new ListRecords(...$arguments))->send()->throw()->json();
1919
}
2020

2121
public function create(...$arguments): array
2222
{
23-
return $this->connector->request(new CreateRecord(...$arguments))->send()->json();
23+
return $this->connector->request(new CreateRecord(...$arguments))->send()->throw()->json();
2424
}
2525

2626
public function get(...$arguments): array
2727
{
28-
return $this->connector->request(new GetRecord(...$arguments))->send()->json();
28+
return $this->connector->request(new GetRecord(...$arguments))->send()->throw()->json();
2929
}
3030

3131
public function update(...$arguments): array
3232
{
33-
return $this->connector->request(new UpdateRecord(...$arguments))->send()->json();
33+
return $this->connector->request(new UpdateRecord(...$arguments))->send()->throw()->json();
3434
}
3535

3636
public function delete(...$arguments): array
3737
{
38-
return $this->connector->request(new DeleteRecord(...$arguments))->send()->json();
38+
return $this->connector->request(new DeleteRecord(...$arguments))->send()->throw()->json();
3939
}
4040

4141
public function bulkCreate(...$arguments): array
4242
{
43-
return $this->connector->request(new BulkCreateRecords(...$arguments))->send()->json();
43+
return $this->connector->request(new BulkCreateRecords(...$arguments))->send()->throw()->json();
4444
}
4545

4646
public function bulkUpdate(...$arguments): array
4747
{
48-
return $this->connector->request(new BulkUpdateRecords(...$arguments))->send()->json();
48+
return $this->connector->request(new BulkUpdateRecords(...$arguments))->send()->throw()->json();
4949
}
5050
}

src/RequestCollections/ZoneCollection.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace DutchCodingCompany\HetznerDnsClient\RequestCollections;
44

5+
use DutchCodingCompany\HetznerDnsClient\Objects\Zone;
56
use DutchCodingCompany\HetznerDnsClient\Requests\Zones\CreateZone;
67
use DutchCodingCompany\HetznerDnsClient\Requests\Zones\DeleteZone;
78
use DutchCodingCompany\HetznerDnsClient\Requests\Zones\ExportZone;
@@ -14,31 +15,31 @@ class ZoneCollection extends RequestCollection
1415
{
1516
public function all(...$arguments): array
1617
{
17-
return $this->connector->request(new ListZones(...$arguments))->send()->json();
18+
return $this->connector->request(new ListZones(...$arguments))->send()->throw()->dto();
1819
}
1920

2021
public function create(...$arguments): array
2122
{
22-
return $this->connector->request(new CreateZone(...$arguments))->send()->json();
23+
return $this->connector->request(new CreateZone(...$arguments))->send()->throw()->json();
2324
}
2425

25-
public function get(...$arguments): array
26+
public function get(...$arguments): ?Zone
2627
{
27-
return $this->connector->request(new GetZone(...$arguments))->send()->json();
28+
return $this->connector->request(new GetZone(...$arguments))->send()->throw()->dto();
2829
}
2930

3031
public function update(...$arguments): array
3132
{
32-
return $this->connector->request(new UpdateZone(...$arguments))->send()->json();
33+
return $this->connector->request(new UpdateZone(...$arguments))->send()->throw()->json();
3334
}
3435

3536
public function delete(...$arguments): array
3637
{
37-
return $this->connector->request(new DeleteZone(...$arguments))->send()->json();
38+
return $this->connector->request(new DeleteZone(...$arguments))->send()->throw()->json();
3839
}
3940

4041
public function export(...$arguments): array
4142
{
42-
return $this->connector->request(new ExportZone(...$arguments))->send()->json();
43+
return $this->connector->request(new ExportZone(...$arguments))->send()->throw()->json();
4344
}
4445
}

src/Requests/Zones/CreateZone.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
namespace DutchCodingCompany\HetznerDnsClient\Requests\Zones;
44

55
use DutchCodingCompany\HetznerDnsClient\HetznerDnsClient;
6+
use DutchCodingCompany\HetznerDnsClient\Objects\Zone;
67
use Sammyjo20\Saloon\Constants\Saloon;
78
use Sammyjo20\Saloon\Http\SaloonRequest;
9+
use Sammyjo20\Saloon\Http\SaloonResponse;
10+
use Sammyjo20\Saloon\Traits\Plugins\CastsToDto;
811
use Sammyjo20\Saloon\Traits\Plugins\HasJsonBody;
912

1013
class CreateZone extends SaloonRequest
1114
{
12-
use HasJsonBody;
15+
use HasJsonBody, CastsToDto;
1316

1417
public function __construct(
1518
protected string $name,
@@ -33,4 +36,9 @@ public function defaultData(): array
3336
'ttl' => $this->ttl,
3437
]);
3538
}
39+
40+
protected function castToDto(SaloonResponse $response): Zone
41+
{
42+
dd($response->json());
43+
}
3644
}

src/Requests/Zones/GetZone.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
namespace DutchCodingCompany\HetznerDnsClient\Requests\Zones;
44

55
use DutchCodingCompany\HetznerDnsClient\HetznerDnsClient;
6+
use DutchCodingCompany\HetznerDnsClient\Objects\Zone;
67
use Illuminate\Support\Arr;
78
use Sammyjo20\Saloon\Constants\Saloon;
89
use Sammyjo20\Saloon\Http\SaloonRequest;
10+
use Sammyjo20\Saloon\Http\SaloonResponse;
11+
use Sammyjo20\Saloon\Traits\Plugins\CastsToDto;
912

1013
class GetZone extends SaloonRequest
1114
{
15+
use CastsToDto;
16+
1217
public function __construct(
1318
protected string $zone_id,
1419
)
@@ -22,4 +27,9 @@ public function defineEndpoint(): string
2227
{
2328
return '/zones/'.$this->zone_id;
2429
}
30+
31+
protected function castToDto(SaloonResponse $response): Zone
32+
{
33+
return new Zone($response->json('zone'));
34+
}
2535
}

src/Requests/Zones/ListZones.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
namespace DutchCodingCompany\HetznerDnsClient\Requests\Zones;
44

55
use DutchCodingCompany\HetznerDnsClient\HetznerDnsClient;
6+
use DutchCodingCompany\HetznerDnsClient\Objects\ZoneCollection;
67
use Illuminate\Support\Arr;
78
use Sammyjo20\Saloon\Constants\Saloon;
89
use Sammyjo20\Saloon\Http\SaloonRequest;
10+
use Sammyjo20\Saloon\Http\SaloonResponse;
11+
use Sammyjo20\Saloon\Traits\Plugins\CastsToDto;
912

1013
class ListZones extends SaloonRequest
1114
{
15+
use CastsToDto;
16+
1217
public function __construct(
1318
protected ?string $name = null,
1419
protected ?int $page = null,
@@ -35,4 +40,10 @@ public function defaultQuery(): array
3540
'search_name' => $this->search_name,
3641
]);
3742
}
43+
44+
protected function castToDto(SaloonResponse $response): mixed
45+
{
46+
47+
dd(new ZoneCollection($response->json()));
48+
}
3849
}

0 commit comments

Comments
 (0)