PHP code example of wendelladriel / laravel-validated-dto

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

    

wendelladriel / laravel-validated-dto example snippets


use Illuminate\Validation\Rules\Password;
use WendellAdriel\ValidatedDTO\Casting\BooleanCast;
use WendellAdriel\ValidatedDTO\Casting\StringCast;
use WendellAdriel\ValidatedDTO\ValidatedDTO;

final class UserDTO extends ValidatedDTO
{
    public string $name;

    public string $email;

    public string $password;

    public bool $active;

    protected function rules(): array
    {
        return [
            'name' => ['           'password' => new StringCast(),
            'active' => new BooleanCast(),
        ];
    }
}

$dto = UserDTO::fromArray([
    'name' => 'John Doe',
    'email' => '[email protected]',
    'password' => 's3CreT!@1a2B',
]);

$dto->name; // John Doe
$dto->toArray();

use Illuminate\Http\JsonResponse;

final class StoreUserController
{
    public function __invoke(UserDTO $dto): JsonResponse
    {
        return response()->json($dto->toArray());
    }
}
bash
php artisan vendor:publish --tag="validated-dto"