Esempio n. 1
0
 public function getAll()
 {
     $cards = $this->cardService->getAll();
     $cardDTOs = Papper::map($cards, 'App\\Entity\\Card')->toType('App\\DTO\\CardDTO');
     $response = new JsonResponse($cardDTOs);
     return $response;
 }
 public function testMapMultipleCardsIntoCardDTOs()
 {
     $card1 = new Card();
     $card1->setFront('front1');
     $card1->setBack('back1');
     $card2 = new Card();
     $card2->setFront('front2');
     $card2->setBack('back2');
     $cards = [$card1, $card2];
     $cardDTOs = Papper::map($cards, 'App\\Entity\\Card')->toType('App\\DTO\\CardDTO');
     $this->assertInternalType('array', $cardDTOs);
     $this->assertCount(2, $cardDTOs);
     $this->assertInstanceOf('App\\DTO\\CardDTO', $cardDTOs[0]);
     $this->assertInstanceOf('App\\DTO\\CardDTO', $cardDTOs[1]);
     $this->assertEquals('front1', $cardDTOs[0]->front);
     $this->assertEquals('back2', $cardDTOs[1]->back);
 }
    public function __construct($name)
    {
        $this->name = $name;
    }
}
class UserDTO
{
    public $name;
    public $age = "default value of ignored member";
}
class MappingConfiguration implements MappingConfigurationInterface
{
    public function configure(MappingConfigurationContext $context)
    {
        $context->createMap('Papper\\Examples\\Configure\\User', 'Papper\\Examples\\Configure\\UserDTO')->forMember('age', new Ignore());
    }
}
Papper::configureMapping(new MappingConfiguration());
/** @var UserDTO $userDTO */
$userDTO = Papper::map(new User('John Smith'))->toType('Papper\\Examples\\Configure\\UserDTO');
print_r($userDTO);
/*
 * The above example will output:
 *
 * Papper\Examples\Configure\UserDTO Object
 * (
 *     [name] => John Smith
 *     [age] => default value of ignored member
 * )
 *
 */
Esempio n. 4
0
        $this->name = $name;
    }
}
class Role
{
    public $name;
    public function __construct($name)
    {
        $this->name = $name;
    }
}
class UserDTO
{
    public $name;
    public $companyName;
    public $roleName;
}
/** @var UserDTO $userDTO */
$userDTO = Papper::map(new User('John Smith', new Company('Acme Corporation'), new Role('Developer')))->toType('Papper\\Examples\\FlatteningMapping\\UserDTO');
print_r($userDTO);
/*
 * The above example will output:
 *
 * Papper\Examples\FlatteningMapping\UserDTO Object
 * (
 *     [name] => John Smith
 *     [companyName] => Acme Corporation
 *     [roleName] => Developer
 * )
 *
 */
Esempio n. 5
0
{
    public $name;
    public function __construct($name)
    {
        $this->name = $name;
    }
}
class UserDTO
{
    public $name;
}
$users = array();
for ($i = 0; $i < 5; $i++) {
    $users[] = new User('John Smith #' . $i);
}
$usersDTOs = Papper::map($users, 'Papper\\Examples\\MappingToCollection\\User')->toType('Papper\\Examples\\MappingToCollection\\UserDTO');
print_r($usersDTOs);
/*
 * The above example will output:
 *
 * Array
 * (
 *     [0] => Papper\Examples\MappingToCollection\UserDTO Object
 *         (
 *             [name] => John Smith #0
 *         )
 *
 *     [1] => Papper\Examples\MappingToCollection\UserDTO Object
 *         (
 *             [name] => John Smith #1
 *         )
 public function run(array $sources)
 {
     return Papper::map($sources, 'Benchmap\\Domain\\User')->toType('Benchmap\\Domain\\UserDTO');
 }
Esempio n. 7
0
use Papper\ClassName;
use Papper\MemberOption\Ignore;
use Papper\Papper;
require_once __DIR__ . '/../vendor/autoload.php';
class User
{
    use ClassName;
    public $name = 'John Smith';
    public $age = 32;
}
class UserDTO
{
    use ClassName;
    public $name;
    public $age;
}
Papper::createMap(User::className(), UserDTO::className())->forMember('age', new Ignore());
/** @var UserDTO $userDTO */
$userDTO = Papper::map(new User())->toType(UserDTO::className());
print_r($userDTO);
/*
 * The above example will output:
 *
 * Papper\Examples\TraitForClassName\UserDTO Object
 * (
 *     [name] => John Smith
 *     [age] =>
 * )
 *
 */
use Papper\MemberOption\MapFrom;
use Papper\Papper;
require_once __DIR__ . '/../vendor/autoload.php';
class User
{
    public $name = 'John';
    public $family = 'Smith';
    public $ageOfUser = 32;
}
class UserDTO
{
    public $name;
    public $age;
}
Papper::createMap('Papper\\Examples\\MappingFromSpecificSourceMember\\User', 'Papper\\Examples\\MappingFromSpecificSourceMember\\UserDTO')->forMember('name', new MapFrom(function (User $source) {
    return $source->family . ' ' . $source->name;
}))->forMember('age', new MapFrom('ageOfUser'));
/** @var UserDTO $userDTO */
$userDTO = Papper::map(new User())->toType('Papper\\Examples\\MappingFromSpecificSourceMember\\UserDTO');
print_r($userDTO);
/*
 * The above example will output:
 *
 * Papper\Examples\MappingFromSpecificSourceMember\UserDTO Object
 * (
 *     [name] => Smith John
 *     [age] => 32
 * )
 *
 */
use Papper\MemberOption\Ignore;
use Papper\Papper;
require_once __DIR__ . '/../vendor/autoload.php';
class User
{
    public $name;
    public function __construct($name)
    {
        $this->name = $name;
    }
}
class UserDTO
{
    public $name;
    public $age = 'default value of ignored member';
}
$user = new User('John Smith');
Papper::createMap('Papper\\Examples\\IgnoreMemberForSkipDuringMapping\\User', 'Papper\\Examples\\IgnoreMemberForSkipDuringMapping\\UserDTO')->forMember('age', new Ignore());
/** @var UserDTO $userDTO */
$userDTO = Papper::map($user)->toType('Papper\\Examples\\IgnoreMemberForSkipDuringMapping\\UserDTO');
print_r($userDTO);
/*
 * The above example will output:
 *
 * Papper\Examples\IgnoreMemberForSkipDuringMapping\UserDTO Object
 * (
 *     [name] => John Smith
 *     [age] => default value of ignored member
 * )
 *
 */
Esempio n. 10
0
<?php

namespace Papper\Examples\Validation;

use Papper\Papper;
require_once __DIR__ . '/../vendor/autoload.php';
class User
{
    public $name;
}
class UserDTO
{
    public $name;
    public $unmappedProperty;
}
Papper::createMap('Papper\\Examples\\Validation\\User', 'Papper\\Examples\\Validation\\UserDTO');
Papper::validate();
Esempio n. 11
0
<?php

namespace Papper\Examples\MappingToStdClass;

use Papper\MemberOption\MapFrom;
use Papper\Papper;
require_once __DIR__ . '/../vendor/autoload.php';
class User
{
    public $name = 'John';
    public $family = 'Smith';
    public $age = 32;
}
Papper::createMap('Papper\\Examples\\MappingToStdClass\\User', 'stdClass')->forDynamicMember('fullname', new MapFrom(function (User $s) {
    return $s->name . ' ' . $s->family;
}))->forDynamicMember('age', new MapFrom('age'));
/** @var \stdClass $userDTO */
$userDTO = Papper::map(new User())->toType('stdClass');
print_r($userDTO);
/*
 * The above example will output:
 *
 * stdClass Object
 * (
 *     [fullname] => John Smith
 *     [age] => 32
 * )
 *
 */
use Papper\MemberOption\ConvertUsing;
use Papper\Papper;
require_once __DIR__ . '/../vendor/autoload.php';
class User
{
    public $age;
    public function __construct(\DateTime $age)
    {
        $this->age = $age;
    }
}
class UserDTO
{
    public $age;
}
$user = new User(new \DateTime('Jan 19, 2009'));
Papper::createMap('Papper\\Examples\\MappingWithValueConverter\\User', 'Papper\\Examples\\MappingWithValueConverter\\UserDTO')->forMember('age', new ConvertUsing(function (\DateTime $value) {
    return $value->diff(new \DateTime('Jun 25, 2014'))->format('%y years %m months %d days');
}));
/** @var UserDTO $userDTO */
$userDTO = Papper::map($user)->toType('Papper\\Examples\\MappingWithValueConverter\\UserDTO');
print_r($userDTO);
/*
 * The above example will output:
 *
 * Papper\Examples\MappingWithValueConverter\UserDTO Object
 * (
 *     [age] => 5 years 5 months 6 days
 * )
 *
 */
Esempio n. 13
0
<?php

namespace Papper\Examples\MappingNullValue;

use Papper\MemberOption\NullSubstitute;
use Papper\Papper;
require_once __DIR__ . '/../vendor/autoload.php';
class User
{
    public $name;
    public function __construct($name)
    {
        $this->name = $name;
    }
}
class UserDTO
{
    public $name;
}
Papper::createMap('Papper\\Examples\\MappingNullValue\\User', 'Papper\\Examples\\MappingNullValue\\UserDTO')->forMember('name', new NullSubstitute('Unknown name'));
$userDTO = Papper::map(new User(null))->toType('Papper\\Examples\\MappingNullValue\\UserDTO');
print_r($userDTO);
/*
 * The above example will output:
 *
 * Papper\Examples\MappingNullValue\UserDTO Object
 * (
 *     [name] => Unknown name
 * )
 *
 */
Esempio n. 14
0
    public function getAge()
    {
        return $this->age;
    }
}
class UserDTO
{
    public $name;
    private $age;
    public function getAge()
    {
        return $this->age;
    }
    public function setAge($age)
    {
        $this->age = $age;
    }
}
/** @var UserDTO $userDTO */
$userDTO = Papper::map(new User('John Smith', 32))->toType('Papper\\Examples\\ConventionMapping\\UserDTO');
print_r($userDTO);
/*
 * The above example will output:
 *
 * Papper\Examples\ConventionMapping\UserDTO Object
 * (
 *     [name] => John Smith
 *     [age:Papper\Examples\ConventionMapping\UserDTO:private] => 32
 * )
 *
 */
Esempio n. 15
0
    }
    public function doAfter()
    {
        $this->actions[] = 'after!';
    }
    public function getActions()
    {
        return $this->actions;
    }
}
Papper::createMap('Papper\\Examples\\BeforeAfterMap\\User', 'Papper\\Examples\\BeforeAfterMap\\UserDTO')->beforeMap(function (User $source, UserDTO $destination) {
    $destination->doBefore();
})->afterMap(function (User $source, UserDTO $destination) {
    $destination->doAfter();
});
$userDTO = Papper::map(new User('John Smith'))->toType('Papper\\Examples\\BeforeAfterMap\\UserDTO');
print_r($userDTO);
/*
 * The above example will output:
 *
 * Papper\Examples\BeforeAfterMap\UserDTO Object
 * (
 *     [name] => John Smith
 *     [actions:Papper\Examples\BeforeAfterMap\UserDTO:private] => Array
 *         (
 *             [0] => before!
 *             [1] => after!
 *         )
 *
 * )
 *
Esempio n. 16
0
require_once __DIR__ . '/../vendor/autoload.php';
class User
{
    public $name = 'John';
    public $family = 'Smith';
    public $age = 32;
}
class UserDTO
{
    public function __construct()
    {
        $this->fullname = null;
        $this->age = null;
    }
}
Papper::createMap('Papper\\Examples\\MapToDynamicProperties\\User', 'Papper\\Examples\\MapToDynamicProperties\\UserDTO')->forDynamicMember('fullname', new MapFrom(function (User $s) {
    return $s->name . ' ' . $s->family;
}))->forDynamicMember('age', new MapFrom('age'));
/** @var \stdClass $userDTO */
$userDTO = Papper::map(new User())->to(new UserDTO());
print_r($userDTO);
/*
 * The above example will output:
 *
 * Papper\Examples\MapToDynamicProperties\UserDTO Object
 * (
 *     [fullname] => John Smith
 *     [age] => 32
 * )
 *
 */
require_once __DIR__ . '/../vendor/autoload.php';
class User
{
    public $name = 'John Smith';
    public $age = 32;
}
class UserDTO
{
    private $name;
    public $age;
    public function __construct($name)
    {
        $this->name = $name;
    }
}
Papper::createMap('Papper\\Examples\\MappingWithCustomConstructor\\User', 'Papper\\Examples\\MappingWithCustomConstructor\\UserDTO')->constructUsing(function (User $user) {
    return new UserDTO($user->name);
});
/** @var UserDTO $userDTO */
$userDTO = Papper::map(new User())->toType('Papper\\Examples\\MappingWithCustomConstructor\\UserDTO');
print_r($userDTO);
/*
 * The above example will output:
 *
 * Papper\Examples\MappingWithCustomConstructor\UserDTO Object
 * (
 *     [name:Papper\Examples\MappingWithCustomConstructor\UserDTO:private] => John Smith
 *     [age] => 32
 * )
 *
 */