Last active
September 29, 2022 17:13
-
-
Save mtvbrianking/04c0ef1711aad8590a4558eb9befd4bb to your computer and use it in GitHub Desktop.
DTO Data Transfer Objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Spatie\DataTransferObject\DataTransferObject; | |
class Customer extends DataTransferObject | |
{ | |
/** @var int|string $id */ | |
public $id; | |
public string $alias; | |
public string $name; | |
public string $email; | |
} | |
new Customer([ | |
"id" => 3, | |
"alias" => "jdoe", | |
"name" => "John Doe", | |
"email" => "[email protected]", | |
]); | |
^ Customer {#282 ▼ | |
#exceptKeys: [] | |
#onlyKeys: [] | |
+id: 3 | |
+alias: "jdoe" | |
+name: "John Doe" | |
+email: "[email protected]" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Spatie\DataTransferObject\DataTransferObject; | |
class Address extends DataTransferObject | |
{ | |
public string $country; | |
public string $region; | |
public string $district; | |
public string $street; | |
} | |
class Customer extends DataTransferObject | |
{ | |
/** @var int|string $id */ | |
public $id; | |
public string $alias; | |
public string $name; | |
public string $email; | |
public array $address; | |
} | |
new Customer([ | |
"id" => 3, | |
"alias" => "jdoe", | |
"name" => "John Doe", | |
"email" => "[email protected]", | |
"address" => [ | |
"counrty" => "Uganda", | |
"region" => "Central", | |
"district" => "Kampala", | |
"street" => "1st street Industrial Area.", | |
], | |
]); | |
^ Customer {#282 ▼ | |
#exceptKeys: [] | |
#onlyKeys: [] | |
+id: 3 | |
+alias: "jdoe" | |
+name: "John Doe" | |
+email: "[email protected]" | |
+address: array:4 [▼ | |
"counrty" => "Uganda" | |
"region" => "Central" | |
"district" => "Kampala" | |
"street" => "1st street Industrial Area." | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Spatie\DataTransferObject\DataTransferObject; | |
class Address extends DataTransferObject | |
{ | |
public string $country; | |
public string $region; | |
public string $district; | |
public string $street; | |
} | |
class Customer extends DataTransferObject | |
{ | |
/** @var int|string $id */ | |
public $id; | |
public string $alias; | |
public string $name; | |
public string $email; | |
public Address $address; | |
} | |
new Customer([ | |
"id" => 3, | |
"alias" => "jdoe", | |
"name" => "John Doe", | |
"email" => "[email protected]", | |
"address" => [ | |
"country" => "Uganda", | |
"region" => "Central", | |
"district" => "Kampala", | |
"street" => "1st street Industrial Area.", | |
], | |
]); | |
^ Customer {#282 ▼ | |
#exceptKeys: [] | |
#onlyKeys: [] | |
+id: 3 | |
+alias: "jdoe" | |
+name: "John Doe" | |
+email: "[email protected]" | |
+address: Address {#300 ▼ | |
#exceptKeys: [] | |
#onlyKeys: [] | |
+country: "Uganda" | |
+region: "Central" | |
+district: "Kampala" | |
+street: "1st street Industrial Area." | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Spatie\DataTransferObject\DataTransferObject; | |
class Address extends DataTransferObject | |
{ | |
public string $country; | |
public string $region; | |
public string $district; | |
public string $street; | |
} | |
class Customer extends DataTransferObject | |
{ | |
/** @var int|string $id */ | |
public $id; | |
public string $alias; | |
public string $name; | |
public string $email; | |
/** @var Address[] $addresses */ | |
public array $addresses; | |
} | |
new Customer([ | |
"id" => 3, | |
"alias" => "jdoe", | |
"name" => "John Doe", | |
"email" => "[email protected]", | |
"addresses" => [ | |
[ | |
"country" => "Uganda", | |
"region" => "Central", | |
"district" => "Kampala", | |
"street" => "1st street Industrial Area.", | |
], | |
], | |
]); | |
^ Customer {#282 ▼ | |
#exceptKeys: [] | |
#onlyKeys: [] | |
+id: 3 | |
+alias: "jdoe" | |
+name: "John Doe" | |
+email: "[email protected]" | |
+addresses: array:1 [▼ | |
0 => array:4 [▼ | |
"country" => "Uganda" | |
"region" => "Central" | |
"district" => "Kampala" | |
"street" => "1st street Industrial Area." | |
] | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Spatie\DataTransferObject\Caster; | |
use Spatie\DataTransferObject\DataTransferObject; | |
use Spatie\DataTransferObject\Attributes\CastWith; | |
class Address extends DataTransferObject | |
{ | |
public string $country; | |
public string $region; | |
public string $district; | |
public string $street; | |
} | |
class AddressArrayCaster implements Caster | |
{ | |
public function cast(mixed $values): array | |
{ | |
if (!\is_array($values)) { | |
throw new \Exception('Can only cast arrays to Address'); | |
} | |
return array_map(function ($value) { | |
return new Address($value); | |
}, $values); | |
} | |
} | |
class Customer extends DataTransferObject | |
{ | |
/** @var int|string $id */ | |
public $id; | |
public string $alias; | |
public string $name; | |
public string $email; | |
#[CastWith(AddressArrayCaster::class)] | |
public array $addresses; | |
} | |
$customer = new Customer([ | |
"id" => 3, | |
"alias" => "jdoe", | |
"name" => "John Doe", | |
"email" => "[email protected]", | |
"addresses" => [ | |
[ | |
"country" => "Uganda", | |
"region" => "Central", | |
"district" => "Kampala", | |
"street" => "1st street Industrial Area.", | |
], | |
], | |
]); | |
^ Customer {#282 ▼ | |
#exceptKeys: [] | |
#onlyKeys: [] | |
+id: 3 | |
+alias: "jdoe" | |
+name: "John Doe" | |
+email: "[email protected]" | |
+addresses: array:1 [▼ | |
0 => Address {#307 ▼ | |
#exceptKeys: [] | |
#onlyKeys: [] | |
+country: "Uganda" | |
+region: "Central" | |
+district: "Kampala" | |
+street: "1st street Industrial Area." | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment