承接 jimmy4fingers/simple-data-mapper 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jimmy4fingers/simple-data-mapper

Composer 安装命令:

composer require jimmy4fingers/simple-data-mapper

包简介

Data mapping class

README 文档

README

Build Status

A lightweight PHP package that implements the Data Mapper pattern to convert raw data into application objects while keeping concerns cleanly separated.

Problem

In a legacy CodeIgniter application with no dependency injection, form handling logic was hard-coded, tightly coupled, and spread across large controllers. Form data structures were difficult to reuse or modify, and there was no clean way to sanitize, clean, or tweak inputs before submission.

What it does

This package allows you to define complete form data as objects and apply any number of transformation functions to clean or modify values before submission. It centralizes form data mapping and processing, improving flexibility and maintainability without requiring changes to the underlying framework.

Collection of objects intended to cut down repetition of code when having to re-map arrays of data with another source (DB field's etc.)

E.g:

<?php

// form data
$postData = [
    'my-form-field-name' => 'jim',
    'my-form-field-email' => 'jim@email.com',
    'my-form-field-age' => 36
];

// data to insert
$entityData = [
    'name' => $postData['my-form-field-name'],
    'email_address' => $postData['my-form-field-email'],
    'users_age' => $postData['my-form-field-age']
];

if you want to also add validation or additional logic then you would expect to reference the key\value pair again and so on...

Alternatively, we can add a mapping object.

E.g:

<?php

use DataMapper\Map;
use DataMapper\MapCollection;
use DataMapper\Mapper;

$map = new Map();
$collection = new MapCollection();
$mapper = new Mapper();

// post data example
$data = [
    'my-form-field-name' => 'jim'
];

// create a collection of mappings
$collection->add(
    // [application reference], [lookup key]
    $map->set('my-form-field-name', 'name')
);

// pass data into the mapper along with the collection
$mapper->set($collection, $data);

$name = $mapper->get('my-form-field-name')->getData();
// $name = 'jim';

While the above seems trivial, let's try and reverse the process. We want to load some data and assign it to our application key ('my-form-field-name').

<?php

use DataMapper\Map;
use DataMapper\MapCollection;
use DataMapper\Mapper;

$map = new Map();
$collection = new MapCollection();
$mapper = new Mapper();

// loaded data example
$data = ['name' => 'jimmy'];

// create a collection of mappings
$collection->add($map->set('my-form-field-name', 'name'));

// the last parameter tells the mapper to map data using the lookup key
$mapper->set($collection, $data, true);

$name = $mapper->get('my-form-field-name')->getData();
// $name = 'jimmy';

Additional functionality

You can pass anonymous functions to be triggered when data is loaded in via Mapper::set. The anonymous function must accept a parameter of data and also return it.

  • setOnMap() - triggered when mapping data via key
  • setOnMapByLookup() - triggered when setting data via lookup key

E.g:

<?php
use DataMapper\Map;
use DataMapper\MapCollection;
use DataMapper\Mapper;

$map = new Map();
$collection = new MapCollection();
$mapper = new Mapper();

$ucwordsCallback = function ($data) {
    return ucwords($data);
};

$maps = [
    $map->set('first-name','first_names')->setOnMap($ucwordsCallback),
    $map->set('last-name','last_name')->setOnMap($ucwordsCallback),
    $map->set('email','email_address'),
    $map->set('address_line1','line_1'),
    $map->set('address_line1','line_2'),
    $map->set('address_line1','line_3')
];
foreach($maps as $map) {
    $collection->add($map);
}

// posted data example
$postData = [
    'first-name' => 'jimmy jams',
    'last-name' => 'higgins',
];

$mapper->set($collection, $postData);

$myData = $mapper->getArray();
// $myData = ['first-name' => 'Jimmy Jams' etc...];
  • setDataFrom()

If the array of data you are mapping contains data you wish to set on a Map but is not referenced by a mapping key or lookup key, you can use the Map::setDataFrom($key).

E.g:

<?php
// data your mapping
$post = [
    'jsonData' => ['city1', 'city2', 'city3']
];

$map = new Map();
$collection = new MapCollection();
$mapper = new Mapper();

$cb1 = function ($data) {
    $data = json_decode($data,true);
    if (is_array($data) && array_key_exists(0,$data))
       $data = $data[0];
    return $data;
};
$cb2 = function ($data) {
     $data = json_decode($data,true);
     if (is_array($data) && array_key_exists(1,$data))
        $data = $data[1];
     return $data;
 };
 
$maps = [
    $map->set('city1','city_1')->setDataFrom('jsonData')->setOnMap($cb1),
    $map->set('city2','city_2')->setDataFrom('jsonData')->setOnMap($cb2)
];
foreach($maps as $map) {
    $collection->add($map);
}

$mapper->set($collection, $post);

$myData = $mapper->getArray();
// $myData = ['city1' => 'city1', 'city2' => 'city2']

Extend

The Map object can set a few more values that you can use to extend the functionality of Mapper::class.

  • setValidation()
  • setFormObject()

E.g:

<?php
use DataMapper\Map;
use DataMapper\MapCollection;
use DataMapper\Mapper;

$map = new Map();
$collection = new MapCollection();
$mapper = new Mapper();

$maps = [
    $map->set('first-name','first_names')->setValidation('required'),
    $map->set('last-name','last_name')->setValidation('required'),
    $map->set('email','email_address')->setValidation('required|email'),
];
foreach($maps as $map) {
    $collection->add($map);
}

$mapper->set($collection, ['first-name'=>'bob', 'last-name'=>'']);

$validation = new ValidationObject();
$validation->validateMapped($mapper->get()); // Mapper::get returns MapInterface[]
if ($validation->isValid()) {
    //...
}

jimmy4fingers/simple-data-mapper 适用场景与选型建议

jimmy4fingers/simple-data-mapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 08 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 jimmy4fingers/simple-data-mapper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jimmy4fingers/simple-data-mapper 我们能提供哪些服务?
定制开发 / 二次开发

基于 jimmy4fingers/simple-data-mapper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2018-08-22