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:
- Create a metadata factory for loads metadata from objects
- 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.
fivelab/object-mapper 适用场景与选型建议
fivelab/object-mapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.14k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 08 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「object」 「mapper」 「object mapper」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 fivelab/object-mapper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 fivelab/object-mapper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 fivelab/object-mapper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
data-mapper php library
Runn Me! Value Objects Library
Convention-based Alternative ORM
Laravel SDK mapping data in object
Mapper for accessing resources in KWCMS
统计信息
- 总下载量: 1.14k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2015-08-31