tychovbh/laravel-resource-mapper
Composer 安装命令:
composer require tychovbh/laravel-resource-mapper
包简介
Map data from external Resources in Laravel
README 文档
README
Laravel Resource Mapper is created by, and is maintained by Tycho, and is a Laravel/Lumen package to map data from external Resources. Feel free to check out the change log, releases, license, and contribution guidelines
Install
Laravel Resource Mapper requires PHP 7.1 or 7.2. This particular version supports Laravel 5.5 - 5.7 only and Lumen.
To get the latest version, simply require the project using Composer.
$ composer require tychovbh/laravel-resource-mapper
Once installed, if you are not using automatic package discovery, then you need to register the Tychovbh\ResourceMapper\ResourceMapperServiceProvider service provider in your config/app.php.
In Lumen add de Service Provider in bootstrap/app.php:
$app->register(\Tychovbh\ResourceMapper\ResourceMapperServiceProvider::class);
Configuration
Laravel Resource Mapper has mapping configuration.
To get started, you'll can publish all vendor assets:
$ php artisan vendor:publish --tag=laravel-resource-mapper
This will create a config/resource-mapper.php file in your app that you can modify to set your configuration. Also, make sure you check for changes to the original config file in this package between releases.
In lumen you have to create the configuration file manually since vendor:publish is not available. Create the file config/resource-mapper.php and copy paste the example file.
You can add as many mappings as you wish give them a name that looks like your API endpoint:
use Tychovbh\ResourceMapper\RawResource; // Let's say i'm retrieving a User, Company and Person from an api: return [ 'user' => [ // this will map ['RawEmail' => 'some@email.com'] to ['email' => 'some@email.com'] 'email' => 'RawEmail' ] 'company' => [ // You can map recursive resource value as well via dot notation // this will map: ['company' => ['name' => 'Bespoke Web']] to ['company' => 'Bespoke Web'] 'company' => 'company.name' ] 'person' => [ // You can also set a callback, then a RawResource object will be available. // You can use RawResource to access all resource values and map is as you like. // this will map ['RawFirstname' => 'john'] to ['firstname' => 'John'] 'firstname' => function (RawResource $resource) { return ucfirst($resource->get('RawFirstname')); } // this will map ['firstname' => 'John', 'suffix' => 'v.', 'lastname' => 'Doe'] to ['fullname' => 'John v. Doe'] 'fullname' => function (RawResource $resource) { return $resource->join(' ', 'firstname', 'suffix', 'lastname'); } ] // You can also do some recursive array mapping 'company' => [ 'user' => function (RawResource $resource) { return $this->config('user')->map($resource->get('RawCompany.RawUser')); } ], 'users' => [ 'items' => function (RawResource $resource) { return $this->config('user')->mapCollection($resource->get('RawItems')); } ] ];
Usage
Real Examples
Instantiate ResourceMapper class:
use Tychovbh\ResourceMapper\ResourceMapper; // Use class injection Route::get('/user', function(ResourceMapper $mapper) { $res = $guzzleClient->request('GET', 'https://some-api.com/user'); $result = json_decode($res->getBody()->getContents(), true); return $mapper->config('user')->map($result); }); // Or use Laravel helper app() Route::get('/user', function() { $mapper = app('resource-mapper'); $res = $guzzleClient->request('GET', 'https://some-api.com/user'); $result = json_decode($res->getBody()->getContents(), true); return $mapper->config('user')->map($result); });
Available ResourceMapper methods:
// Map via config with $RawResource as array $mapped = $mapper->config('user')->map($RawResource) // Map via config with $RawResource as json $mapped = $mapper->config('user')->mapJson($RawResource) // Skip config file and use custom mapping $mapped = $mapper->mapping([ 'title' => 'RawTitle' ])->map($RawResource) // Map via config with a $RawResource as a Collection of items $mapped = $mapper->config('user')->mapCollection([ [ 'RawFistname' => 'John', ], [ 'RawFistname' => 'Alex', ], ])
Available RawResource methods:
$RawResource = new RawResource([ 'title' => 'my new website', 'firstname' => 'John', 'prefix' => 'v', 'lastname' => 'Doe', 'company' => [ 'name' => 'Bespoke Web' ] ]) // Get value from array $title = $RawResource->get('title') echo $title; // my new website // Get recursive value from array $company = $RawResource->get('company.name') echo $company; // Bespoke Web // Join values from array with a delimiter $title = $RawResource->join(' ', 'firstname', 'prefix, 'lastname') echo $title; // John v Doe
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email info@bespokeweb.nl instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
tychovbh/laravel-resource-mapper 适用场景与选型建议
tychovbh/laravel-resource-mapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 59 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 11 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「tychovbh」 「laravel-resource-mapper」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tychovbh/laravel-resource-mapper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tychovbh/laravel-resource-mapper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tychovbh/laravel-resource-mapper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 59
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-11-02