PHP code example of ccharz / laravel-dto-lite

1. Go to this page and download the library: Download ccharz/laravel-dto-lite 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/ */

    

ccharz / laravel-dto-lite example snippets


enum ContactType : string {
    case PERSON = 'person';
    case COMPANY = 'company';
}

class ContactData extends DataTransferObject {
    public function __construct(
        public readonly string $name,
        public readonly string $email,
        public readonly ContactType $type,
    ) {}
}

enum ContactType : string {
    case PERSON = 'person';
    case COMPANY = 'company';
}

class AddressData extends DataTransferObject
{
    public function __construct(
        public readonly ?string $country = null,
        public readonly ?string $zip = null,
        public readonly ?string $location = null,
        public readonly ?string $street = null,
        public readonly ?string $streetnumber = null,
        public readonly ?string $stair = null,
        public readonly ?string $top = null,
    ) {
    }
}

class ContactData extends DataTransferObject {
    public function __construct(
        /** @var AddressData[] $addresses */
        public readonly array $addresses,
        public readonly DateTime $birthday,
        public readonly ContactType $type,
    ) {}

    public static function casts(): array
    {
        return [
            'addresses' => AddressData::class . '[]',
            'birthday' => 'datetime',
            'type' =>  ContactType::class,
        ];
    }
}


use Illuminate\Http\Request;

public static function rules(?Request $request = null): ?array
{
    return ['prename' => 'min:2'];
}

use Illuminate\Http\Request;

public static function rules(?Request $request = null): ?array
{
    return [
        ...parent::castRules(),
    ];
}

public function store(ContactData $contactData): RedirectResponse
{
    Contact::create([
        'name' => $contactData->name,
        'email' => $contactData->email,
    ]);

    return redirect()->back();
}

class Contact extends Model
{
    protected $casts = [
        'address' => AddressData::class,
    ];
}

class Contact extends Model
{
    protected $casts = [
        'address' => AddressData::class . ':nullable',
    ];
}

class ContactController extends Controller {
    public function show(Contact $contact): ContactData
    {
        return ContactData::make($contact);
    }
}

class ContactController extends Controller {
    public function index()
    {
        return ContactData::resourceCollection(Contact::paginate());
    }
}

$addresses = [
    [
        'country' => 'AT',
        'zip' => '8010',
    ],
    [
        'country' => 'AT',
        'zip' => '8010',
    ]
];

AddressData::mapToDtoArray($addresses);

namespace App\Data;

use Spatie\TypeScriptTransformer\Attributes\TypeScript;

#[TypeScript]
class AddressData extends DataTransferObject
{
    public function __construct(
        public readonly ?string $country = null,
        public readonly ?string $zip = null,
        public readonly ?string $location = null,
        public readonly ?string $street = null,
        public readonly ?string $streetnumber = null,
        public readonly ?string $stair = null,
        public readonly ?string $top = null,
    ) {
    }
}
bash
php artisan make:dto Address