PHP code example of kuaukutsu / ds-dto

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

    

kuaukutsu / ds-dto example snippets


interface Form extends Dtoable
{
    /**
     * Конвертирует объект в массив.
     *
     * @param string[] $fields поля которые должны быть в исходном массиве
     * @return array<string, mixed>
     */
    public function toArray(array $fields = []): array;
    
    /**
     * Форма содержит данные, их нужно передать в сервис (например, Service). 
     *
     * @param class-string $dtoClassName
     */
    public function toDto(string $dtoClassName): DtoInterface;
}

interface Model 
{
    public function fromDto(DtoInterface $dto): self;
}

interface Service 
{
    public function save(DtoInterface $dto): bool;
}

/**
 * @psalm-immutable
 */
final class ModelDto extends BaseDto
{
    public int $id;

    public string $name;

    /**
     * nullable в данном случае говорит что значение при Инициализации объекта может быть незадано.
     */
    public ?array $props = [];
}

class Service 
{
    public function save(DtoInterface $dto): bool
    {
        $data = $dto->toArray();

        ...
    
        return true;
    }
}