Php Mapping Merge
ty-mapping-merge; package is multiple data with do mapping and merge results., (*1)
Object and Array usable., (*2)
For Example:
Post and User relation map and merge.
Map: Relation with key, Post (fromUserId) == User(id)
Merge: Related data merge of with selected field (fromUser), (*3)
// post data
$posts = array(
array('id'=> 1, 'title'=> 'happy years 2018', 'fromUserId'=> 1),
array('id'=> 2, 'title'=> 'i want to see you', 'fromUserId'=> 1),
array('id'=> 3, 'title'=> 'Champion RealMadrid', 'fromUserId'=> 2),
array('id'=> 4, 'title'=> 'love php', 'fromUserId'=> 3),
);
// user data
$users = array(
array('id'=> 1, 'name'=> 'George', 'avatar'=> 'user1.jpg'),
array('id'=> 2, 'name'=> 'Jack', 'avatar'=> 'user2.jpg'),
array('id'=> 3, 'name'=> 'Oscar', 'avatar'=> 'user3.jpg'),
);
Result:, (*4)
Array
(
[0] => Array
(
[id] => 1
[title] => happy years 2018
[fromUserId] => 1
[fromUser] => Array
(
[id] => 1
[name] => George
[avatar] => user1.jpg
)
)
)
Use Case
Different data source merge at one result
Relational data sources create or merge
Create data by Microservice results, combine
Aggregate data for multiple results, (*5)
Microservice
For example social app, microservice architecture:
- Post Microservice
- User Microservice
- Like Microservice
- Comment Microservice
- Metric Microservice
etc., (*6)
Step by step data aggregate, (*7)
posts = postService->getPosts();
ids = posts.extractIds();
fromUserIds = posts.extractFromUserIds();
// get result sets by per microservice
users = userService->getByIds(fromUserIds);
likes = likeService->getByIds(ids);
comments = commentService->getByIds(ids);
metrics = metricService->getByIds(ids);
Data map, aggregate, combine, merge, join?
Use to TyMappingMerge., (*8)
How To Use
/*
$posts = array( ... );
$users = array( ... );
... defines codes ...
*/
// option user
$optionUser = new MapOption();
$optionUser->setVar($users);
$optionUser->setProp('fromUser');
$optionUser->setMapField('fromUserId');
$optionUser->setTargetMapField('id');
// set options
$options[] = $optionUser;
// mapping
$mapping = new OptionMapping();
$mapping->setOptions($options);
$result = $mapping->map($posts);
Look at test directory for social app example, (*9)
Doc
MapOption: multiple map and merge definitions., (*10)
MapOption functions:
->setVar($targetVar) required: target variable for relation data
->setProp('viewField') required: add target results
->setMapField('sourceRelationField') required: source relation key field
->setTargetMapField('targetRelationId') optional: is empty then use source map field
->setSetType('add') optional: item add type (set|add|bool) default: set
->setSelects(array('id', 'message')) optional: select visible fields, (*11)
OptionMapping: main map and merge operations., (*12)
OptionMapping functions:
->setOptions($defineOptionList) required: map rule definitions
->map($mainData) required: main source data, (*13)
Features
- Establish a relationship between the recipients
- Data merge with custom field
- Array or Object usable
- Select visible fields
- Easy use
Examples and Tests
Look at Tests folder, (*14)
Support Types:
Array and Object, (*15)
Todos
Nested data, (*16)
Requires & Dependency
PHP Version >= 5.4, (*17)
Version
v1.1.0, (*18)
License
MIT, (*19)
Questions, suggestions, comments:, (*20)
Tahsin Yüksel info@tahsinyuksel.com, (*21)