五实验室 / 对象映射器
此包已被废弃,不再维护。未建议替代包。
将数组数据映射到对象
v1.0
2015-08-31 09:05 UTC
Requires
- php: >=5.4
- fivelab/cache: ~1.0
- fivelab/exception: ~1.0
- fivelab/reflection: ~1.0
Requires (Dev)
- doctrine/annotations: ~1.0
- phpunit/phpunit: 4.*
Suggests
- doctrine/annotations: For read metadata from annotations
This package is not auto-updated.
Last update: 2017-08-16 12:52:02 UTC
README
使用此包,您可以将 array
数据映射到 object
实例。
安装
在您的 composer.json 中添加 FiveLab/ObjectMapper
{ "require": { "fivelab/object-mapper": "~1.0" } }
现在运行以下命令让 composer 下载库
$ php composer.phar update fivelab/object-mapper
基本用法
在使用 ObjectMapper 之前,您必须配置实例
- 创建一个元数据工厂以从对象加载元数据
- 创建一个策略管理器
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
注意:现在只支持注解元数据加载器。
配置并创建实例后,您可以使用 ObjectMapper
的映射功能。
映射示例对象
use FiveLab\Component\ObjectMapper\Annotation\Object; use FiveLab\Component\ObjectMapper\Annotation\Property; /** * @Object() */ class MyClass { /** * @Property() */ public $id; /** * @Property() */ public $name; }
并映射数组数据
$object = new MyClass(); $objectMapper->map($object, [ 'id' => 1, 'name' => 'Foo Bar' ]);
如果您想映射对象中的所有属性,您可以为 @Object 设置属性 allProperties,这表示从类加载所有属性。
use FiveLab\Component\ObjectMapper\Annotation\Object; /** * @Object(allProperties=true) */ class MyClass { public $id; public $name; }
如果数组的键不等于对象的属性名,您可以为 @Property 设置属性 fieldName
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' ]);
递归映射
您可以将数据递归映射到对象。
使用简单对象
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' ] ]);
使用集合
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'] ] ]);
如果需要,您可以设置集合类,将其属性 collection 设置为 collection="MyCollectionClass"
所有键将默认保存。