1. Go to this page and download the library: Download opportus/object-mapper 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/ */
opportus / object-mapper example snippets
use Opportus\ObjectMapper\Point\PointFactory;
use Opportus\ObjectMapper\Route\RouteBuilder;
use Opportus\ObjectMapper\Map\MapBuilder;
use Opportus\ObjectMapper\ObjectMapper;
$pointFactory = new PointFactory();
$routeBuilder = new RouteBuilder($pointFactory);
$mapBuilder = new MapBuilder($routeBuilder);
$objectMapper = new ObjectMapper($mapBuilder);
class User
{
private $username;
public function __construct(string $username)
{
$this->username = $username;
}
public function getUsername(): string
{
return $this->username;
}
}
class UserDto
{
public $username;
}
$user = new User('Toto');
$userDto = new UserDto();
// Map the data of the User instance to the UserDto instance
$objectMapper->map($user, $userDto);
echo $userDto->username; // Toto
// Map the data of the UserDto instance to a new User instance
$user = $objectMapper->map($userDto, User::class);
echo $user->getUsername(); // Toto
class DynamicUserDto {}
$user = new User('Toto');
$userDto = new DynamicUserDto();
// Build the map
$map = $mapBuilder
->addStaticSourceToDynamicTargetPathFinder()
->getMap();
// Map the data of the User instance to the DynamicUserDto instance
$objectMapper->map($user, $userDto, $map);
echo $userDto->username; // Toto
class DynamicUserDto {}
$userDto = new DynamicUserDto();
$userDto->username = 'Toto';
// Build the map
$map = $mapBuilder
->addDynamicSourceToStaticTargetPathFinder()
->getMap();
// Map the data of the DynamicUserDto instance to a new User instance
$user = $objectMapper->map($userDto, User::class, $map);
echo $user->getUsername(); // Toto
class MyPathFinder implements PathFinderInterface
{
private $routeBuilder;
// ...
public function getRoutes(SourceInterface $source, TargetInterface $target): RouteCollection
{
$source->getClassReflection();
$target->getClassReflection();
$routes = [];
/**
* Custom mapping algorithm based on source/target relection and
* possibly their data...
*
* Use route builder to build routes...
*/
return new RouteCollection($routes);
}
}
// Pass to the map builder pathfinders you want it to compose the map of
$map = $mapBuilder
->addStaticPathFinder()
->addPathFinder(new MyPathFinder($routeBuilder))
->getMap();
// Use the map
$user = $objectMapper->map($userDto, User::class, $map);
class User
{
private $username;
public function __construct(string $username)
{
$this->username = $username;
}
public function getUsername(): string
{
return $this->username;
}
}
class ContributorDto
{
public $name;
}
$user = new User('Toto');
$contributorDto = new ContributorDto();
// Define the route manually
$map = $mapBuilder
->getRouteBuilder()
->setStaticSourcePoint('User::getUsername()')
->setStaticTargetPoint('ContributorDto::$name')
->addRouteToMapBuilder()
->getMapBuilder()
->getMap();
// Map the data of the User instance to the ContributorDto instance
$objectMapper->map($user, $contributorDto, $map);
echo $contributorDto->name; // Toto
// Define the route manually
$map = $mapBuilder
->getRouteBuilder()
->setStaticSourcePoint('ContributorDto::$name')
->setStaticTargetPoint('User::__construct()::$username')
->addRouteToMapBuilder()
->getMapBuilder()
->getMap();
// Map the data of the ContributorDto instance to a new User instance
$user = $objectMapper->map($contributorDto, User::class, $map);
echo $user->getUsername(); // 'Toto'
class Contributor
{
private $bio;
public function __construct(string $bio)
{
$this->bio = $bio;
}
public function getBio(): string
{
return $this->bio;
}
}
class ContributorView
{
public $bio;
}
class GenericViewHtmlTagStripper implements CheckPointInterface
{
public function control($value, RouteInterface $route, MapInterface $map, SourceInterface $source, TargetInterface $target)
{
return \strip_tags($value);
}
}
class GenericViewMarkdownTransformer implements CheckPointInterface
{
// ...
public function control($value, RouteInterface $route, MapInterface $map, SourceInterface $source, TargetInterface $target)
{
return $this->markdownParser->transform($value);
}
}
class GenericPresentation extends StaticPathFinder
{
// ...
public function getRoutes(Source $source, Target $target): RouteCollection
{
$routes = parent::getRoutes($source, $target);
$controlledRoutes = [];
foreach ($routes as $route) {
$controlledRoutes[] = $this->routeBuilder
->setSourcePoint($route->getSourcePoint()->getFqn())
->setTargetPoint($route->getTargetPoint()->getFqn())
->addCheckPoint(new GenericViewHtmlTagStripper(), 10)
->addCheckPoint(new GenericViewMarkdownTransformer($this->markdownParser), 20)
->getRoute();
}
return new RouteCollection($controlledRoutes);
}
}
$contributor = new Contributor('<script>**Hello World!**</script>');
$map = $mapBuilder
->addPathFinder(new GenericPresentation($markdownTransformer))
->getMap();
$contributorView = $objectMapper->map($contributor, ContributorView::class, $map);
echo $contributorView->bio; // <b>Hello World!</b>
class Post
{
public Author $author;
public Comment[] $comments;
}
class Author
{
public string $name;
}
class Comment
{
public Author $author;
}
class PostDto {}
class AuthorDto {}
class CommentDto {}
$comment1 = new Comment();
$comment1->author = new Author();
$comment1->author->name = 'clem';
$comment2 = new Comment();
$comment2->author = new Author();
$comment2->author->name = 'bob';
$post = new Post();
$post->author = new Author();
$post->author->name = 'Martin Fowler';
$post->comments = [$comment1, $comment2];
// Let's map the Post instance above and its composites to a new PostDto instance and DTO composites...
$mapBuilder
->getRouteBuilder
->setStaticSourcePoint('Post::$author')
->setDynamicTargetPoint('PostDto::$author')
->addRecursionCheckPoint('Author', 'AuthorDto', 'PostDto::$author') // Mapping also Post's Author to PostDto's AuthorDto
->addRouteToMapBuilder()
->setStaticSourcePoint('Comment::$author')
->setDynamicTargetPoint('CommentDto::$author')
->addRecursionCheckPoint('Author', 'AuthorDto', 'CommentDto::$author') // Mapping also Comment's Author to CommentDto's AuthorDto
->addRouteToMapBuilder()
->setStaticSourcePoint('Post::$comments')
->setDynamicTargetPoint('PostDto::$comments')
->addIterableRecursionCheckPoint('Comment', 'CommentDto', 'PostDto::$comments') // Mapping also Post's Comment's to PostDto's CommentDto's
->addRouteToMapBuilder()
->getMapBuilder()
->addStaticSourceToDynamicTargetPathFinder()
->getMap();
$postDto = $objectMapper->($post, PostDto::class, $map)
get_class($postDto); // PostDto
get_class($postDto->author); // AuthorDto
echo $postDto->author->name; // Matin Fowler
get_class($postDto->comments[0]); // CommentDto
get_class($postDto->comments[0]->author); // AuthorDto
echo $postDto->comments[0]->author->name; // clem
get_class($postDto->comments[1]); // CommentDto
get_class($postDto->comments[1]->author); // AuthorDto
echo $postDto->comments[1]->author->name; // bob