proklung/dto-mapper-bundle

DTO mapper bundle.

Installs: 1

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

1.0.0 2021-06-01 07:31 UTC

This package is auto-updated.

Last update: 2024-11-29 06:29:08 UTC


README

INTERNAL

Установка

composer.json:

    "repositories": [
        {
            "type": "git",
            "url": "https://github.com/proklung/dto.mapper.bundle"
        }
    ]
composer require proklung/dto-mapper-bundle

Функционал

Сервис dto_mapper_bundle.mapper с двумя методами:

  1. convert($source, $destination)
  2. convertCollection(iterable $sources, string $destination): iterable

Пример

Как-то так:

class EmployeVersionTwo
{
    /**
     * @var string $firstName
     */
    private $firstName = 'first name version two';

    /**
     * @var string $lastName
     */
    private $lastName = 'last name version two';

    /**
     * @var string $unused
     */
    private $unused = '';

    /**
     * @return string
     */
    public function getFirstName(): string
    {
        return $this->firstName;
    }

    /**
     * @return string
     */
    public function getLastName(): string
    {
        return $this->lastName;
    }
}
class EmployeDtoVersionTwo
{
    /**
     * @var string $firstName
     */
    public $firstName = 'first name dto';

    /**
     * @var string $lastName
     */
    public $lastName = 'last name dto';
}


$mapper = container()->get('dto_mapper_bundle.mapper');
$srcObj = new EmployeVersionTwo();

$result = $mapper->convert($srcObj, EmployeDtoVersionTwo::class);
var_dump($result);

Результат (замаппились свойства из исходного класса):

object(EmployeDtoVersionTwo)#7268 (2) { ["firstName"]=> string(22) "first name version two" ["lastName"]=> string(21) "last name version two" }