class User
{
    public $name = 'John';
    public $family = 'Smith';
}
class UserDTO
{
    public $name;
    public $family;
    public $unmappedProperty1;
    public $unmappedProperty2;
    public $unmappedProperty3;
    public $unmappedProperty4;
    public $unmappedProperty5;
}
Papper::createMap('Papper\\Examples\\IgnoreAllNonExisting\\User', 'Papper\\Examples\\IgnoreAllNonExisting\\UserDTO')->ignoreAllNonExisting();
/** @var UserDTO $userDTO */
$userDTO = Papper::map(new User())->to(new UserDTO());
print_r($userDTO);
/*
 * The above example will output:
 *
 * Papper\Examples\IgnoreAllNonExisting\UserDTO Object
 * (
 *     [name] => John
 *     [family] => Smith
 *     [unmappedProperty1] =>
 *     [unmappedProperty2] =>
 *     [unmappedProperty3] =>
 *     [unmappedProperty4] =>
 *     [unmappedProperty5] =>
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
 * )
 *
 */
use Papper\MemberOption\Ignore;
use Papper\Papper;
require_once __DIR__ . '/../vendor/autoload.php';
class User
{
    public $name = 'John Smith';
}
class UserDTO
{
    public $id;
    public $name;
    public function __construct($id)
    {
        $this->id = $id;
    }
}
Papper::createMap('Papper\\Examples\\MappingToExistsObject\\User', 'Papper\\Examples\\MappingToExistsObject\\UserDTO')->forMember('id', new Ignore());
/** @var UserDTO $userDTO */
$userDTO = Papper::map(new User())->to(new UserDTO(123));
print_r($userDTO);
/*
 * The above example will output:
 *
 * Papper\Examples\MappingToExistsObject\UserDTO Object
 * (
 *     [id] => 123
 *     [name] => John Smith
 * )
 *
 */
 public function prepare()
 {
     Papper::createMap('Benchmap\\Domain\\User', 'Benchmap\\Domain\\UserDTO');
 }
示例#5
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] =>
 * )
 *
 */
示例#6
0
    public $name;
    private $actions = array();
    public function doBefore()
    {
        $this->actions[] = 'before!';
    }
    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!
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
 * )
 *
 */
示例#8
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();
示例#9
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
 * )
 *
 */
示例#11
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
 * )
 *
 */
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
 * )
 *
 */
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
 * )
 *
 */