tychovbh/laravel-resource-mapper

在 Laravel 中映射外部资源中的数据

v0.2 2019-05-13 06:25 UTC

This package is auto-updated.

Last update: 2024-09-13 18:49:52 UTC


README

Latest Version on Packagist Software License Total Downloads

Laravel Resource Mapper 是由 Tycho 创建并维护的,是一个用于从外部资源映射数据的 Laravel/Lumen 扩展包。您可以自由查看变更日志发布版本许可协议贡献指南

安装

Laravel Resource Mapper 需要 PHP 7.1 或 7.2。本版本仅支持 Laravel 5.5 - 5.7 以及 Lumen。

要获取最新版本,只需使用 Composer 引入项目。

$ composer require tychovbh/laravel-resource-mapper

安装后,如果您没有使用自动包发现,那么您需要在您的 config/app.php 中注册 Tychovbh\ResourceMapper\ResourceMapperServiceProvider 服务提供者。

在 Lumen 中,请在 bootstrap/app.php 中添加 Service Provider。

$app->register(\Tychovbh\ResourceMapper\ResourceMapperServiceProvider::class);

配置

Laravel Resource Mapper 有映射配置。

要开始,您可以发布所有供应商资源

$ php artisan vendor:publish --tag=laravel-resource-mapper

这将创建一个位于您的应用中的 config/resource-mapper.php 文件,您可以修改它来设置配置。同时,请确保检查此包中原始配置文件在发布之间的变化。

在 Lumen 中,由于 vendor:publish 不可用,您必须手动创建配置文件。创建 config/resource-mapper.php 文件,并复制粘贴示例文件

您可以根据需要添加尽可能多的映射,并为它们提供一个类似 API 端点的名称

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'));
        }
    ]
];

用法

实际示例

实例化 ResourceMapper 类

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);
});

可用的 ResourceMapper 方法

// 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',
    ],
])

可用的 RawResource 方法

$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

变更日志

请参阅CHANGELOG了解最近的变化信息。

测试

$ composer test

贡献

请参阅CONTRIBUTING以获取详细信息。

安全

如果您发现任何安全问题,请通过info@bespokeweb.nl发送电子邮件,而不是使用问题跟踪器。

鸣谢

许可协议

MIT 许可协议 (MIT)。请参阅许可文件以获取更多信息。