PHP code example of dezer32 / dto

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

    

dezer32 / dto example snippets




declare(strict_types=1);

use Dezer32\Libraries\Dto\DataTransferObject;

class TestDto extends DataTransferObject
{
    public function __construct(
        private string $text,
    ) {
    }
    
    public function getText(): string
    {
        return $this->text;
    }
}



declare(strict_types=1);

use Dezer32\Libraries\Dto\Attributes\DataTransferObject;

#[DataTransferObject]
class TestDto
{
    public function __construct(
        private string $text,
    ) {
    }
    
    public function getText(): string
    {
        return $this->text;
    }
}



declare(strict_types=1);

use Dezer32\Libraries\Dto\Transformer;

$parameters = [
    'text' => 'test'
];
$dto = Transformer::transform(TestDto::class, $parameters);
//or
$dto = new TestDto('test');





declare(strict_types=1);

use Dezer32\Libraries\Dto\Contracts\CasterInterface;

class CasterClass implements CasterInterface
{
    public function cast(mixed $value): string
    {
        return 'New string and ' . $value;
    }
}



declare(strict_types=1);

use Dezer32\Libraries\Dto\Attributes\Cast;

class TestDto
{
    public function __construct(
        #[Cast(CasterClass::class)]
        private string $text,
    ) {
    }

    public function getText(): string
    {
        return $this->text;
    }
}



declare(strict_types=1);

use Dezer32\Libraries\Dto\Transformer;

$dto = Transformer::transform(TestDto::class, ['text' => 'test text.']);

echo $dto->getText(); //New string and test text.



declare(strict_types=1);

use Dezer32\Libraries\Dto\Attributes\DefaultCast;
use Dezer32\Libraries\Dto\Contracts\CasterInterface;
use Dezer32\Libraries\Dto\Transformer;

class DefaultCasterClass implements CasterInterface
{
    public function cast(mixed $value): AnotherClass
    {
        return new AnotherClass('New string and ' . $value);
    }
}

class AnotherClass
{
    public function __construct(
        private string $text
    ) {
    }

    public function getText(): string
    {
        return $this->text;
    }
}

#[DefaultCast(AnotherClass::class, DefaultCasterClass::class)]
class TestDto
{
    public function __construct(
        private AnotherClass $object,
    ) {
    }

    public function getObject(): AnotherClass
    {
        return $this->object;
    }
}

$dto = Transformer::transform(TestDto::class, ['text' => 'test text.']);

echo $dto->getObject()->getText(); //New string and test text.



declare(strict_types=1);

use Dezer32\Libraries\Dto\Contracts\CasterInterface;

class DefaultCasterClass implements CasterInterface
{
    public function cast(mixed $value): AnotherClass
    {
        return new AnotherClass('New string and ' . $value);
    }
}



declare(strict_types=1);

class AnotherClass
{
    public function __construct(
        private string $text
    ) {
    }

    public function getText(): string
    {
        return $this->text;
    }
}



declare(strict_types=1);

use Dezer32\Libraries\Dto\Attributes\DefaultCast;

#[DefaultCast(AnotherClass::class, DefaultCasterClass::class)]
#[DataTransferObject]
class TestDto
{
    public function __construct(
        private AnotherClass $object,
    ) {
    }

    public function getObject(): AnotherClass
    {
        return $this->object;
    }
}



declare(strict_types=1);

use Dezer32\Libraries\Dto\Transformer;

$dto = Transformer::transform(TestDto::class, ['text' => 'test text.']);

echo $dto->getObject()->getText(); //New string and test text.



declare(strict_types=1);

use Dezer32\Libraries\Dto\Attributes\DataTransferObject;

#[DataTransferObject]
class SimpleDto
{
    public function __construct(
        private string $text
    ) {
    }

    public function getText(): string
    {
        return $this->text;
    }
}



declare(strict_types=1);

use Dezer32\Libraries\Dto\Attributes\Cast;
use Dezer32\Libraries\Dto\Attributes\DataTransferObject;
use Dezer32\Libraries\Dto\Casters\ArrayCaster;

#[DataTransferObject]
class SimpleListDto
{
    public function __construct(
        #[Cast(ArrayCaster::class, SimpleDto::class)]
        private array $list
    ) {
    }

    /** @return SimpleDto[] */
    public function getList(): array
    {
        return $this->list;
    }
}