PHP code example of sj / dtomatic

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

    

sj / dtomatic example snippets


return [

    'strict_types' => true, //If enabled, Dtomatic will throw an exception when a type mismatch occurs.
    'date_format' => 'Y-m-d H:i:s', // The default date format used when converting DateTime objects to strings

    //You can register custom type converters here. When mapping a value,
    //Dtomatic will check if a converter exists for its type and use it.
    'custom_converters' => [   
        // Example:
        // 'string' => \App\Converters\JsonToArrayConverter::class,
    ],
];

    use ShihabJamil\Dtomatic\Attributes\ArrayOf;
    
    class UserDTO
    {
        #[ArrayOf(PostDTO::class)]
        public array $posts;
    }

    use ShihabJamil\Dtomatic\Attributes\Ignore;

    class PostDTO
    {
        #[Ignore]
        public string $internalNotes;
    }

    use ShihabJamil\Dtomatic\Attributes\Converter;

    class PostDTO
    {
        #[Converter(MyCustomConverter::class)]
        public string $specialField;
    }

namespace App\DTO;

class UserDTO
{
    public int $id;
    public string $name;
    public string $email;
}

use App\Models\User;
use Dtomatic\Facades\ModelMapper;
use App\DTO\UserDTO;

$user = User::find(1);
$dto = ModelMapper::map($user, UserDTO::class);

return response()->json($dto);

class PostDTO {
    public int $id;
    public string $title;
    public UserDTO $author;
}

$post = Post::with('author')->find(1);
$dto = ModelMapper::map($post, PostDTO::class);

    
    
    namespace App\Dto;
    
    use ShihabJamil\Dtomatic\Attributes\ArrayOf;
    
    class UserDTO
    {
        public int $id;
        public string $name;
    
        #[ArrayOf(PostDTO::class)]
        public array $posts;
    }
    
    class PostDTO
    {
        public int $id;
        public string $title;
    }

    $userModel = User::with('posts')->find(1);
    $userDto = ModelMapper::map($userModel, UserDTO::class);
    
    // $userDto->posts is now an array of PostDTO objects


$users = User::all();
$dtoList = ModelMapper::mapCollection($users, UserDTO::class);

namespace App\Converters;

class UppercaseConverter
{
    public function convert(string $value): string
    {
        return strtoupper($value);
    }
}

use Dtomatic\Attributes\Converter;

class UserDTO
{
    public int $id;

    #[Converter(\App\Converters\UppercaseConverter::class)]
    public string $name;

    public string $email;
}

'custom_converters' => [
    'string' => \App\Converters\JsonToArrayConverter::class,
]

namespace App\Converters;

class JsonToArrayConverter
{
    public function convert($value): mixed
    {
        if (is_string($value) && $this->isJson($value)) {
            return json_decode($value, true);
        }
        return $value;
    }
    
    private function isJson(string $string): bool
    {
        json_decode($string);
        return json_last_error() === JSON_ERROR_NONE;
    }
}

    class UserPreferencesDTO
    {
        public array $preferences;
    }

    $user = new \App\Models\User();
    $user->preferences = '{"theme":"dark","notifications":true}';

    $dto = Dtomatic::map($user, UserPreferencesDTO::class);
    print_r($dto->preferences);
    // Output:
    // [
    //   "theme" => "dark",
    //   "notifications" => true
    // ]

use Dtomatic\Attributes\Ignore;

class UserDTO
{
    public int $id;

    #[Ignore]
    public string $password;
}

public function getFullName()
{
    return "{$this->first_name} {$this->last_name}";
}

class UserDTO
{
    public string $full_name;
}
bash
php artisan vendor:publish --provider="Dtomatic\DtomaticServiceProvider" --tag=dtomatic-config

config/dtomatic.php