bobbysciacchitano/manuel
最新稳定版本:2.0.2
Composer 安装命令:
composer require bobbysciacchitano/manuel
包简介
A simple object serializer for PHP which supports flexible document structures.
README 文档
README
The easy to use PHP Serializer. Because translation shouldn't be a chore.
How to use
To serialize an object you first need to create a transformer. Transformers must implement the transform method and return an array. Transformers can accept any type of data or object that requires serialisation.
<?php namespace App\Transformer; use TheObject; use Manuel\Transformer\TransformerAbstract; class MyTransformer extends TransformerAbstract { /** * @var string */ protected $type = 'customer'; /** * Transform object properties. * * @param TheObject $myObject * @return array */ public function transform(TheObject $myObject) { return array( 'id' => (int) $myObject->id, 'name' => $myObject->name, 'email' => $myObject->email, 'active' => (bool) $myObject->is_active ); } /** * Define other resources to be included in transformation. * * @param TheObject $myObject */ public function resources(TheObject $myObject) { $this->addLink("/customer/{$myObject->id}/tasks"); $this->addRelationship('organisation', $myObject->organisation_id); } }
To serialize your data into a resource, you can wrap it in a Resource object. Wrapping the data into in a Resource tells Manuel whether you want to return a collection or a single resource. You can also declare your own resource types.
<?php use Manuel\Manager; use Manuel\Resource; use App\Transformer; $manager = new Manager(new JsonAPISerializer); // Serialize a object $translated = $manager->translate(new Resource\Item($data, new Transfomer\MyTransformer)); // Serialize an array or collection of objects $translated = $manager->translate(new Resource\Collection($array, new Transfomer\MyTransformer));
The above transformer with the Json API serializer will generate the following representation:
{
"data": {
"id": 1,
"type": "customer",
"attributes": {
"name": "Johnny",
"email": "johnny@test.com",
"active": true
},
"relationships": {
"organisation": {
"data": {
"id": 5,
"type": "organisation"
}
},
"tasks": {
"links": {
"related": "/customer/1/tasks"
}
}
}
}
}
Associations
Manuel can handle an variety of association types. It is the responsibility of the serializer to translate the relationship into the correct format.
Simple Relationship
Simple relationships can be used to contrive additional data or return a simple value. When used with the JsonAPI serializer, this type of resource relationship can be used to declare a reference to an object that is not embedded or side-loaded.
Linked Resources
Much like simple relationships, this type of resource can be used to create a link reference to another resource that can be loaded from the API.
Embedded Resources
Much like simple relationships, embedded resources can be used to nest another resource within the resource tree. Embedded resources can be either a Collection or Item and the serializer will attempt to serialize all relationships underneath.
/** * Define other resources to be included in transformation. * * @param TheObject $myObject */ public function resources(TheObject $myObject) { $this->addResource('test_item', new Item($data->item, new Transformer)); $this->addResource('test_collection', new Collection($data->items, new Transformer)); }
Sideloaded Resources
This type of resource will be included along side the main resource and references to the resource identifiers can be loaded as part of the relationship serialization.
/** * Define other resources to be included in transformation. * * @param TheObject $myObject */ public function resources(TheObject $myObject) { $this->addResource('test_item', new Item($data->item, new Transformer), true); $this->addResource('test_collection', new Collection($data->items, new Transformer), true); }
Serializers
Manuel out of the box includes a basic implementation of the JsonAPI serializer. The abstract serializer includes a flexible API which can form the basis for your own serializer.
bobbysciacchitano/manuel 适用场景与选型建议
bobbysciacchitano/manuel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 311 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 10 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「encoder」 「data mapper」 「formatter」 「serializer」 「mapper」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bobbysciacchitano/manuel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bobbysciacchitano/manuel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bobbysciacchitano/manuel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Kinikit - PHP Application development framework MVC component
Adds the EDTF data type to Wikibase
base62 encoder and decoder also for big numbers with Laravel integration
A simple library that allows transform any kind of data to native php data or whatever
ext-json wrapper with sane defaults
统计信息
- 总下载量: 311
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-31