kepka42/laravel-mapper
最新稳定版本:0.1.2
Composer 安装命令:
composer require kepka42/laravel-mapper
包简介
Package for map data
README 文档
README
About
The laravel-mapper package allows you to map objects is very simply
Installation
Require the kepka42/laravel-mapper package in your composer.json and update your dependencies:
composer require kepka42/laravel-mapper
Also you need publish the config using command:
php artisan vendor:publish --provider="kepka42\LaravelMapper\MapperServiceProvider"
Usage
For create mapper you can you command:
php artisan make:mapper NameOfMapper
This command will create class of mapper in Mapper folder which is located in app directory of your application:
<?php class NameOfMapper extends AbstractMapper { protected $sourceType = ""; protected $hintType = "" /** * @param $object * @param array $params * @return mixed */ public function map($object, $params = []) { // TODO: Map here } }
$sourceType is type which you want to map.
$hintType is type in which you want to map.
Function map is your realisation of functional to map object with type$sourceType to object with type $hintType
Also you need add you mapper in config/mapper.php:
<?php return [ 'mappers' => [ NameOfMapper::class ] ];
That's all. You can map objects using Facade:
<?php $result = Mapper::map($object, HintType::class);
or contract:
<?php $result = $mapperContract->map($object, HintType::class);
Example
I have DTO class:
<?php namespace App\Domain; /** * Class SearchInfo * @package App\Domain */ class SearchInfo { /** @var string */ private $name; /** @var string */ private $address; /** * SearchInfo constructor. * @param string $name * @param string $address */ public function __construct( string $name, string $address ) { $this->name = $name; $this->address = $address; } /** * @return string */ public function getName(): string { return $this->name; } /** * @param string $name */ public function setName(string $name): void { $this->name = $name; } /** * @return string */ public function getAddress(): string { return $this->address; } /** * @param string $address */ public function setAddress(string $address): void { $this->address = $address; } }
and i want map Request to SearchInfo.
I need to create mapper:
php artisan make:mapper RequestToSearchInfoMapper
and write map function:
<?php namespace App\Mappers; use App\Domain\SearchInfo; use Illuminate\Http\Request; use kepka42\LaravelMapper\Mapper\AbstractMapper; /** * Class RequestToSearchInfoMapper * @package App\Mappers */ class RequestToSearchInfoMapper extends AbstractMapper { protected $sourceType = Request::class; protected $hintType = SearchInfo::class; /** * @param Request $object * @param array $params * @return SearchInfo */ public function map($object, $params = []) { return new SearchInfo( $object->get('name'), $object->get('address') ); } }
Now i need add my mapper to config file:
<?php return [ 'mappers' => [ \App\Mappers\RequestToSearchInfoMapper::class, ] ];
And now i can use this mapper. We'll do it in controller:
<?php // ... use kepka42\LaravelMapper\Facades\Mapper; // ... public function index(Request $request): Response { $requestInfo = Mapper::map($request, SearchInfo::class); //... return new Response('{}'); }
or:
<?php // ... use kepka42\LaravelMapper\Contracts\MapperContract; // ... public function index(Request $request, MapperContract $mapperContract): Response { $requestInfo = $mapperContract->map($request, SearchInfo::class); //... return new Response('{}'); }
License
Released under the MIT License, see LICENSE.
kepka42/laravel-mapper 适用场景与选型建议
kepka42/laravel-mapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 953 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 10 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「map」 「mapper」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kepka42/laravel-mapper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kepka42/laravel-mapper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kepka42/laravel-mapper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
data-mapper php library
Maps in minutes. Powered by the Google Maps API.
Yii2 map input widget. Allows you to select geographcal coordinates via a human-friendly inteface.
Convention-based Alternative ORM
Mapper for accessing resources in KWCMS
Tests for auth-mapper implementations.
统计信息
- 总下载量: 953
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-10-19