Skip to content

Instantly share code, notes, and snippets.

@mtvbrianking
Last active September 29, 2022 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtvbrianking/04c0ef1711aad8590a4558eb9befd4bb to your computer and use it in GitHub Desktop.
Save mtvbrianking/04c0ef1711aad8590a4558eb9befd4bb to your computer and use it in GitHub Desktop.
DTO Data Transfer Objects
<?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" => "jdoe@example.local",
]);
^ Customer {#282 ▼
#exceptKeys: []
#onlyKeys: []
+id: 3
+alias: "jdoe"
+name: "John Doe"
+email: "jdoe@example.local"
}
<?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" => "jdoe@example.local",
"address" => [
"counrty" => "Uganda",
"region" => "Central",
"district" => "Kampala",
"street" => "1st street Industrial Area.",
],
]);
^ Customer {#282 ▼
#exceptKeys: []
#onlyKeys: []
+id: 3
+alias: "jdoe"
+name: "John Doe"
+email: "jdoe@example.local"
+address: array:4 [▼
"counrty" => "Uganda"
"region" => "Central"
"district" => "Kampala"
"street" => "1st street Industrial Area."
]
}
<?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" => "jdoe@example.local",
"address" => [
"country" => "Uganda",
"region" => "Central",
"district" => "Kampala",
"street" => "1st street Industrial Area.",
],
]);
^ Customer {#282 ▼
#exceptKeys: []
#onlyKeys: []
+id: 3
+alias: "jdoe"
+name: "John Doe"
+email: "jdoe@example.local"
+address: Address {#300 ▼
#exceptKeys: []
#onlyKeys: []
+country: "Uganda"
+region: "Central"
+district: "Kampala"
+street: "1st street Industrial Area."
}
}
<?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" => "jdoe@example.local",
"addresses" => [
[
"country" => "Uganda",
"region" => "Central",
"district" => "Kampala",
"street" => "1st street Industrial Area.",
],
],
]);
^ Customer {#282 ▼
#exceptKeys: []
#onlyKeys: []
+id: 3
+alias: "jdoe"
+name: "John Doe"
+email: "jdoe@example.local"
+addresses: array:1 [▼
0 => array:4 [▼
"country" => "Uganda"
"region" => "Central"
"district" => "Kampala"
"street" => "1st street Industrial Area."
]
]
}
<?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" => "jdoe@example.local",
"addresses" => [
[
"country" => "Uganda",
"region" => "Central",
"district" => "Kampala",
"street" => "1st street Industrial Area.",
],
],
]);
^ Customer {#282 ▼
#exceptKeys: []
#onlyKeys: []
+id: 3
+alias: "jdoe"
+name: "John Doe"
+email: "jdoe@example.local"
+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