定制 fivelab/object-mapper 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fivelab/object-mapper

最新稳定版本:v1.0

Composer 安装命令:

composer require fivelab/object-mapper

包简介

Map array data to objects

README 文档

README

With this package, you can map array data to object instances.

Installation

Add FiveLab/ObjectMapper in your composer.json:

{
    "require": {
        "fivelab/object-mapper": "~1.0"
    }
}

Now tell composer to download the library by running the command:

$ php composer.phar update fivelab/object-mapper

Basic usage

Before use ObjectMapper you must configure instance:

  1. Create a metadata factory for loads metadata from objects
  2. Create a strategy manager
use Doctrine\Common\Annotations\AnnotationReader;
use FiveLab\Component\ObjectMapper\Strategy\StrategyManager;
use FiveLab\Component\ObjectMapper\Metadata\MetadataFactory;
use FiveLab\Component\ObjectMapper\Metadata\Loader\AnnotationLoader;
use FiveLab\Component\ObjectMapper\ObjectMapper;
use FiveLab\Component\ObjectMapper\Metadata\ObjectMetadata;

$strategyManager = StrategyManager::createDefault();

$annotationLoader = new AnnotationLoader(new AnnotationReader());
$metadataFactory = new MetadataFactory($annotationLoader);

$objectMapper = new ObjectMapper($metadataFactory, $strategyManager);

// Or create default object mapper
$objectMapper = ObjectMapper::createDefault(); // Used AnnotationLoader for load metadata

Attention: now supported only annotation metadata loader.

After configure and create instance, you can use ObjectMapper map function.

Example object for maps:

use FiveLab\Component\ObjectMapper\Annotation\Object;
use FiveLab\Component\ObjectMapper\Annotation\Property;

/**
 * @Object()
 */
class MyClass
{
    /**
     * @Property()
     */
    public $id;

    /**
     * @Property()
     */
    public $name;
}

And map array data:

$object = new MyClass();
$objectMapper->map($object, [
    'id' => 1,
    'name' => 'Foo Bar'
]);

If you want map all properties in object, you can set attribute allProperties for @Object, then this indicate for load all properties from class.

use FiveLab\Component\ObjectMapper\Annotation\Object;

/**
 * @Object(allProperties=true)
 */
class MyClass
{
    public $id;
    public $name;
}

If key of array not equals to property name of object, you can set attribute fieldName for @Property

use FiveLab\Component\ObjectMapper\Annotation\Object;
use FiveLab\Component\ObjectMapper\Annotation\Property;

/**
 * @Object()
 */
class MyClass
{
    /**
     * @Property()
     */
    public $id;

    /**
     * @Property(fieldName="first_name")
     */
    public $firstName;
}

$object = new MyClass();
$objectMapper->map($object, [
    'id' => 1,
    'first_name' => 'Foo Bar'
]);

Recursive mapping

You can recursive map data to object.

With simple object:

use FiveLab\Component\ObjectMapper\Annotation\Object;
use FiveLab\Component\ObjectMapper\Annotation\Property;

class MyClass
{
    /**
     * @DataMapping\Property(class="Tag")
     */
    protected $tag;
}

/**
 * @DataMapping\Object(allProperties=true)
 */
class Tag
{
    protected $name;
}

$object = new MyClass();
$objectMapper->map($object, [
    'tag' => [
        'name' => 'Foo Bar'
    ]
]);

With collection:

use FiveLab\Component\ObjectMapper\Annotation\Object;
use FiveLab\Component\ObjectMapper\Annotation\Property;

class MyClass
{
    /**
     * @DataMapping\Property(collection=true, class="Tag")
     */
    protected $tag;
}

/**
 * @DataMapping\Object(allProperties=true)
 */
class Tag
{
    protected $name;
}

$object = new MyClass();
$objectMapper->map($object, [
    'tag' => [
        ['name' => 'Foo Bar'],
        ['name' => 'Bar Foo']
    ]
]);

And you can set the collection class if necessary, to attribute collection collection="MyCollectionClass" All keys as default will be saved.

统计信息

  • 总下载量: 1.14k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 1

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-08-31