nilportugues/api-transformer
Composer 安装命令:
composer require nilportugues/api-transformer
包简介
Base library providing the core functionality for API transformation.
README 文档
README
Purpose
This library provides the core functionality for API transformation and is a base library for many other packages.
By itself it's not usable at all. Check the projects using it below.
Used by
Currently the following transformers make use of this library as foundation:
Installation
Use Composer to install the package:
$ composer require nilportugues/api-transformer
How it works
Loading must be done using the Mapper class, as it expects an array containing a defined structure, or a class name implementing ApiMapping.
There are 2 styles for flexibility when integrating the library into PHP frameworks.
While I discourage having 2 styles for mapping it is well possible to have them side by side in the very same configuration file. The Mapper class that loads all Mappings does internal transformation for both, so client does not have to worry.
Usage
use NilPortugues\Api\Mapping\Mapper; use NilPortugues\AcmeProject\Infrastructure\Api\Mappings\PostApiMapping; $arrayConfig = include 'mappings.php'; $classConfig = [ PostApiMapping::class, ]; $mappings = array_merge($classConfig, $arrayConfig); //Now $mapper can be passed to a Transformer. $mapper = new Mapper($mappings);
Creating the Mapping files
Implementing ApiMapping (Prefered method)
To create a Mapping you may implement the following interfaces:
ApiMapping: transform your data into plain JSON for API consumtion or the JSend API format.JsonApiMappingto transform your data into the JSONAPI 1.0 standard.HalMappingto transform your data to HAL+JSON and HAL+XML API standards.
As expected you may implement many interfaces to support multiple API formats.
<?php namespace NilPortugues\AcmeProject\Infrastructure\Api\Mappings; use NilPortugues\AcmeProject\Blog\Domain\Post; use NilPortugues\Api\Mappings\HalMapping; use NilPortugues\Api\Mappings\JsonApiMapping; class PostApiMapping implements JsonApiMapping, HalMapping { /** * {@inheritdoc} */ public function getClass() : string { return Post::class; } /** * {@inheritdoc} */ public function getAlias() : string { return 'Posting'; //If none is used 'Post' will be used instead. } /** * {@inheritdoc} */ public function getAliasedProperties() : array { return [ 'title' => 'headline', 'content' => 'body', ]; } /** * {@inheritdoc} */ public function getHideProperties() : array { return [ 'comments', ]; } /** * {@inheritdoc} */ public function getIdProperties() : array { return [ 'postId', ]; } /** * {@inheritdoc} */ public function getUrls() : array { return [ // Mandatory 'self' => 'http://example.com/posts/{postId}', // Optional 'comments' => 'http://example.com/posts/{postId}/comments', ]; } /** * Returns an array of curies. * * @return array */ public function getCuries() : array { return [ 'name' => 'example', 'href' => 'http://example.com/docs/rels/{rel}', ]; } /** * {@inheritdoc} */ public function getRelationships() : array { return [ 'author' => [ 'related' => 'http://example.com/posts/{postId}/author', 'self' => 'http://example.com/posts/{postId}/relationships/author', ], ]; } }
Mapping using an array
// mappings.php return [ [ 'class' => Post::class, 'alias' => 'Posting', //If none is used 'Post' will be used instead. 'aliased_properties' => [ 'title' => 'headline', 'content' => 'body', ], 'hide_properties' => [ 'comments', ], 'id_properties' => [ 'postId', ], 'urls' => [ // Mandatory 'self' => 'http://example.com/posts/{postId}', // Optional 'comments' => 'http://example.com/posts/{postId}/comments', ], // (Optional) Used by HAL+JSON / HAL+XML 'curies' => [ 'name' => 'example', 'href' => 'http://example.com/docs/rels/{rel}', ], // (Optional) Used by JSONAPI 'relationships' => [ 'author' => [ 'related' => 'http://example.com/posts/{postId}/author', 'self' => 'http://example.com/posts/{postId}/relationships/author', ], ] ], ];
Quality
To run the PHPUnit tests at the command line, go to the tests directory and issue phpunit.
This library attempts to comply with PSR-1, PSR-2, PSR-4 and PSR-7.
If you notice compliance oversights, please send a patch via Pull Request.
Contribute
Contributions to the package are always welcome!
- Report any bugs or issues you find on the issue tracker.
- You can grab the source code at the package's Git repository.
Support
Get in touch with me using one of the following means:
- Emailing me at contact@nilportugues.com
- Opening an Issue
Authors
License
The code base is licensed under the MIT license.
nilportugues/api-transformer 适用场景与选型建议
nilportugues/api-transformer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 109.6k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2015 年 08 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「response」 「serializer」 「transformer」 「psr7」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nilportugues/api-transformer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nilportugues/api-transformer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nilportugues/api-transformer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Bridge between JMS Serializer Bundle and Superdesk Web Publisher.
De/normalize business objects without tightly coupling them to your normalization format
HTTP request logger middleware for Laravel
A PSR-7 compatible library for making CRUD API endpoints
Class to generate a standard structure for api json responses
Smokescreen is a PHP library for transforming and serializing data such as API responses.
统计信息
- 总下载量: 109.6k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 9
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-08-14