1. Go to this page and download the library: Download yorcreative/argonaut-dto library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yorcreative / argonaut-dto example snippets
use YorCreative\ArgonautDTO\ArgonautDTO;
final class UserDTO extends ArgonautDTO
{
public string $name;
public string $email;
protected array $casts = [
'name' => 'string',
'email' => 'string',
];
public function rules(): array
{
return [
'name' => ['
class ConflictingDTO extends ArgonautDTO
{
protected array $casts = ['name' => 'string']; // wins over the attribute below
#[CastWith(UppercaseCast::class)]
public mixed $name = null;
}
(new ConflictingDTO(['name' => 'ada']))->name; // 'ada', not 'ADA'
final class InvoiceSnapshotDTO extends ArgonautImmutableDTO
{
#[CastWith(MoneyCast::class)]
public readonly mixed $total;
}
(new InvoiceSnapshotDTO(['total' => 4250]))->total; // '$42.50'
use YorCreative\ArgonautDTO\ArgonautDTO;
use YorCreative\ArgonautDTO\Attributes\MapFrom;
final class ProfileDTO extends ArgonautDTO
{
protected array $maps = ['first_name' => 'firstName'];
public string $firstName = '';
#[MapFrom('last_name')]
public string $lastName = '';
}
$profile = new ProfileDTO(['first_name' => 'Ada', 'last_name' => 'Lovelace']);
$profile->firstName; // 'Ada'
$profile->lastName; // 'Lovelace'
final class ConflictingDTO extends ArgonautDTO
{
// $maps wins: the #[MapFrom] attribute on $name below is dropped
// entirely, even though 'legacy_name' and 'name_field' are different
// incoming keys.
protected array $maps = ['legacy_name' => 'name'];
#[MapFrom('name_field')]
public string $name = '';
}
$dto = new ConflictingDTO(['legacy_name' => 'Ada', 'name_field' => 'ignored']);
$dto->name; // 'Ada' — 'name_field' is no longer a recognized mapping, so it
// is just an unknown input key and is ignored.
final class AmountDTO extends ArgonautDTO
{
#[MapFrom('unit_price')]
public int $price = 0;
// Casting looks up 'price' — the property name — not 'unit_price', the
// incoming key, because mapping already ran and renamed the key.
protected array $casts = ['price' => 'integer'];
}
$amount = new AmountDTO(['unit_price' => '4200']);
$amount->price; // 4200 (int)
use YorCreative\ArgonautDTO\ArgonautImmutableDTO;
final class ContactDTO extends ArgonautImmutableDTO
{
protected array $maps = ['email_address' => 'email'];
public readonly string $email;
}
$contact = new ContactDTO(['email_address' => '[email protected]']);
$contact->email; // '[email protected]'
$updated = $contact->with(['email_address' => '[email protected]']);
$updated->email; // '[email protected]'
final class AmbiguousDTO extends ArgonautDTO
{
// fullName is renamed to 'name' on output, but 'name' is a property too.
protected array $maps = ['name' => 'fullName'];
public ?string $fullName = null;
public ?string $name = null;
}
(new AmbiguousDTO(['fullName' => 'Ada']))->toMappedArray();
// LogicException: two keys collide on 'name'
final class AddressDTO extends ArgonautDTO
{
protected array $maps = ['zip_code' => 'zip'];
public string $zip = '';
}
final class CustomerDTO extends ArgonautDTO
{
protected array $casts = ['address' => AddressDTO::class];
public ?AddressDTO $address = null;
}
$customer = new CustomerDTO(['address' => ['zip_code' => '10001']]);
$customer->toArray();
// ['address' => ['zip' => '10001']]
$customer->toMappedArray();
// ['address' => ['zip' => '10001']] — identical. toMappedArray() only
// renames CustomerDTO's own top-level keys ('address' has no mapping here,
// so it keeps its name). By the time it runs, toArray() has already
// flattened $address into a plain array — AddressDTO's own $maps has no
// object left to apply to.
$dto->toArray(); // full graph
$dto->toArray(2); // two levels of nested DTOs
$dto->toJson(depth: 2); // same limit, JSON encoded
use Illuminate\Support\Collection;
final class OrderDTO extends ArgonautDTO
{
protected array $casts = ['lines' => 'collection:'.LineDTO::class];
/** @var Collection<int, LineDTO> */
public Collection $lines;
protected function collectionClass(): string
{
return Collection::class;
}
}
'lines' => Collection::class.':'.LineDTO::class, // the class you configured
'lines' => \YorCreative\ArgonautDTO\Collection::class.':'.LineDTO::class,
'lines' => 'collection:'.LineDTO::class, // shorthand
/** @var Collection<TagDTO> */
public Collection $tags;
$this->tags->first()->name; // PHPStan resolves this to TagDTO::$name
$emails = EmailDTO::collection([
['email' => '[email protected]'],
['email' => 'not-an-email'],
]);
$emails->isValidAll(); // false
$errors = $emails->validateAll(false);
// [1 => ['email' => ['The email must be a valid email address.']]]
$emails->validateAll(); // throws the failing item's ValidationException
final class UserSnapshotDTO extends ArgonautImmutableDTO
{
public readonly string $id;
public readonly string $name;
}