Bundle makes the process of binding all your data mapping rules a lot more easy by providing annotation declarations. You can extract content from objects and fill objects from raw arrays. Instead of slow and dummy reflection classes, the bundle uses fast and performance optimized code generator. All mapping and DI configurations use native symfony cache and lazy loading.
VKMapperBundle uses customized performance strategies:
- Converts
Object
toarray
- Extracts
Object
toarray
- Puts data from
array
intoObject
The suggested installation method's via composer:
php composer.phar require vklymniuk/dto-mapper-bundle
Register bundle in bundles.php file.
<?php
return [
VKMapperBundle\VKMapperBundle::class => ['all' => true],
];
Tag directory with classes that you want add to mapping.
Tests\DataFixtures\Dto\:
resource: '../../DataFixtures/Dto/*'
tags:
- { name: dto_mapper.annotated }
Tests\DataFixtures\Model\:
resource: '../../DataFixtures/Model/*'
tags:
- { name: dto_mapper.annotated }
Fill in the directory you tagged with annotated classes. Class example:
<?php
namespace Tests\DataFixtures\Dto;
use VKMapperBundle\Annotation\MappingMeta\SourceClass;
use VKMapperBundle\Annotation\MappingMeta\DestinationClass;
use VKMapperBundle\Annotation\MappingMeta\Strategy;
/**
* @SourceClass
*/
class Source
{
/**
* @Strategy\XPathStrategy(xPath="nodeA.inner.optionA")
*/
public $nodeA;
}
/**
* @DestinationClass
*/
class Destination
{
/**
* Contains value optionA.
*/
public $nodeA;
}
Inject MapperInterface into your service.
<?php
use DataMapper\MapperInterface;
class DataTransformer
{
public function __construct(MapperInterface $mapper);
}
Convert source to dto object:
<?php
use DataMapper\MapperInterface;
/** @var MapperInterface $mapper */
$mapper->convert($source, $destination);
Extract content from object into array:
<?php
use DataMapper\MapperInterface;
/** @var MapperInterface $mapper */
$mapper->extract($object);
You can learn more about the bundle possibilities and how to use the VKMapperBundle in the docs.